Skip to content

Commit 36a3621

Browse files
committed
refactor: move path_components construction into maybe_color_slice
Defer the path_components Vec allocation to inside maybe_color_slice, so it only happens when coloring is actually enabled. This avoids unnecessary per-row allocation in the common no-color case. Also removes the pipe_trait::Pipe dependency from methods.rs. https://claude.ai/code/session_012139SmUSC1oYSKNkvsg78g
1 parent 0bfaf85 commit 36a3621

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

src/visualizer/coloring.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,21 @@ impl Width for ColoredTreeHorizontalSlice<'_> {
110110
}
111111

112112
/// Wrap a [`TreeHorizontalSlice`] with color if coloring is available, otherwise return it as-is.
113+
///
114+
/// Path components are only constructed when coloring is enabled, avoiding
115+
/// unnecessary allocation in the common no-color case.
113116
pub(super) fn maybe_color_slice<'a, 'b>(
114117
coloring: Option<&'b Coloring<'a>>,
115-
path_components: &[&'a OsStr],
118+
ancestors: impl Iterator<Item = &'a OsStr>,
119+
name: &'a OsStr,
116120
has_children: bool,
117121
slice: TreeHorizontalSlice<String>,
118122
) -> MaybeColoredTreeHorizontalSlice<'b> {
119123
match coloring {
120-
Some(coloring) => coloring.maybe_color_tree_slice(path_components, has_children, slice),
124+
Some(coloring) => {
125+
let path_components: Vec<&OsStr> = ancestors.chain(std::iter::once(name)).collect();
126+
coloring.maybe_color_tree_slice(&path_components, has_children, slice)
127+
}
121128
None => MaybeColoredTreeHorizontalSlice::Colorless(slice),
122129
}
123130
}

src/visualizer/methods.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use tree_table::*;
1414

1515
use super::{coloring::maybe_color_slice, ColumnWidthDistribution, Visualizer};
1616
use crate::size;
17-
use pipe_trait::Pipe;
18-
use std::{cmp::min, ffi::OsStr, fmt::Display, iter::once};
17+
use std::{cmp::min, ffi::OsStr, fmt::Display};
1918
use zero_copy_pads::{align_left, align_right};
2019

2120
impl<'a, Name, Size> Visualizer<'a, Name, Size>
@@ -92,15 +91,10 @@ where
9291
tree_horizontal_slice,
9392
} = tree_row;
9493

95-
let path_components: Vec<&OsStr> = initial_row
96-
.ancestors
97-
.iter()
98-
.map(|node| node.name.as_ref())
99-
.chain(initial_row.node_info.name.pipe_as_ref(once))
100-
.collect();
10194
let tree = maybe_color_slice(
10295
self.coloring,
103-
&path_components,
96+
initial_row.ancestors.iter().map(|node| node.name.as_ref()),
97+
initial_row.node_info.name.as_ref(),
10498
initial_row.node_info.children_count > 0,
10599
tree_horizontal_slice,
106100
);

0 commit comments

Comments
 (0)