From 20c1f1a5f72233ba542e12d4dea0560ab69eff90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 28 Sep 2022 12:14:09 +0200 Subject: [PATCH] fix resize edge cases: always report cleanup state --- src/common/Types.d.ts | 2 +- src/common/buffer/BufferLine.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/common/Types.d.ts b/src/common/Types.d.ts index d44bb197..bbf00f1b 100644 --- a/src/common/Types.d.ts +++ b/src/common/Types.d.ts @@ -205,7 +205,7 @@ export interface IBufferLine { insertCells(pos: number, n: number, ch: ICellData, eraseAttr?: IAttributeData): void; deleteCells(pos: number, n: number, fill: ICellData, eraseAttr?: IAttributeData): void; replaceCells(start: number, end: number, fill: ICellData, eraseAttr?: IAttributeData, respectProtect?: boolean): void; - resize(cols: number, fill: ICellData): void; + resize(cols: number, fill: ICellData): boolean; fill(fillCellData: ICellData, respectProtect?: boolean): void; copyFrom(line: IBufferLine): void; clone(): IBufferLine; diff --git a/src/common/buffer/BufferLine.ts b/src/common/buffer/BufferLine.ts index dc7ab51c..f6279f9d 100644 --- a/src/common/buffer/BufferLine.ts +++ b/src/common/buffer/BufferLine.ts @@ -338,10 +338,16 @@ export class BufferLine implements IBufferLine { } } + /** + * Resize BufferLine to `cols` filling excess cells with `fillCellData`. + * The underlying array buffer will not change if there is still enough space + * to hold the new buffer line data. + * Returns a boolean indicating, whether a `cleanBuffer` call would free + * excess memory (after shrinking > CLEANUP_THRESHOLD). + */ public resize(cols: number, fillCellData: ICellData): boolean { - let needsCleanup = false; if (cols === this.length) { - return needsCleanup; + return this._data.length * 4 < this._data.buffer.byteLength * CLEANUP_THRESHOLD; } const fourByteCells = cols * CELL_SIZE; if (cols > this.length) { @@ -362,7 +368,6 @@ export class BufferLine implements IBufferLine { } else { // optimization: just shrink the view on existing buffer this._data = this._data.subarray(0, fourByteCells); - needsCleanup = fourByteCells * 4 < this._data.buffer.byteLength * CLEANUP_THRESHOLD; // Remove any cut off combined data const keys = Object.keys(this._combined); for (let i = 0; i < keys.length; i++) { @@ -381,7 +386,7 @@ export class BufferLine implements IBufferLine { } } this.length = cols; - return needsCleanup; + return fourByteCells * 4 < this._data.buffer.byteLength * CLEANUP_THRESHOLD; } /**