Merge pull request #1731 from jerch/reuse_bufferlines
Recycle buffer lines
This commit is contained in:
+2
-5
@@ -94,11 +94,8 @@ export class BufferLine implements IBufferLine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public copyFrom(line: IBufferLine): void {
|
public copyFrom(line: BufferLine): void {
|
||||||
this._data = [];
|
this._data = line._data.slice(0);
|
||||||
for (let i = 0; i < line.length; ++i) {
|
|
||||||
this._push(line.get(i));
|
|
||||||
}
|
|
||||||
this.length = line.length;
|
this.length = line.length;
|
||||||
this.isWrapped = line.isWrapped;
|
this.isWrapped = line.isWrapped;
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-8
@@ -21,11 +21,11 @@
|
|||||||
* http://linux.die.net/man/7/urxvt
|
* http://linux.die.net/man/7/urxvt
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler } from './Types';
|
import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types';
|
||||||
import { IMouseZoneManager } from './ui/Types';
|
import { IMouseZoneManager } from './ui/Types';
|
||||||
import { IRenderer } from './renderer/Types';
|
import { IRenderer } from './renderer/Types';
|
||||||
import { BufferSet } from './BufferSet';
|
import { BufferSet } from './BufferSet';
|
||||||
import { Buffer, MAX_BUFFER_SIZE, DEFAULT_ATTR, NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR } from './Buffer';
|
import { Buffer, MAX_BUFFER_SIZE, DEFAULT_ATTR, NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_ATTR_INDEX } from './Buffer';
|
||||||
import { CompositionHelper } from './CompositionHelper';
|
import { CompositionHelper } from './CompositionHelper';
|
||||||
import { EventEmitter } from './common/EventEmitter';
|
import { EventEmitter } from './common/EventEmitter';
|
||||||
import { Viewport } from './Viewport';
|
import { Viewport } from './Viewport';
|
||||||
@@ -208,6 +208,9 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
private _screenDprMonitor: ScreenDprMonitor;
|
private _screenDprMonitor: ScreenDprMonitor;
|
||||||
private _theme: ITheme;
|
private _theme: ITheme;
|
||||||
|
|
||||||
|
// bufferline to clone/copy from for new blank lines
|
||||||
|
private _blankLine: IBufferLine = null;
|
||||||
|
|
||||||
public cols: number;
|
public cols: number;
|
||||||
public rows: number;
|
public rows: number;
|
||||||
|
|
||||||
@@ -497,6 +500,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
case 'experimentalBufferLineImpl':
|
case 'experimentalBufferLineImpl':
|
||||||
this.buffers.normal.setBufferLineFactory(value);
|
this.buffers.normal.setBufferLineFactory(value);
|
||||||
this.buffers.alt.setBufferLineFactory(value);
|
this.buffers.alt.setBufferLineFactory(value);
|
||||||
|
this._blankLine = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Inform renderer of changes
|
// Inform renderer of changes
|
||||||
@@ -1174,20 +1178,40 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
* Scroll the terminal down 1 row, creating a blank line.
|
* Scroll the terminal down 1 row, creating a blank line.
|
||||||
* @param isWrapped Whether the new line is wrapped from the previous line.
|
* @param isWrapped Whether the new line is wrapped from the previous line.
|
||||||
*/
|
*/
|
||||||
public scroll(isWrapped?: boolean): void {
|
public scroll(isWrapped: boolean = false): void {
|
||||||
const newLine = this.buffer.getBlankLine(this.eraseAttr(), isWrapped);
|
let newLine: IBufferLine;
|
||||||
|
const useRecycling = this.options.experimentalBufferLineImpl === 'TypedArray';
|
||||||
|
if (useRecycling) {
|
||||||
|
newLine = this._blankLine;
|
||||||
|
if (!newLine || newLine.length !== this.cols || newLine.get(0)[CHAR_DATA_ATTR_INDEX] !== this.eraseAttr()) {
|
||||||
|
newLine = this.buffer.getBlankLine(this.eraseAttr(), isWrapped);
|
||||||
|
this._blankLine = newLine;
|
||||||
|
}
|
||||||
|
newLine.isWrapped = isWrapped;
|
||||||
|
} else {
|
||||||
|
newLine = this.buffer.getBlankLine(this.eraseAttr(), isWrapped);
|
||||||
|
}
|
||||||
|
|
||||||
const topRow = this.buffer.ybase + this.buffer.scrollTop;
|
const topRow = this.buffer.ybase + this.buffer.scrollTop;
|
||||||
const bottomRow = this.buffer.ybase + this.buffer.scrollBottom;
|
const bottomRow = this.buffer.ybase + this.buffer.scrollBottom;
|
||||||
|
|
||||||
if (this.buffer.scrollTop === 0) {
|
if (this.buffer.scrollTop === 0) {
|
||||||
// Determine whether the buffer is going to be trimmed after insertion.
|
// Determine whether the buffer is going to be trimmed after insertion.
|
||||||
const willBufferBeTrimmed = this.buffer.lines.length === this.buffer.lines.maxLength;
|
const willBufferBeTrimmed = this.buffer.lines.isFull;
|
||||||
|
|
||||||
// Insert the line using the fastest method
|
// Insert the line using the fastest method
|
||||||
if (bottomRow === this.buffer.lines.length - 1) {
|
if (bottomRow === this.buffer.lines.length - 1) {
|
||||||
this.buffer.lines.push(newLine);
|
if (useRecycling) {
|
||||||
|
if (willBufferBeTrimmed) {
|
||||||
|
this.buffer.lines.recycle().copyFrom(newLine);
|
||||||
|
} else {
|
||||||
|
this.buffer.lines.push(newLine.clone());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.buffer.lines.push(newLine);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.buffer.lines.splice(bottomRow + 1, 0, newLine);
|
this.buffer.lines.splice(bottomRow + 1, 0, (useRecycling) ? newLine.clone() : newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only adjust ybase and ydisp when the buffer is not trimmed
|
// Only adjust ybase and ydisp when the buffer is not trimmed
|
||||||
@@ -1209,7 +1233,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
|
|||||||
// scrollback, instead we can just shift them in-place.
|
// scrollback, instead we can just shift them in-place.
|
||||||
const scrollRegionHeight = bottomRow - topRow + 1/*as it's zero-based*/;
|
const scrollRegionHeight = bottomRow - topRow + 1/*as it's zero-based*/;
|
||||||
this.buffer.lines.shiftElements(topRow + 1, scrollRegionHeight - 1, -1);
|
this.buffer.lines.shiftElements(topRow + 1, scrollRegionHeight - 1, -1);
|
||||||
this.buffer.lines.set(bottomRow, newLine);
|
this.buffer.lines.set(bottomRow, (useRecycling) ? newLine.clone() : newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move the viewport to the bottom of the buffer unless the user is
|
// Move the viewport to the bottom of the buffer unless the user is
|
||||||
|
|||||||
@@ -90,16 +90,34 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
|
|||||||
public push(value: T): void {
|
public push(value: T): void {
|
||||||
this._array[this._getCyclicIndex(this._length)] = value;
|
this._array[this._getCyclicIndex(this._length)] = value;
|
||||||
if (this._length === this._maxLength) {
|
if (this._length === this._maxLength) {
|
||||||
this._startIndex++;
|
this._startIndex = ++this._startIndex % this._maxLength;
|
||||||
if (this._startIndex === this._maxLength) {
|
|
||||||
this._startIndex = 0;
|
|
||||||
}
|
|
||||||
this.emit('trim', 1);
|
this.emit('trim', 1);
|
||||||
} else {
|
} else {
|
||||||
this._length++;
|
this._length++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance ringbuffer index and return current element for recycling.
|
||||||
|
* Note: The buffer must be full for this method to work.
|
||||||
|
* @throws When the buffer is not full.
|
||||||
|
*/
|
||||||
|
public recycle(): T {
|
||||||
|
if (this._length !== this._maxLength) {
|
||||||
|
throw new Error('Can only recycle when the buffer is full');
|
||||||
|
}
|
||||||
|
this._startIndex = ++this._startIndex % this._maxLength;
|
||||||
|
this.emit('trim', 1);
|
||||||
|
return this._array[this._getCyclicIndex(this._length - 1)]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ringbuffer is at max length.
|
||||||
|
*/
|
||||||
|
public get isFull(): boolean {
|
||||||
|
return this._length === this._maxLength;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes and returns the last value on the list.
|
* Removes and returns the last value on the list.
|
||||||
* @return The popped value.
|
* @return The popped value.
|
||||||
@@ -136,10 +154,10 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adjust length as needed
|
// Adjust length as needed
|
||||||
if (this._length + items.length > this.maxLength) {
|
if (this._length + items.length > this._maxLength) {
|
||||||
const countToTrim = (this._length + items.length) - this.maxLength;
|
const countToTrim = (this._length + items.length) - this._maxLength;
|
||||||
this._startIndex += countToTrim;
|
this._startIndex += countToTrim;
|
||||||
this._length = this.maxLength;
|
this._length = this._maxLength;
|
||||||
this.emit('trim', countToTrim);
|
this.emit('trim', countToTrim);
|
||||||
} else {
|
} else {
|
||||||
this._length += items.length;
|
this._length += items.length;
|
||||||
@@ -178,7 +196,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
|
|||||||
const expandListBy = (start + count + offset) - this._length;
|
const expandListBy = (start + count + offset) - this._length;
|
||||||
if (expandListBy > 0) {
|
if (expandListBy > 0) {
|
||||||
this._length += expandListBy;
|
this._length += expandListBy;
|
||||||
while (this._length > this.maxLength) {
|
while (this._length > this._maxLength) {
|
||||||
this._length--;
|
this._length--;
|
||||||
this._startIndex++;
|
this._startIndex++;
|
||||||
this.emit('trim', 1);
|
this.emit('trim', 1);
|
||||||
@@ -198,6 +216,6 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
|
|||||||
* @returns The cyclic index.
|
* @returns The cyclic index.
|
||||||
*/
|
*/
|
||||||
private _getCyclicIndex(index: number): number {
|
private _getCyclicIndex(index: number): number {
|
||||||
return (this._startIndex + index) % this.maxLength;
|
return (this._startIndex + index) % this._maxLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,12 @@ export interface IKeyboardEvent {
|
|||||||
export interface ICircularList<T> extends IEventEmitter {
|
export interface ICircularList<T> extends IEventEmitter {
|
||||||
length: number;
|
length: number;
|
||||||
maxLength: number;
|
maxLength: number;
|
||||||
|
isFull: boolean;
|
||||||
|
|
||||||
get(index: number): T | undefined;
|
get(index: number): T | undefined;
|
||||||
set(index: number, value: T): void;
|
set(index: number, value: T): void;
|
||||||
push(value: T): void;
|
push(value: T): void;
|
||||||
|
recycle(): T | undefined;
|
||||||
pop(): T | undefined;
|
pop(): T | undefined;
|
||||||
splice(start: number, deleteCount: number, ...items: T[]): void;
|
splice(start: number, deleteCount: number, ...items: T[]): void;
|
||||||
trimStart(count: number): void;
|
trimStart(count: number): void;
|
||||||
|
|||||||
Reference in New Issue
Block a user