-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some documentation needed.
@@ -0,0 +1,58 @@ | |||
// TODO: Give this a proper type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add license declaration based on #388?
@@ -0,0 +1,1522 @@ | |||
import { IInputHandler, ITerminal } from './Interfaces'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add license declaration based on #388?
import { C0 } from './EscapeSequences'; | ||
import { CHARSETS } from './Charsets'; | ||
|
||
export class InputHandler implements IInputHandler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add JSDoc comment please?
* BEL | ||
* Bell (Ctrl-G). | ||
*/ | ||
public bell(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain a little bit more in the JSDoc comment what this method does?
this._terminal.currentParam = 0; | ||
} | ||
|
||
public setPostfix(postfix: string): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we delete these comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
InputHandler
Pull 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.
p
is 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:
IInputHandler
and separation ofTerminal
andParser
(also fix the escape-sequences-test.js tests we're not passing)Terminal
andInputHandler
closer, perhaps using anInputHandlingTerminal
which extendsTerminal
and implementsIInputHandler
?InputHandleraddChar
, pull wcwidth into own module, etc.Parser
's dependence onTerminal
so that theTerminal
requests parsing fromParser
which passes everything along toInputHandler
params
,currentParam
, prefix, etc) into
Parser`