Skip to content

Commit cbe59a1

Browse files
authored
Merge pull request #5830 from homanp/perf/array-exe-handlers
perf: array-indexed execute handler lookup in hot path
2 parents 7fe89a1 + 76671ce commit cbe59a1

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/common/parser/EscapeSequenceParser.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
247247
// handler lookup containers
248248
protected _printHandler: PrintHandlerType;
249249
protected _executeHandlers: { [flag: number]: ExecuteHandlerType };
250+
// fast path for EXE bytes < 0x18
251+
protected _executeHandlersArr: (ExecuteHandlerType | undefined)[];
250252
protected _csiHandlers: IHandlerCollection<CsiHandlerType>;
251253
protected _escHandlers: IHandlerCollection<EscHandlerType>;
252254
protected readonly _oscParser: IOscParser;
@@ -290,11 +292,13 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
290292
this._errorHandlerFb = (state: IParsingState): IParsingState => state;
291293
this._printHandler = this._printHandlerFb;
292294
this._executeHandlers = Object.create(null);
295+
this._executeHandlersArr = new Array(0x18).fill(undefined);
293296
this._csiHandlers = Object.create(null);
294297
this._escHandlers = Object.create(null);
295298
this._register(toDisposable(() => {
296299
this._csiHandlers = Object.create(null);
297300
this._executeHandlers = Object.create(null);
301+
this._executeHandlersArr = new Array(0x18).fill(undefined);
298302
this._escHandlers = Object.create(null);
299303
}));
300304
this._oscParser = this._register(new OscParser());
@@ -381,10 +385,14 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
381385
}
382386

383387
public setExecuteHandler(flag: string, handler: ExecuteHandlerType): void {
384-
this._executeHandlers[flag.charCodeAt(0)] = handler;
388+
const code = flag.charCodeAt(0);
389+
this._executeHandlers[code] = handler;
390+
if (code < 0x18) this._executeHandlersArr[code] = handler;
385391
}
386392
public clearExecuteHandler(flag: string): void {
387-
if (this._executeHandlers[flag.charCodeAt(0)]) delete this._executeHandlers[flag.charCodeAt(0)];
393+
const code = flag.charCodeAt(0);
394+
if (this._executeHandlers[code]) delete this._executeHandlers[code];
395+
if (code < 0x18) this._executeHandlersArr[code] = undefined;
388396
}
389397
public setExecuteHandlerFallback(handler: ExecuteFallbackHandlerType): void {
390398
this._executeHandlerFb = handler;
@@ -651,8 +659,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
651659

652660
// EXE fast-path: common control bytes (0x00-0x17) in non-payload states
653661
if (code < 0x18 && this.currentState <= ParserState.CSI_IGNORE) {
654-
if (this._executeHandlers[code]) this._executeHandlers[code]();
655-
else this._executeHandlerFb(code);
662+
(this._executeHandlersArr[code] ?? this._executeHandlerFb)(code);
656663
this.precedingJoinState = 0;
657664
continue;
658665
}

src/common/parser/Types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export type EscFallbackHandlerType = (identifier: number) => void;
108108
/**
109109
* EXECUTE handler types.
110110
*/
111-
export type ExecuteHandlerType = () => boolean;
111+
export type ExecuteHandlerType = (ident?: number) => boolean;
112112
export type ExecuteFallbackHandlerType = (ident: number) => void;
113113

114114
/**

0 commit comments

Comments
 (0)