perf(addon-search): streamline decoration creation

Create highlight decorations in a single pass, dispose markers when
registration fails, and reduce per-decoration allocation overhead.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Daniel Imms
2026-05-25 19:15:28 -07:00
co-authored by Cursor
parent 3ee7df2596
commit 8405d584d8
+24 -25
View File
@@ -13,7 +13,6 @@ import type { ISearchResult } from './SearchEngine';
*/ */
interface IHighlight extends IDisposable { interface IHighlight extends IDisposable {
decoration: IDecoration; decoration: IDecoration;
match: ISearchResult;
} }
/** /**
@@ -49,7 +48,7 @@ export class DecorationManager extends Disposable {
const decorations = this._createResultDecorations(match, options, false); const decorations = this._createResultDecorations(match, options, false);
if (decorations) { if (decorations) {
for (const decoration of decorations) { for (const decoration of decorations) {
this._storeDecoration(decoration, match); this._storeDecoration(decoration);
} }
} }
} }
@@ -83,9 +82,9 @@ export class DecorationManager extends Disposable {
* @param decoration The decoration to store. * @param decoration The decoration to store.
* @param match The search result this decoration represents. * @param match The search result this decoration represents.
*/ */
private _storeDecoration(decoration: IDecoration, match: ISearchResult): void { private _storeDecoration(decoration: IDecoration): void {
this._highlightedLines.add(decoration.marker.line); this._highlightedLines.add(decoration.marker.line);
this._highlightDecorations.push({ decoration, match, dispose() { decoration.dispose(); } }); this._highlightDecorations.push({ decoration, dispose() { decoration.dispose(); } });
} }
/** /**
@@ -128,38 +127,38 @@ export class DecorationManager extends Disposable {
* @returns the decorations or undefined if the marker has already been disposed of * @returns the decorations or undefined if the marker has already been disposed of
*/ */
private _createResultDecorations(result: ISearchResult, options: ISearchDecorationOptions, isActiveResult: boolean): IDecoration[] | undefined { private _createResultDecorations(result: ISearchResult, options: ISearchDecorationOptions, isActiveResult: boolean): IDecoration[] | undefined {
// Gather decoration ranges for this match as it could wrap const decorations: IDecoration[] = [];
const decorationRanges: [number, number, number][] = []; const cols = this._terminal.cols;
const layer = isActiveResult ? 'top' : 'bottom';
const backgroundColor = isActiveResult ? options.activeMatchBackground : options.matchBackground;
const borderColor = isActiveResult ? options.activeMatchBorder : options.matchBorder;
let currentCol = result.col; let currentCol = result.col;
let remainingSize = result.size; let remainingSize = result.size;
let markerOffset = -this._terminal.buffer.active.baseY - this._terminal.buffer.active.cursorY + result.row; let markerOffset = -this._terminal.buffer.active.baseY - this._terminal.buffer.active.cursorY + result.row;
while (remainingSize > 0) { while (remainingSize > 0) {
const amountThisRow = Math.min(this._terminal.cols - currentCol, remainingSize); const amountThisRow = Math.min(cols - currentCol, remainingSize);
decorationRanges.push([markerOffset, currentCol, amountThisRow]); const marker = this._terminal.registerMarker(markerOffset);
currentCol = 0;
remainingSize -= amountThisRow;
markerOffset++;
}
// Create the decorations
const decorations: IDecoration[] = [];
for (const range of decorationRanges) {
const marker = this._terminal.registerMarker(range[0]);
const decoration = this._terminal.registerDecoration({ const decoration = this._terminal.registerDecoration({
marker, marker,
x: range[1], x: currentCol,
width: range[2], width: amountThisRow,
layer: isActiveResult ? 'top' : 'bottom', layer,
backgroundColor: isActiveResult ? options.activeMatchBackground : options.matchBackground, backgroundColor,
overviewRulerOptions: this._getOverviewRulerOptions(marker.line, isActiveResult, options) overviewRulerOptions: this._getOverviewRulerOptions(marker.line, isActiveResult, options)
}); });
if (decoration) { if (decoration) {
const disposables: IDisposable[] = []; const renderDisposable = decoration.onRender((e) => this._applyStyles(e, borderColor, isActiveResult));
disposables.push(marker); decoration.onDispose(() => {
disposables.push(decoration.onRender((e) => this._applyStyles(e, isActiveResult ? options.activeMatchBorder : options.matchBorder, isActiveResult))); marker.dispose();
disposables.push(decoration.onDispose(() => dispose(disposables))); renderDisposable.dispose();
});
decorations.push(decoration); decorations.push(decoration);
} else {
marker.dispose();
} }
currentCol = 0;
remainingSize -= amountThisRow;
markerOffset++;
} }
return decorations.length === 0 ? undefined : decorations; return decorations.length === 0 ? undefined : decorations;