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

Store token type as number #13768

Merged
merged 14 commits into from Sep 17, 2021
Merged

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Sep 15, 2021

Q                       A
Tests Pass? Yes
License MIT

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 the charCodes.* usage: The babel-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:

token >= tt._do && token <= tt._while;

the ordinality of integers allow us to reduce multiple branch condition to two branch condition*.

Since we export the tokens via tokTypes, an extra compatibility layer is introduced so the parser can work on internal number token types while emitting the object token types when tokens is enabled.

* Technically an in-range call can be further reduced to a single branch condition:
function inRange(n, start, end) {
  return (n - start) >>> 0 <= end - start
}

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.

@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Sep 15, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Sep 15, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48769/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 15, 2021

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:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

packages/babel-parser/src/plugins/jsx/index.js Outdated Show resolved Hide resolved
packages/babel-parser/src/tokenizer/types.js Outdated Show resolved Hide resolved
}

// 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 } = {
Copy link
Member

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?

@JLHwung
Copy link
Contributor Author

JLHwung commented Sep 16, 2021

I mark this PR as babel-8-breaking -> main since this PR removes updateContext in the token types exposed via tokens: true.

@JLHwung JLHwung merged commit d2076a5 into babel:main Sep 17, 2021
@JLHwung JLHwung deleted the store-token-type-as-number branch September 17, 2021 13:36
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 18, 2021
@nicolo-ribaudo nicolo-ribaudo added this to the v8.0.0 milestone Aug 8, 2023
@nicolo-ribaudo nicolo-ribaudo added PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release and removed babel 8 labels Aug 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Internal 🏠 A type of pull request used for our changelog categories PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants