Remove some unnecessary static Vecs (#11947)

Avoid unnecessary allocations or larger iterator structs

- Turn static `Vec`s into arrays when possible
- Use `std::iter::once`/`empty` where applicable
- Use `bool::then_some` in `detect column` `.chain`
- Drop in the bucket: de-vec-ing tests
This commit is contained in:
Stefan Holderbach 2024-02-24 20:58:01 +01:00 committed by GitHub
parent 098527b263
commit 7884de1941
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 115 additions and 113 deletions

View File

@ -253,7 +253,7 @@ mod command_completions_tests {
#[test] #[test]
fn test_find_non_whitespace_index() { fn test_find_non_whitespace_index() {
let commands = vec![ let commands = [
(" hello", 4), (" hello", 4),
("sudo ", 0), ("sudo ", 0),
(" sudo ", 2), (" sudo ", 2),
@ -273,7 +273,7 @@ mod command_completions_tests {
#[test] #[test]
fn test_is_last_command_passthrough() { fn test_is_last_command_passthrough() {
let commands = vec![ let commands = [
(" hello", false), (" hello", false),
(" sudo ", true), (" sudo ", true),
("sudo ", true), ("sudo ", true),

View File

@ -564,7 +564,7 @@ mod completer_tests {
); );
let mut completer = NuCompleter::new(engine_state.into(), Stack::new()); let mut completer = NuCompleter::new(engine_state.into(), Stack::new());
let dataset = vec![ let dataset = [
("sudo", false, "", Vec::new()), ("sudo", false, "", Vec::new()),
("sudo l", true, "l", vec!["ls", "let", "lines", "loop"]), ("sudo l", true, "l", vec!["ls", "let", "lines", "loop"]),
(" sudo", false, "", Vec::new()), (" sudo", false, "", Vec::new()),

View File

@ -84,7 +84,7 @@ pub(crate) fn add_menus(
} }
// Checking if the default menus have been added from the config file // Checking if the default menus have been added from the config file
let default_menus = vec![ let default_menus = [
("completion_menu", DEFAULT_COMPLETION_MENU), ("completion_menu", DEFAULT_COMPLETION_MENU),
("history_menu", DEFAULT_HISTORY_MENU), ("history_menu", DEFAULT_HISTORY_MENU),
("help_menu", DEFAULT_HELP_MENU), ("help_menu", DEFAULT_HELP_MENU),

View File

@ -154,9 +154,9 @@ impl Command for Do {
let ctrlc = stdout_stream.ctrlc.clone(); let ctrlc = stdout_stream.ctrlc.clone();
let span = stdout_stream.span; let span = stdout_stream.span;
RawStream::new( RawStream::new(
Box::new( Box::new(std::iter::once(
vec![stdout_stream.into_bytes().map(|s| s.item)].into_iter(), stdout_stream.into_bytes().map(|s| s.item),
), )),
ctrlc, ctrlc,
span, span,
None, None,
@ -213,7 +213,7 @@ impl Command for Do {
Ok(PipelineData::ExternalStream { Ok(PipelineData::ExternalStream {
stdout, stdout,
stderr: Some(RawStream::new( stderr: Some(RawStream::new(
Box::new(vec![Ok(stderr_msg.into_bytes())].into_iter()), Box::new(std::iter::once(Ok(stderr_msg.into_bytes()))),
stderr_ctrlc, stderr_ctrlc,
span, span,
None, None,

View File

@ -46,7 +46,7 @@ pub(crate) fn generate_strftime_list(head: Span, show_parse_only_formats: bool)
description: &'a str, description: &'a str,
} }
let specifications = vec![ let specifications = [
FormatSpecification { FormatSpecification {
spec: "%Y", spec: "%Y",
description: "The full proleptic Gregorian year, zero-padded to 4 digits.", description: "The full proleptic Gregorian year, zero-padded to 4 digits.",

View File

@ -85,7 +85,7 @@ If multiple cell paths are given, this will produce a list of values."#
} else { } else {
let mut output = vec![]; let mut output = vec![];
let paths = vec![cell_path].into_iter().chain(rest); let paths = std::iter::once(cell_path).chain(rest);
let input = input.into_value(span); let input = input.into_value(span);

View File

@ -128,11 +128,10 @@ fn detect_columns(
} }
} }
Ok((if noheader { Ok(noheader
vec![orig_headers].into_iter().chain(input) .then_some(orig_headers)
} else { .into_iter()
vec![].into_iter().chain(input) .chain(input)
})
.map(move |x| { .map(move |x| {
let row = find_columns(&x); let row = find_columns(&x);
@ -148,7 +147,9 @@ fn detect_columns(
// column counts don't line up, so see if we can figure out why // column counts don't line up, so see if we can figure out why
for cell in row { for cell in row {
for header in &headers { for header in &headers {
if cell.span.start <= header.span.end && cell.span.end > header.span.start { if cell.span.start <= header.span.end
&& cell.span.end > header.span.start
{
pre_output.push(( pre_output.push((
header.item.to_string(), header.item.to_string(),
Value::string(&cell.item, name_span), Value::string(&cell.item, name_span),
@ -195,7 +196,8 @@ fn detect_columns(
r_idx r_idx
}; };
if !(l_idx <= r_idx && (r_idx >= 0 || l_idx < (record.len() as isize))) { if !(l_idx <= r_idx && (r_idx >= 0 || l_idx < (record.len() as isize)))
{
return Value::record(record, name_span); return Value::record(record, name_span);
} }

View File

@ -550,7 +550,7 @@ fn eval_element_with_input(
// so nushell knows this result is not the last part of a command. // so nushell knows this result is not the last part of a command.
( (
Some(RawStream::new( Some(RawStream::new(
Box::new(vec![].into_iter()), Box::new(std::iter::empty()),
None, None,
*span, *span,
Some(0), Some(0),

View File

@ -605,7 +605,7 @@ impl PipelineData {
.map(|bytes| bytes.item) .map(|bytes| bytes.item)
.unwrap_or_default(); .unwrap_or_default();
RawStream::new( RawStream::new(
Box::new(vec![Ok(stderr_bytes)].into_iter()), Box::new(std::iter::once(Ok(stderr_bytes))),
stderr_ctrlc, stderr_ctrlc,
stderr_span, stderr_span,
None, None,

View File

@ -82,7 +82,7 @@ impl NuProcess {
command.env_clear(); command.env_clear();
let paths = vec![test_bins_path()]; let paths = [test_bins_path()];
let paths_joined = match std::env::join_paths(paths) { let paths_joined = match std::env::join_paths(paths) {
Ok(all) => all, Ok(all) => all,