Skip to content

Commit a562f33

Browse files
committed
remove nonsense exp runtime of rescans
1 parent 309ba47 commit a562f33

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/common/buffer/Buffer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ describe('Buffer', () => {
11981198

11991199
// wait for a bit to give IdleTaskQueue a chance to kick in
12001200
// and finish memory cleaning
1201-
await new Promise(r => setTimeout(r, 100));
1201+
await new Promise(r => setTimeout(r, 30));
12021202

12031203
// cleanup should have realigned memory with exact bytelength
12041204
for (let i = 0; i < INIT_ROWS; i++) {

src/common/buffer/Buffer.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,34 @@ export class Buffer implements IBuffer {
259259
this._memoryCleanupQueue.clear();
260260
// schedule memory cleanup only, if more than 10% of the lines are affected
261261
if (dirtyMemoryLines > 0.1 * this.lines.length) {
262+
this._memoryCleanupPosition = 0;
262263
this._memoryCleanupQueue.enqueue(() => this._batchedMemoryCleanup());
263264
}
264265
}
265266

266267
private _memoryCleanupQueue = new IdleTaskQueue();
268+
private _memoryCleanupPosition = 0;
267269

268270
private _batchedMemoryCleanup(): boolean {
271+
let normalRun = true;
272+
if (this._memoryCleanupPosition >= this.lines.length) {
273+
// cleanup made it once through all lines, thus rescan in loop below to also catch shifted lines,
274+
// which should finish rather quick if there are no more cleanups pending
275+
this._memoryCleanupPosition = 0;
276+
normalRun = false;
277+
}
269278
let counted = 0;
270-
for (let i = 0; i < this.lines.length; i++) {
271-
counted += this.lines.get(i)!.cleanupMemory();
272-
// throttle to 500 lines at once and
273-
// return true to indicate, that the task is not finished yet
274-
if (counted > 500) {
279+
while (this._memoryCleanupPosition < this.lines.length) {
280+
counted += this.lines.get(this._memoryCleanupPosition++)!.cleanupMemory();
281+
// cleanup max 100 lines per batch
282+
if (counted > 100) {
275283
return true;
276284
}
277285
}
278-
return false;
286+
// normal runs always need another rescan afterwards
287+
// if we made it here with normalRun=false, we are in a final run
288+
// and can end the cleanup task for sure
289+
return normalRun;
279290
}
280291

281292
private get _isReflowEnabled(): boolean {

0 commit comments

Comments
 (0)