Extract run blocks into functions (#7964)

As usual, Rust optimizes a bit less if more function boundaries are
introduced.
However, in my opinion, being able to comprehend the decision tree beats
a couple of string allocations.
This commit is contained in:
Hofer-Julian 2023-02-05 22:20:35 +01:00 committed by GitHub
parent 99076af18b
commit c79ece2b21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 264 additions and 220 deletions

View File

@ -1,32 +1,27 @@
mod command; mod command;
mod config_files; mod config_files;
mod logger; mod logger;
mod run;
mod signals; mod signals;
mod terminal; mod terminal;
mod test_bins; mod test_bins;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(feature = "plugin")]
use crate::config_files::NUSHELL_FOLDER;
use crate::{ use crate::{
command::parse_commandline_args, command::parse_commandline_args,
config_files::{set_config_path, setup_config}, config_files::set_config_path,
logger::{configure, logger}, logger::{configure, logger},
terminal::acquire_terminal, terminal::acquire_terminal,
}; };
use command::gather_commandline_args; use command::gather_commandline_args;
use log::Level; use log::Level;
use miette::Result; use miette::Result;
#[cfg(feature = "plugin")] use nu_cli::{gather_parent_env_vars, get_init_cwd, report_error_new};
use nu_cli::read_plugin_file;
use nu_cli::{
evaluate_commands, evaluate_file, evaluate_repl, gather_parent_env_vars, get_init_cwd,
report_error_new,
};
use nu_command::create_default_context; use nu_command::create_default_context;
use nu_protocol::{util::BufferedReader, PipelineData, RawStream}; use nu_protocol::{util::BufferedReader, PipelineData, RawStream};
use nu_utils::utils::perf; use nu_utils::utils::perf;
use run::{run_commands, run_file, run_repl};
use signals::{ctrlc_protection, sigquit_protection}; use signals::{ctrlc_protection, sigquit_protection};
use std::{ use std::{
io::BufReader, io::BufReader,
@ -69,7 +64,11 @@ fn main() -> Result<()> {
.unwrap_or_else(|_| std::process::exit(1)); .unwrap_or_else(|_| std::process::exit(1));
let use_color = engine_state.get_config().use_ansi_coloring; let use_color = engine_state.get_config().use_ansi_coloring;
if let Some(level) = parsed_nu_cli_args.log_level.map(|level| level.item) { if let Some(level) = parsed_nu_cli_args
.log_level
.as_ref()
.map(|level| level.item.clone())
{
let level = if Level::from_str(&level).is_ok() { let level = if Level::from_str(&level).is_ok() {
level level
} else { } else {
@ -80,7 +79,8 @@ fn main() -> Result<()> {
}; };
let target = parsed_nu_cli_args let target = parsed_nu_cli_args
.log_target .log_target
.map(|target| target.item) .as_ref()
.map(|target| target.item.clone())
.unwrap_or_else(|| "stderr".to_string()); .unwrap_or_else(|| "stderr".to_string());
logger(|builder| configure(&level, &target, builder))?; logger(|builder| configure(&level, &target, builder))?;
@ -136,7 +136,7 @@ fn main() -> Result<()> {
); );
start_time = std::time::Instant::now(); start_time = std::time::Instant::now();
if let Some(t) = parsed_nu_cli_args.threads { if let Some(t) = parsed_nu_cli_args.threads.clone() {
// 0 means to let rayon decide how many threads to use // 0 means to let rayon decide how many threads to use
let threads = t.as_i64().unwrap_or(0); let threads = t.as_i64().unwrap_or(0);
rayon::ThreadPoolBuilder::new() rayon::ThreadPoolBuilder::new()
@ -224,222 +224,24 @@ fn main() -> Result<()> {
use_color, use_color,
); );
let mut stack = nu_protocol::engine::Stack::new(); if let Some(commands) = parsed_nu_cli_args.commands.clone() {
run_commands(
if let Some(commands) = &parsed_nu_cli_args.commands {
start_time = std::time::Instant::now();
#[cfg(feature = "plugin")]
read_plugin_file(
&mut engine_state, &mut engine_state,
&mut stack, parsed_nu_cli_args,
parsed_nu_cli_args.plugin_file,
NUSHELL_FOLDER,
);
perf(
"read plugins",
start_time,
file!(),
line!(),
column!(),
use_color, use_color,
); &commands,
start_time = std::time::Instant::now();
// only want to load config and env if relative argument is provided.
if parsed_nu_cli_args.env_file.is_some() {
config_files::read_config_file(
&mut engine_state,
&mut stack,
parsed_nu_cli_args.env_file,
true,
);
} else {
config_files::read_default_env_file(&mut engine_state, &mut stack)
}
perf(
"read env.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
if parsed_nu_cli_args.config_file.is_some() {
config_files::read_config_file(
&mut engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
);
}
perf(
"read config.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
let ret_val = evaluate_commands(
commands,
&mut engine_state,
&mut stack,
input, input,
parsed_nu_cli_args.table_mode, )
);
perf(
"evaluate_commands",
start_time,
file!(),
line!(),
column!(),
use_color,
);
match ret_val {
Ok(Some(exit_code)) => std::process::exit(exit_code as i32),
Ok(None) => Ok(()),
Err(e) => Err(e),
}
} else if !script_name.is_empty() && parsed_nu_cli_args.interactive_shell.is_none() { } else if !script_name.is_empty() && parsed_nu_cli_args.interactive_shell.is_none() {
start_time = std::time::Instant::now(); run_file(
#[cfg(feature = "plugin")]
read_plugin_file(
&mut engine_state, &mut engine_state,
&mut stack, parsed_nu_cli_args,
parsed_nu_cli_args.plugin_file,
NUSHELL_FOLDER,
);
perf(
"read plugins",
start_time,
file!(),
line!(),
column!(),
use_color, use_color,
);
start_time = std::time::Instant::now();
// only want to load config and env if relative argument is provided.
if parsed_nu_cli_args.env_file.is_some() {
config_files::read_config_file(
&mut engine_state,
&mut stack,
parsed_nu_cli_args.env_file,
true,
);
} else {
config_files::read_default_env_file(&mut engine_state, &mut stack)
}
perf(
"read env.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
if parsed_nu_cli_args.config_file.is_some() {
config_files::read_config_file(
&mut engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
);
}
perf(
"read config.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
let ret_val = evaluate_file(
script_name, script_name,
&args_to_script, args_to_script,
&mut engine_state,
&mut stack,
input, input,
); )
perf(
"evaluate_file",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
let last_exit_code = stack.get_env_var(&engine_state, "LAST_EXIT_CODE");
if let Some(last_exit_code) = last_exit_code {
let value = last_exit_code.as_integer();
if let Ok(value) = value {
if value != 0 {
std::process::exit(value as i32);
}
}
}
perf(
"get exit code",
start_time,
file!(),
line!(),
column!(),
use_color,
);
ret_val
} else { } else {
start_time = std::time::Instant::now(); run_repl(engine_state, parsed_nu_cli_args, entire_start_time)
setup_config(
&mut engine_state,
&mut stack,
#[cfg(feature = "plugin")]
parsed_nu_cli_args.plugin_file,
parsed_nu_cli_args.config_file,
parsed_nu_cli_args.env_file,
parsed_nu_cli_args.login_shell.is_some(),
);
// Reload use_color from config in case it's different from the default value
let use_color = engine_state.get_config().use_ansi_coloring;
perf(
"setup_config",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
let ret_val = evaluate_repl(
&mut engine_state,
&mut stack,
config_files::NUSHELL_FOLDER,
parsed_nu_cli_args.execute,
entire_start_time,
);
perf(
"evaluate_repl",
start_time,
file!(),
line!(),
column!(),
use_color,
);
ret_val
} }
} }

242
src/run.rs Normal file
View File

@ -0,0 +1,242 @@
#[cfg(feature = "plugin")]
use crate::config_files::NUSHELL_FOLDER;
use crate::{
command,
config_files::{self, setup_config},
};
#[cfg(feature = "plugin")]
use nu_cli::read_plugin_file;
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl};
use nu_protocol::PipelineData;
use nu_utils::utils::perf;
pub(crate) fn run_commands(
engine_state: &mut nu_protocol::engine::EngineState,
parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool,
commands: &nu_protocol::Spanned<String>,
input: PipelineData,
) -> Result<(), miette::ErrReport> {
let mut stack = nu_protocol::engine::Stack::new();
let start_time = std::time::Instant::now();
#[cfg(feature = "plugin")]
read_plugin_file(
engine_state,
&mut stack,
parsed_nu_cli_args.plugin_file,
NUSHELL_FOLDER,
);
perf(
"read plugins",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
// only want to load config and env if relative argument is provided.
if parsed_nu_cli_args.env_file.is_some() {
config_files::read_config_file(engine_state, &mut stack, parsed_nu_cli_args.env_file, true);
} else {
config_files::read_default_env_file(engine_state, &mut stack)
}
perf(
"read env.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
if parsed_nu_cli_args.config_file.is_some() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
);
}
perf(
"read config.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
let ret_val = evaluate_commands(
commands,
engine_state,
&mut stack,
input,
parsed_nu_cli_args.table_mode,
);
perf(
"evaluate_commands",
start_time,
file!(),
line!(),
column!(),
use_color,
);
match ret_val {
Ok(Some(exit_code)) => std::process::exit(exit_code as i32),
Ok(None) => Ok(()),
Err(e) => Err(e),
}
}
pub(crate) fn run_file(
engine_state: &mut nu_protocol::engine::EngineState,
parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool,
script_name: String,
args_to_script: Vec<String>,
input: PipelineData,
) -> Result<(), miette::ErrReport> {
let mut stack = nu_protocol::engine::Stack::new();
let start_time = std::time::Instant::now();
#[cfg(feature = "plugin")]
read_plugin_file(
engine_state,
&mut stack,
parsed_nu_cli_args.plugin_file,
NUSHELL_FOLDER,
);
perf(
"read plugins",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
// only want to load config and env if relative argument is provided.
if parsed_nu_cli_args.env_file.is_some() {
config_files::read_config_file(engine_state, &mut stack, parsed_nu_cli_args.env_file, true);
} else {
config_files::read_default_env_file(engine_state, &mut stack)
}
perf(
"read env.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
if parsed_nu_cli_args.config_file.is_some() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
);
}
perf(
"read config.nu",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
let ret_val = evaluate_file(
script_name,
&args_to_script,
engine_state,
&mut stack,
input,
);
perf(
"evaluate_file",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
let last_exit_code = stack.get_env_var(&*engine_state, "LAST_EXIT_CODE");
if let Some(last_exit_code) = last_exit_code {
let value = last_exit_code.as_integer();
if let Ok(value) = value {
if value != 0 {
std::process::exit(value as i32);
}
}
}
perf(
"get exit code",
start_time,
file!(),
line!(),
column!(),
use_color,
);
ret_val
}
pub(crate) fn run_repl(
mut engine_state: nu_protocol::engine::EngineState,
parsed_nu_cli_args: command::NushellCliArgs,
entire_start_time: std::time::Instant,
) -> Result<(), miette::ErrReport> {
let mut stack = nu_protocol::engine::Stack::new();
let start_time = std::time::Instant::now();
setup_config(
&mut engine_state,
&mut stack,
#[cfg(feature = "plugin")]
parsed_nu_cli_args.plugin_file,
parsed_nu_cli_args.config_file,
parsed_nu_cli_args.env_file,
parsed_nu_cli_args.login_shell.is_some(),
);
// Reload use_color from config in case it's different from the default value
let use_color = engine_state.get_config().use_ansi_coloring;
perf(
"setup_config",
start_time,
file!(),
line!(),
column!(),
use_color,
);
let start_time = std::time::Instant::now();
let ret_val = evaluate_repl(
&mut engine_state,
&mut stack,
config_files::NUSHELL_FOLDER,
parsed_nu_cli_args.execute,
entire_start_time,
);
perf(
"evaluate_repl",
start_time,
file!(),
line!(),
column!(),
use_color,
);
ret_val
}