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 <cppcoffee@gmail.com>
This commit is contained in:
1 parent
df0eb4f2a9
commit
43bfecaafa
1 file changed
+9
-15
@@ -3682,7 +3682,7 @@ impl EditorElement {
|
||||
fn bg_segments_per_row(
|
||||
rows: Range<DisplayRow>,
|
||||
selections: &[(PlayerColor, Vec<SelectionLayout>)],
|
||||
highlight_ranges: &[(Range<DisplayPoint>, Hsla)],
|
||||
highlight_ranges: impl IntoIterator<Item = (Range<DisplayPoint>, Hsla)>,
|
||||
base_background: Hsla,
|
||||
) -> Vec<Vec<(Range<DisplayPoint>, 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,
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user