-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
Store token type as number #13768
Store token type as number #13768
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48769/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit d34fb4b:
|
} | ||
|
||
// For performance the token type helpers depend on the following declarations order. | ||
// When adding new token types, please also check if the token helpers needs update. | ||
|
||
export const types: { [name: string]: TokenType } = { |
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 we just export this as tt
, since types
won't be replaced by the plugin?
I mark this PR as |
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
7c9237d
to
6a14732
Compare
6a14732
to
d34fb4b
Compare
Token type is the output of tokenizer, a digested interpretation of the source code. A token type is an object including a few meta infos, e.g. whether a token is an assignment operator (
isAssign
), or the precedence if it is a binary operator (binop
). While accessing info from the token type directly is convenient, it introduces extra object access instructions.In this PR we store the token type as a number. By doing so we can eliminate all the
tt.*
usage at build time, replacing them to a number, which is similar to how we handle thecharCodes.*
usage: Thebabel-plugin-transform-charcodes
replaces them by the actual numeric values.Because the token types are now a continuum of integers, some queries can be simplified to a in-range call if we carefully align the declarations of token types. For example, in this PR,
token.isLoop
is reimplemented as:the ordinality of integers allow us to reduce multiple branch condition to two branch condition*.
Since we export the tokens via
* Technically an in-range call can be further reduced to a single branch condition:tokTypes
, an extra compatibility layer is introduced so the parser can work on internal number token types while emitting the object token types whentokens
is enabled.But I didn't observe actual performance improvements. Since the JavaScript does not have an unsigned u32 type, the compiler can not actually use u32 here.