Improve fold bench data generation (#9210)

This commit is contained in:
mattsu 2025-11-11 04:48:28 +09:00 committed by GitHub
parent 9f4fa5bad2
commit 1eafb40628
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,6 @@
// file that was distributed with this source code.
use divan::{Bencher, black_box};
use std::fmt::Write;
use uu_fold::uumain;
use uucore::benchmark::{create_test_file, run_util_function};
@ -12,12 +11,12 @@ use uucore::benchmark::{create_test_file, run_util_function};
#[divan::bench(args = [100_000])]
fn fold_many_lines(bencher: Bencher, num_lines: usize) {
let temp_dir = tempfile::tempdir().unwrap();
// Create long lines that need folding
let data = (0..num_lines)
.fold(String::new(), |mut acc, i| {
writeln!(&mut acc, "This is a very long line number {i} that definitely needs to be folded at the default width of 80 columns").unwrap();
acc
});
let mut data = String::with_capacity(num_lines * 110);
for i in 0..num_lines {
data.push_str("This is a very long line number ");
append_usize(&mut data, i);
data.push_str(" that definitely needs to be folded at the default width of 80 columns\n");
}
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
let file_path_str = file_path.to_str().unwrap();
@ -30,14 +29,12 @@ fn fold_many_lines(bencher: Bencher, num_lines: usize) {
#[divan::bench(args = [50_000])]
fn fold_custom_width(bencher: Bencher, num_lines: usize) {
let temp_dir = tempfile::tempdir().unwrap();
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
writeln!(
&mut acc,
"Line {i} with enough text to exceed width 40 characters and require folding"
)
.unwrap();
acc
});
let mut data = String::with_capacity(num_lines * 80);
for i in 0..num_lines {
data.push_str("Line ");
append_usize(&mut data, i);
data.push_str(" with enough text to exceed width 40 characters and require folding\n");
}
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
let file_path_str = file_path.to_str().unwrap();
@ -49,3 +46,21 @@ fn fold_custom_width(bencher: Bencher, num_lines: usize) {
fn main() {
divan::main();
}
fn append_usize(buf: &mut String, mut value: usize) {
let mut digits = [0u8; 20];
let mut idx = digits.len();
if value == 0 {
buf.push('0');
return;
}
while value > 0 {
idx -= 1;
digits[idx] = b'0' + (value % 10) as u8;
value /= 10;
}
buf.push_str(std::str::from_utf8(&digits[idx..]).unwrap());
}