Skip to content

Commit e395ebe

Browse files
xtqqczzemattsu2020
authored andcommitted
benchmarks: reduce allocations in run_util_function (uutils#10378)
1 parent 00c49ad commit e395ebe

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/uucore/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ nix = { workspace = true, features = [
9696
"poll",
9797
] }
9898
xattr = { workspace = true, optional = true }
99+
itertools = { workspace = true, optional = true }
99100

100101
[dev-dependencies]
101102
tempfile = { workspace = true }
@@ -186,4 +187,4 @@ wide = []
186187
tty = []
187188
time = ["jiff"]
188189
uptime = ["jiff", "libc", "windows-sys", "utmpx", "utmp-classic"]
189-
benchmark = ["divan", "tempfile"]
190+
benchmark = ["divan", "itertools", "tempfile"]

src/uucore/src/lib/features/benchmark.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::fs::File;
1212
use std::io::{BufWriter, Write};
1313
use std::path::{Path, PathBuf};
1414

15+
use itertools::Itertools as _;
16+
1517
/// Create a temporary file with test data
1618
pub fn create_test_file(data: &[u8], temp_dir: &Path) -> PathBuf {
1719
let file_path = temp_dir.join("test_data.txt");
@@ -32,8 +34,9 @@ where
3234
F: FnOnce(std::vec::IntoIter<std::ffi::OsString>) -> i32,
3335
{
3436
// Prepend a dummy program name as argv[0] since clap expects it
35-
let mut os_args: Vec<std::ffi::OsString> = vec!["benchmark".into()];
36-
os_args.extend(args.iter().map(|s| (*s).into()));
37+
let os_args = std::iter::once("benchmark".into())
38+
.chain(args.iter().map(Into::into))
39+
.collect_vec();
3740
util_func(os_args.into_iter())
3841
}
3942

0 commit comments

Comments
 (0)