From b1333b53ad6595e12587e8bbc58509fb2551bc43 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 10 Dec 2025 11:35:29 +0100 Subject: [PATCH] editor: Improve performance of `create_highlight_endpoints` (#44521) We reallocate quite a bunch in this codepath even though we don't need to, we already roughly know what number of elements we are working with so we can reduce the required allocations to some degree. This also reduces the amount of anchor comparisons required. Came up in profiling for https://github.com/zed-industries/zed/issues/44503 Release Notes: - N/A *or* Added/Fixed/Improved ... --- crates/editor/src/display_map/custom_highlights.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/display_map/custom_highlights.rs b/crates/editor/src/display_map/custom_highlights.rs index c9202280bf957fac4d729bab558f686c0f62e774..1ece2493e3228536999036a32959a6228f0f7cd1 100644 --- a/crates/editor/src/display_map/custom_highlights.rs +++ b/crates/editor/src/display_map/custom_highlights.rs @@ -79,12 +79,15 @@ fn create_highlight_endpoints( let start_ix = ranges .binary_search_by(|probe| probe.end.cmp(&start, buffer).then(cmp::Ordering::Less)) .unwrap_or_else(|i| i); + let end_ix = ranges[start_ix..] + .binary_search_by(|probe| { + probe.start.cmp(&end, buffer).then(cmp::Ordering::Greater) + }) + .unwrap_or_else(|i| i); - for range in &ranges[start_ix..] { - if range.start.cmp(&end, buffer).is_ge() { - break; - } + highlight_endpoints.reserve(2 * end_ix); + for range in &ranges[start_ix..][..end_ix] { let start = range.start.to_offset(buffer); let end = range.end.to_offset(buffer); if start == end {