From 43bfecaafa7c406242e95c02c1e8d9fd21fce435 Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Mon, 18 May 2026 15:45:02 +0800 Subject: [PATCH] editor: Remove redundant allocation when merging highlight ranges (#57034) Reduce one Vec clone + collect allocation per frame Release Notes: - N/A or Added/Fixed/Improved ... --------- Signed-off-by: Xiaobo Liu --- crates/editor/src/element.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index bca778b79b..e04161b6c8 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -3682,7 +3682,7 @@ impl EditorElement { fn bg_segments_per_row( rows: Range, selections: &[(PlayerColor, Vec)], - highlight_ranges: &[(Range, Hsla)], + highlight_ranges: impl IntoIterator, Hsla)>, base_background: Hsla, ) -> Vec, Hsla)>> { if rows.start >= rows.end { @@ -3692,7 +3692,7 @@ impl EditorElement { // We don't actually know what color is behind this editor. return Vec::new(); } - let highlight_iter = highlight_ranges.iter().cloned(); + let highlight_iter = highlight_ranges.into_iter(); let selection_iter = selections.iter().flat_map(|(player_color, layouts)| { let color = player_color.selection; layouts.iter().filter_map(move |selection_layout| { @@ -10410,20 +10410,14 @@ impl Element for EditorElement { cx, ); - let merged_highlighted_ranges = - if let Some((_, colors)) = document_colors.as_ref() { - &highlighted_ranges - .clone() - .into_iter() - .chain(colors.clone()) - .collect() - } else { - &highlighted_ranges - }; let bg_segments_per_row = Self::bg_segments_per_row( start_row..end_row, &selections, - &merged_highlighted_ranges, + highlighted_ranges.iter().cloned().chain( + document_colors + .iter() + .flat_map(|(_, colors)| colors.iter().cloned()), + ), self.style.background, ); @@ -13849,7 +13843,7 @@ mod tests { let result = EditorElement::bg_segments_per_row( DisplayRow(0)..DisplayRow(5), &selections, - &[], + [].into_iter(), base_bg, ); @@ -13898,7 +13892,7 @@ mod tests { let result = EditorElement::bg_segments_per_row( DisplayRow(0)..DisplayRow(4), &selections, - &[], + [].into_iter(), base_bg, );