Parser refactor and optimization#462
Conversation
f7710a4 to
672dc1a
Compare
49ece2d to
7572dd5
Compare
|
I wrote a little benchmark and this PR is already showing great improvements! The regression below is probably due to overhead in funneling the calls through InputHandler -> Terminal which is a temporary situation. Before: After: On a related note, phantomjs unit tests are fairly easy via |
|
Great, I think that the one-char overhead won't be even noticed, so we don't have to worry about it 👍 . |
|
The overhead also appears to have gone away for the most part which long lines with no escape sequences are ~20% faster. |
|
This PR seems to have taken a great course 👍 . The whole process of input handling is much cleaner right now. @Tyriar do you have any to-dos in your mind that have to be completed before moving on with reviewing an merging this? If yes, can you put them in the description? |
|
@parisk I don't think so, there are some TODOs in the code but they are questions and/or can be deferred. I'll update the main PR comment to reflect current state (ready for review) and what is included. I just did an implementation of |
|
@parisk updated |
parisk
left a comment
There was a problem hiding this comment.
Just some documentation needed.
src/Charsets.ts
Outdated
| @@ -0,0 +1,58 @@ | |||
| // TODO: Give this a proper type | |||
| @@ -0,0 +1,1522 @@ | |||
| import { IInputHandler, ITerminal } from './Interfaces'; | |||
| import { C0 } from './EscapeSequences'; | ||
| import { CHARSETS } from './Charsets'; | ||
|
|
||
| export class InputHandler implements IInputHandler { |
There was a problem hiding this comment.
Can you add JSDoc comment please?
| // TODO: We want to type _terminal when it's pulled into TS | ||
| constructor(private _terminal: any) { } | ||
|
|
||
| public addChar(char: string, code: number): void { |
There was a problem hiding this comment.
Can you add JSDoc comment please?
| * BEL | ||
| * Bell (Ctrl-G). | ||
| */ | ||
| public bell(): void { |
There was a problem hiding this comment.
Can you explain in the JSDoc comment what this method is about (ringing the "bell" of the terminal).
| this._terminal.currentParam = param; | ||
| } | ||
|
|
||
| public getParam(): number { |
There was a problem hiding this comment.
Can you explain a little bit more in the JSDoc comment what this method does and what does it return?
| return this._terminal.currentParam; | ||
| } | ||
|
|
||
| public finalizeParam(): void { |
There was a problem hiding this comment.
Can you explain a little bit more in the JSDoc comment what this method does?
src/Parser.ts
Outdated
| this._terminal.currentParam = 0; | ||
| } | ||
|
|
||
| public setPostfix(postfix: string): void { |
There was a problem hiding this comment.
Can you explain a little bit more in the JSDoc comment what this method does and document arguments as well?
| this._terminal.postfix = postfix; | ||
| } | ||
|
|
||
| public skipNextChar(): void { |
There was a problem hiding this comment.
Can you explain a little bit more in the JSDoc comment what this method does?
| this._position++; | ||
| } | ||
|
|
||
| // public repeatChar(): void { |
There was a problem hiding this comment.
Should we delete these comments?
There was a problem hiding this comment.
I kept this in as it will be needed when we convert more to use maps, comments are removed not from output so it doesn't hurt us at all.
|
Great job @Tyriar! The code is much more readable right now and maintainable. Tests run successfully and everything seems to be working great. The only thing missing is some documentation comments and I think we can move on with merging 🚀 . |
|
@parisk I added jsdoc, I didn't document the individual input handler methods but instead in |
|
Here goes 😅 |
Fixes #459
This PR refactors the parser to improve performance and maintainability. Here are the specifics:
Pull parsing logic from xterm.js into Parser.ts
Use a set of maps when parsing so that the operation is O(1) not O(n), where n is the number of possibilities, this is the main performance gain
Pull all input/code handling into
InputHandlerPull charsets into Charsets.ts
Remove empty functions and commented out code for unimplemented codes, this old code will always be available by looking into git history or at chjj/term.js
By looking at the top of Parser.ts you can now see much more easily what is implemented and what is not, eg.
pis implemented for prefix!only:Parsing performance of regular characters is expected to improve ~20% (much more when parsing CSI codes). These are things I have in mind for what to follow this up with:
IInputHandlerand separation ofTerminalandParser(also fix the escape-sequences-test.js tests we're not passing)TerminalandInputHandlercloser, perhaps using anInputHandlingTerminalwhich extendsTerminaland implementsIInputHandler?InputHandleraddChar, pull wcwidth into own module, etc.Parser's dependence onTerminalso that theTerminalrequests parsing fromParserwhich passes everything along toInputHandlerparams,currentParam, prefixpostfix, etc) intoParser`