Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce garbage generation in InputHandler.print #1817

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class DECRQSS implements IDcsHandler {
*/
export class InputHandler extends Disposable implements IInputHandler {
private _surrogateFirst: string;
private _charCache: { [s: number]: string };

constructor(
protected _terminal: IInputHandlingTerminal,
Expand Down Expand Up @@ -288,6 +289,12 @@ export class InputHandler extends Disposable implements IInputHandler {
*/
this._parser.setDcsHandler('$q', new DECRQSS(this._terminal));
this._parser.setDcsHandler('+q', new RequestTerminfo(this._terminal));

/**
* This cache prevents a new string from being generated every time getChar
* is called in print. Reduces GC pressure when printing lots of data
*/
this._charCache = {};
}

public dispose(): void {
Expand Down Expand Up @@ -339,8 +346,12 @@ export class InputHandler extends Disposable implements IInputHandler {

this._terminal.updateRange(buffer.y);
for (let stringPosition = start; stringPosition < end; ++stringPosition) {
char = data.charAt(stringPosition);
code = data.charCodeAt(stringPosition);
char = this._charCache[code];
if (char === undefined) {
char = data.charAt(stringPosition);
this._charCache[code] = char;
}

// surrogate pair handling
if (0xD800 <= code && code <= 0xDBFF) {
Expand Down