add parameter to set thread count for parallel commands (#4424)

This commit is contained in:
Darren Schroeder 2022-02-11 12:46:36 -06:00 committed by GitHub
parent a16e485cce
commit 968ef1e953
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

1
Cargo.lock generated
View file

@ -2123,6 +2123,7 @@ dependencies = [
"nu_plugin_query", "nu_plugin_query",
"pretty_assertions", "pretty_assertions",
"pretty_env_logger", "pretty_env_logger",
"rayon",
"reedline", "reedline",
"rstest", "rstest",
"serial_test", "serial_test",

View file

@ -52,6 +52,7 @@ nu-system = { path = "./crates/nu-system", version = "0.59.0" }
nu-table = { path = "./crates/nu-table", version = "0.59.0" } nu-table = { path = "./crates/nu-table", version = "0.59.0" }
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.59.0" } nu-term-grid = { path = "./crates/nu-term-grid", version = "0.59.0" }
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
rayon = "1.5.1"
reedline = { git = "https://github.com/nushell/reedline", branch = "main" } reedline = { git = "https://github.com/nushell/reedline", branch = "main" }
# mimalloc = { version = "*", default-features = false } # mimalloc = { version = "*", default-features = false }

View file

@ -14,7 +14,7 @@ use crate::logger::{configure, logger};
use log::info; use log::info;
use miette::Result; use miette::Result;
use nu_command::{create_default_context, BufferedReader}; use nu_command::{create_default_context, BufferedReader};
use nu_engine::get_full_help; use nu_engine::{get_full_help, CallExt};
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
ast::{Call, Expr, Expression, Pipeline, Statement}, ast::{Call, Expr, Expression, Pipeline, Statement},
@ -103,6 +103,7 @@ fn main() -> Result<()> {
|| arg == "--loglevel" || arg == "--loglevel"
|| arg == "--config-file" || arg == "--config-file"
|| arg == "--perf" || arg == "--perf"
|| arg == "--threads"
{ {
collect_arg_nushell = true; collect_arg_nushell = true;
} }
@ -122,6 +123,15 @@ fn main() -> Result<()> {
match parsed_nu_cli_args { match parsed_nu_cli_args {
Ok(binary_args) => { Ok(binary_args) => {
if let Some(t) = binary_args.threads {
// 0 means to let rayon decide how many threads to use
let threads = t.as_i64().unwrap_or(0);
rayon::ThreadPoolBuilder::new()
.num_threads(threads as usize)
.build_global()
.expect("error setting number of threads");
}
set_is_perf_value(binary_args.perf); set_is_perf_value(binary_args.perf);
if binary_args.perf { if binary_args.perf {
@ -248,6 +258,7 @@ fn parse_commandline_args(
let commands: Option<Expression> = call.get_flag_expr("commands"); let commands: Option<Expression> = call.get_flag_expr("commands");
let testbin: Option<Expression> = call.get_flag_expr("testbin"); let testbin: Option<Expression> = call.get_flag_expr("testbin");
let perf = call.has_flag("perf"); let perf = call.has_flag("perf");
let threads: Option<Value> = call.get_flag(engine_state, &mut stack, "threads")?;
let commands = if let Some(expression) = commands { let commands = if let Some(expression) = commands {
let contents = engine_state.get_span_contents(&expression.span); let contents = engine_state.get_span_contents(&expression.span);
@ -293,6 +304,7 @@ fn parse_commandline_args(
commands, commands,
testbin, testbin,
perf, perf,
threads,
}); });
} }
} }
@ -311,6 +323,7 @@ struct NushellCliArgs {
commands: Option<Spanned<String>>, commands: Option<Spanned<String>>,
testbin: Option<Spanned<String>>, testbin: Option<Spanned<String>>,
perf: bool, perf: bool,
threads: Option<Value>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -349,6 +362,12 @@ impl Command for Nu {
SyntaxShape::Filepath, SyntaxShape::Filepath,
"name of the optional script file to run", "name of the optional script file to run",
) )
.named(
"threads",
SyntaxShape::Int,
"threads to use for parallel commands",
Some('t'),
)
.rest( .rest(
"script args", "script args",
SyntaxShape::String, SyntaxShape::String,