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

perf: minimize identifier lookahead when parsing let #13328

Merged
merged 2 commits into from May 18, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented May 17, 2021

Q                       A
License MIT

When reviewing #13325 I realize that we always peek a complete identifier after let in order to compare with in and instanceof. However, we could exit early if the next character is an IdentfierStart and from this position on it does not match in or instanceof.

In a moderate let + length-10 identifier (not starting with i) benchmark cases, I see 17% performance gain. This benchmark is included in this PR.

node --predictable ./10-length-binding.mjs
baseline 64 let and length-10 binding identifiers: 5257 ops/sec ±45.3% (0.19ms)
baseline 128 let and length-10 binding identifiers: 3482 ops/sec ±0.28% (0.287ms)
baseline 256 let and length-10 binding identifiers: 1712 ops/sec ±0.75% (0.584ms)
baseline 512 let and length-10 binding identifiers: 848 ops/sec ±0.34% (1.179ms)
current 64 let and length-10 binding identifiers: 6109 ops/sec ±49.27% (0.164ms)
current 128 let and length-10 binding identifiers: 4018 ops/sec ±0.93% (0.249ms)
current 256 let and length-10 binding identifiers: 2005 ops/sec ±0.36% (0.499ms)
current 512 let and length-10 binding identifiers: 966 ops/sec ±0.89% (1.036ms)

If we go to extreme and benchmark with length-100 identifier, the performance gain is 30%. This is expected since it completely avoid lookahead 100 characters to compare with in or instanceof. The benchmark is not included in this PR.

baseline 64 let and length-100 binding identifiers: 2934 ops/sec ±45.04% (0.341ms)
baseline 128 let and length-100 binding identifiers: 2036 ops/sec ±1.22% (0.491ms)
baseline 256 let and length-100 binding identifiers: 1003 ops/sec ±1.21% (0.997ms)
baseline 512 let and length-100 binding identifiers: 484 ops/sec ±0.95% (2.067ms)
current 64 let and length-100 binding identifiers: 4252 ops/sec ±34.14% (0.235ms)
current 128 let and length-100 binding identifiers: 2636 ops/sec ±1.15% (0.379ms)
current 256 let and length-100 binding identifiers: 1318 ops/sec ±1.4% (0.759ms)
current 512 let and length-100 binding identifiers: 658 ops/sec ±0.54% (1.52ms)

This PR also fixed handling of non-BMP and escaped identifiers after let, the following valid cases are now parsed successfully:

let in\u0061;
let in𝐬𝐭𝐚𝐧𝐜𝐞𝐨𝐟;
let 𝐢𝐧;

@JLHwung JLHwung added pkg: parser PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels May 17, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented May 17, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 17, 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 bcd5dc9:

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

@JLHwung JLHwung added the PR: Spec Compliance 👓 A type of pull request used for our changelog categories label May 18, 2021
@JLHwung JLHwung merged commit 2a5b231 into babel:main May 18, 2021
@JLHwung JLHwung deleted the perf-let-keyword-parsing branch May 18, 2021 12:20
@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 Aug 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 18, 2021
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: Performance 🏃‍♀️ A type of pull request used for our changelog categories PR: Spec Compliance 👓 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants