compromise between code safety and speed

This commit is contained in:
Jörg Breitbart
2018-11-07 00:56:45 +01:00
parent fb64c527a5
commit e9906f9b9e
3 changed files with 21 additions and 10 deletions
+2 -6
View File
@@ -1198,17 +1198,13 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
if (this.buffer.scrollTop === 0) {
// Determine whether the buffer is going to be trimmed after insertion.
const willBufferBeTrimmed = this.buffer.lines.pushWouldTrim();
const willBufferBeTrimmed = this.buffer.lines.isFull();
// Insert the line using the fastest method
if (bottomRow === this.buffer.lines.length - 1) {
if (useRecycling) {
if (willBufferBeTrimmed) {
// push would trim the oldest line in the ringbuffer
// therefore we can recycle it here as the new line
const recycled = this.buffer.lines.get(0);
recycled.copyFrom(newLine);
this.buffer.lines.push(recycled);
this.buffer.lines.recycle().copyFrom(newLine);
} else {
this.buffer.lines.push(newLine.clone());
}
+17 -3
View File
@@ -98,10 +98,24 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
}
/**
* Whether a push would trim.
* True when the ringbuffer is full.
* Advance ringbuffer index and return current element for recycling.
* Note: If the ringbuffer is not full this method will return undefined,
* Either precheck with isFull() or handle the undefined return value accordingly.
*/
public pushWouldTrim(): boolean {
public recycle(): T | undefined {
if (this._length === this._maxLength) {
this._startIndex = ++this._startIndex % this._maxLength;
this.emit('trim', 1);
} else {
this._length++;
}
return this._array[this._getCyclicIndex(this._length - 1)];
}
/**
* Ringbuffer is at max length.
*/
public isFull(): boolean {
return this._length === this._maxLength;
}
+2 -1
View File
@@ -28,7 +28,8 @@ export interface ICircularList<T> extends IEventEmitter {
get(index: number): T | undefined;
set(index: number, value: T): void;
push(value: T): void;
pushWouldTrim(): boolean;
recycle(): T | undefined;
isFull(): boolean;
pop(): T | undefined;
splice(start: number, deleteCount: number, ...items: T[]): void;
trimStart(count: number): void;