Skip to content

Commit

Permalink
Optimize stdbuf using OnceCell
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed May 1, 2024
1 parent 9a7ebad commit 6ea4a16
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
0.4
- Better shell specific examples for `rew x` + cleanup and reorder all `rew x` examples.
- Refactor and split `x.rs` into multiple files.
- Add missing unit test to increase code coverage.
- Prepare changelog for 0.4.0
- Finish user guide (performance, caveats, global opts propagation in `rew x`, forward compatibility, versioning).
Expand Down
6 changes: 3 additions & 3 deletions src/commands/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ fn eval_simple_pattern(context: &Context, pattern: &SimplePattern) -> Result<()>
}

fn eval_pattern(context: &Context, pattern: &Pattern, shell: &Shell) -> Result<()> {
let mut env = context.env();
let env = context.env();
let mut children = Vec::new();
let mut producers = Vec::new();
let mut consumers = Vec::new();

for item in pattern.items() {
match &item {
Item::Constant(value) => producers.push(Producer::Constant(value.clone())),
Item::Expression(ref expr) => match build_pipeline(&mut env, shell, expr) {
Item::Expression(ref expr) => match build_pipeline(&env, shell, expr) {
Ok(mut pipeline) => {
pipeline.add_context(ContextItem {
name: "expression",
Expand Down Expand Up @@ -262,7 +262,7 @@ fn eval_pattern(context: &Context, pattern: &Pattern, shell: &Shell) -> Result<(
}
}

fn build_pipeline(env: &mut Env, shell: &Shell, expr: &Expression) -> Result<Pipeline> {
fn build_pipeline(env: &Env, shell: &Shell, expr: &Expression) -> Result<Pipeline> {
let stdin_mode = if expr.no_stdin {
StdinMode::Disconnected
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Env {
]
}

pub fn external(&mut self) -> Vec<(String, String)> {
pub fn external(&self) -> Vec<(String, String)> {
let mut env = Vec::new();

if self.args.buf_mode.is_line() {
Expand Down
2 changes: 1 addition & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Command {
}
}

pub fn build(&self, env: &mut Env) -> Result<process::Command> {
pub fn build(&self, env: &Env) -> Result<process::Command> {
match self {
Command::Internal { meta, args } => {
let mut command = internal_command()?;
Expand Down
7 changes: 4 additions & 3 deletions src/stdbuf.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use anyhow::Result;
use bstr::ByteSlice;
use std::cell::OnceCell;
use std::process::Command;
use std::process::Stdio;

#[derive(Default)]
pub struct StdBuf {
line_buf_env: Option<Vec<(String, String)>>,
line_buf_env: OnceCell<Vec<(String, String)>>,
}

impl StdBuf {
pub fn line_buf_env(&mut self) -> Vec<(String, String)> {
pub fn line_buf_env(&self) -> Vec<(String, String)> {
self.line_buf_env
.get_or_insert_with(|| Self::detect_env(&["-oL"]).unwrap_or_default())
.get_or_init(|| Self::detect_env(&["-oL"]).unwrap_or_default())
.clone()
}

Expand Down

0 comments on commit 6ea4a16

Please sign in to comment.