From 543c566cccc4b8aa26f799441a93b0db59da4b07 Mon Sep 17 00:00:00 2001 From: JT Date: Tue, 26 Oct 2021 16:22:37 +1300 Subject: [PATCH] Some iter perf improvements --- crates/nu-command/src/core_commands/for_.rs | 13 ++++---- crates/nu-command/src/filters/each.rs | 35 ++++++--------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/crates/nu-command/src/core_commands/for_.rs b/crates/nu-command/src/core_commands/for_.rs index f9278de05e..beee775b22 100644 --- a/crates/nu-command/src/core_commands/for_.rs +++ b/crates/nu-command/src/core_commands/for_.rs @@ -56,18 +56,17 @@ impl Command for For { .expect("internal error: expected block"); let engine_state = engine_state.clone(); - let block = engine_state.get_block(block_id); + let block = engine_state.get_block(block_id).clone(); let mut stack = stack.collect_captures(&block.captures); match values { Value::List { vals, .. } => Ok(vals .into_iter() .map(move |x| { - let mut stack = stack.clone(); stack.add_var(var_id, x); - let block = engine_state.get_block(block_id); - match eval_block(&engine_state, &mut stack, block, PipelineData::new()) { + //let block = engine_state.get_block(block_id); + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { Ok(pipeline_data) => pipeline_data.into_value(), Err(error) => Value::Error { error }, } @@ -78,8 +77,8 @@ impl Command for For { .map(move |x| { stack.add_var(var_id, x); - let block = engine_state.get_block(block_id); - match eval_block(&engine_state, &mut stack, block, PipelineData::new()) { + //let block = engine_state.get_block(block_id); + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { Ok(pipeline_data) => pipeline_data.into_value(), Err(error) => Value::Error { error }, } @@ -88,7 +87,7 @@ impl Command for For { x => { stack.add_var(var_id, x); - eval_block(&engine_state, &mut stack, block, PipelineData::new()) + eval_block(&engine_state, &mut stack, &block, PipelineData::new()) } } } diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index 6cbaf8c0e9..145584b53d 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -64,7 +64,7 @@ impl Command for Each { let numbered = call.has_flag("numbered"); let engine_state = engine_state.clone(); - let block = engine_state.get_block(block_id); + let block = engine_state.get_block(block_id).clone(); let mut stack = stack.collect_captures(&block.captures); let span = call.head; @@ -73,10 +73,6 @@ impl Command for Each { .into_range_iter()? .enumerate() .map(move |(idx, x)| { - let block = engine_state.get_block(block_id); - - let mut stack = stack.clone(); - if let Some(var) = block.signature.get_positional(0) { if let Some(var_id) = &var.var_id { if numbered { @@ -100,21 +96,16 @@ impl Command for Each { } } - match eval_block(&engine_state, &mut stack, block, PipelineData::new()) { - Ok(v) => v, - Err(error) => Value::Error { error }.into_pipeline_data(), + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { + Ok(v) => v.into_value(), + Err(error) => Value::Error { error }, } }) - .flatten() .into_pipeline_data()), PipelineData::Value(Value::List { vals: val, .. }) => Ok(val .into_iter() .enumerate() .map(move |(idx, x)| { - let block = engine_state.get_block(block_id); - - let mut stack = stack.clone(); - if let Some(var) = block.signature.get_positional(0) { if let Some(var_id) = &var.var_id { if numbered { @@ -138,20 +129,15 @@ impl Command for Each { } } - match eval_block(&engine_state, &mut stack, block, PipelineData::new()) { - Ok(v) => v, - Err(error) => Value::Error { error }.into_pipeline_data(), + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { + Ok(v) => v.into_value(), + Err(error) => Value::Error { error }, } }) - .flatten() .into_pipeline_data()), PipelineData::Stream(stream) => Ok(stream .enumerate() .map(move |(idx, x)| { - let block = engine_state.get_block(block_id); - - let mut stack = stack.clone(); - if let Some(var) = block.signature.get_positional(0) { if let Some(var_id) = &var.var_id { if numbered { @@ -175,12 +161,11 @@ impl Command for Each { } } - match eval_block(&engine_state, &mut stack, block, PipelineData::new()) { - Ok(v) => v, - Err(error) => Value::Error { error }.into_pipeline_data(), + match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) { + Ok(v) => v.into_value(), + Err(error) => Value::Error { error }, } }) - .flatten() .into_pipeline_data()), PipelineData::Value(Value::Record { cols, vals, .. }) => { let mut output_cols = vec![];