Skip to content

Commit cd4cb43

Browse files
committed
ls: display_item_name: Make current_column a closure
In many cases, current_column value is not actually needed, but computing its value is quite expensive (`ansi_width` isn't very fast). Move the computation to a LazyCell, so that we only execute it when required. Saves 5% on a basic `ls -lR .git`.
1 parent 4212385 commit cd4cb43

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

src/uu/ls/src/ls.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::iter;
99
#[cfg(windows)]
1010
use std::os::windows::fs::MetadataExt;
11-
use std::{cell::OnceCell, num::IntErrorKind};
11+
use std::{cell::LazyCell, cell::OnceCell, num::IntErrorKind};
1212
use std::{
1313
cmp::Reverse,
1414
ffi::{OsStr, OsString},
@@ -2577,8 +2577,15 @@ fn display_items(
25772577
// whether text will wrap or not, because when format is grid or
25782578
// column ls will try to place the item name in a new line if it
25792579
// wraps.
2580-
let cell =
2581-
display_item_name(i, config, prefix_context, more_info, out, style_manager, 0);
2580+
let cell = display_item_name(
2581+
i,
2582+
config,
2583+
prefix_context,
2584+
more_info,
2585+
out,
2586+
style_manager,
2587+
LazyCell::new(Box::new(|| 0)),
2588+
);
25822589

25832590
names_vec.push(cell);
25842591
}
@@ -2870,7 +2877,9 @@ fn display_item_long(
28702877
String::new(),
28712878
out,
28722879
style_manager,
2873-
ansi_width(&String::from_utf8_lossy(&output_display)),
2880+
LazyCell::new(Box::new(|| {
2881+
ansi_width(&String::from_utf8_lossy(&output_display))
2882+
})),
28742883
);
28752884

28762885
let displayed_item = if quoted && !os_str_starts_with(&item_name, b"'") {
@@ -2964,7 +2973,9 @@ fn display_item_long(
29642973
String::new(),
29652974
out,
29662975
style_manager,
2967-
ansi_width(&String::from_utf8_lossy(&output_display)),
2976+
LazyCell::new(Box::new(|| {
2977+
ansi_width(&String::from_utf8_lossy(&output_display))
2978+
})),
29682979
);
29692980
let date_len = 12;
29702981

@@ -3198,13 +3209,13 @@ fn display_item_name(
31983209
more_info: String,
31993210
out: &mut BufWriter<Stdout>,
32003211
style_manager: &mut Option<StyleManager>,
3201-
current_column: usize,
3212+
current_column: LazyCell<usize, Box<dyn FnOnce() -> usize + '_>>,
32023213
) -> OsString {
32033214
// This is our return value. We start by `&path.display_name` and modify it along the way.
32043215
let mut name = escape_name(&path.display_name, &config.quoting_style);
32053216

32063217
let is_wrap =
3207-
|namelen: usize| config.width != 0 && current_column + namelen > config.width.into();
3218+
|namelen: usize| config.width != 0 && *current_column + namelen > config.width.into();
32083219

32093220
if config.hyperlink {
32103221
name = create_hyperlink(&name, path);

0 commit comments

Comments
 (0)