Support caching of trimRight

This commit is contained in:
Daniel Imms
2026-05-02 04:35:18 -07:00
parent 7fa8b7a069
commit 4d4b994c35
3 changed files with 69 additions and 20 deletions
+19 -9
View File
@@ -63,7 +63,9 @@ export class BufferLine implements IBufferLine {
protected _data: Uint32Array;
protected _combined: {[index: number]: string} = {};
protected _extendedAttrs: {[index: number]: IExtendedAttrs | undefined} = {};
private _cachedTrimmedString: string | undefined;
// Cache for canonical full-line translations.
protected _cachedCanonicalString: string | undefined;
protected _cachedCanonicalTrimmedString: string | undefined;
public length: number;
constructor(cols: number, fillCellData?: ICellData, public isWrapped: boolean = false) {
@@ -520,8 +522,8 @@ export class BufferLine implements IBufferLine {
}
/**
* Translates the buffer line to a string. Caching only applies to canonical non-trimmed
* translation requests. Using `trimRight` or any optional range/mapping arguments disables cache.
* Translates the buffer line to a string. Caching only applies to canonical full-line translation
* requests (regardless of `trimRight` value).
*
* @param trimRight Whether to trim any empty cells on the right.
* @param startCol The column to start the string (0-based inclusive).
@@ -534,9 +536,10 @@ export class BufferLine implements IBufferLine {
* returned string, the corresponding entries in `outColumns` will have the same column number.
*/
public translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string {
const isCanonicalTrimmedRequest = !trimRight && (startCol === undefined || startCol === 0) && endCol === undefined && outColumns === undefined;
if (isCanonicalTrimmedRequest && this._cachedTrimmedString !== undefined) {
return this._cachedTrimmedString;
const isCanonicalRequest = (startCol === undefined || startCol === 0) && endCol === undefined && outColumns === undefined;
const cachedResult = trimRight ? this._cachedCanonicalTrimmedString ?? this._cachedCanonicalString?.trimEnd() : this._cachedCanonicalString;
if (isCanonicalRequest && cachedResult !== undefined) {
return cachedResult;
}
startCol = startCol ?? 0;
endCol = endCol ?? this.length;
@@ -562,13 +565,20 @@ export class BufferLine implements IBufferLine {
if (outColumns) {
outColumns.push(startCol);
}
if (isCanonicalTrimmedRequest) {
this._cachedTrimmedString = result;
if (isCanonicalRequest) {
if (trimRight) {
this._cachedCanonicalTrimmedString = result;
} else {
this._cachedCanonicalString = result;
// Free the trimmed string as trimEnd() will be used to save memory
this._cachedCanonicalTrimmedString = undefined;
}
}
return result;
}
private _invalidateStringCache(): void {
this._cachedTrimmedString = undefined;
this._cachedCanonicalString = undefined;
this._cachedCanonicalTrimmedString = undefined;
}
}