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: Improve @babel/types builders #15843

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Aug 7, 2023

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Some discussion is needed.
Because it's a breaking change.
Ref: #13868

This PR makes fields and builderKeys only lookup once on initialization.

PS F:\babel\benchmark\babel-types\builders> node .\memberExpression.mjs
baseline memberExpression builder: 804_478 ops/sec ±0.52% (0.001ms)
current memberExpression builder: 7_391_582 ops/sec ±0.26% (0ms)
PS F:\babel\benchmark\babel-types\builders> node .\stringLiteral.mjs
baseline stringLiteral builder: 32_462_576 ops/sec ±0.42% (0ms)
current stringLiteral builder: 81_001_155 ops/sec ±0.6% (0ms)

@babel-bot
Copy link
Collaborator

babel-bot commented Aug 7, 2023

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

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome performance numbers! I am in favor of landing this for Babel 8.

I'm wondering if maybe we can get even better numbers by inlining the validateInternal calls directly in the builder functions, at least for "pupular" nodes (Identifier, BinaryExpression, UnaryExpression, LogicalExpresssion, VariableDeclarator, VariableDeclaration, ...)


for (const key of builderKeys) {
const field = fields[key];
if (field != null) validateInternal(field, node, key, node[key]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the validation for required field still work with this field != null check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work, I just followed here to check ahead.
https://github.com/babel/babel/pull/15843/files#diff-04a0d87b83569ab9a9d94ee801346cf5443ecf057bf35f4836d4a259e882c8fcR39
But thanks to you, I found another problem, validateChild was accidentally skipped. :)

@liuxingbaoyu
Copy link
Member Author

liuxingbaoyu commented Aug 8, 2023

Yes, performance gets better when we inline, but I'm not sure if that's the way to go since the size would increase a lot.
Maybe we can dynamically generate fast versions of them when new Function()/eval is available?
But I'm not sure if doing so under ESM would be difficult.

current

PS F:\babel\benchmark\babel-types\builders> node .\memberExpression.mjs
baseline memberExpression builder: 788_321 ops/sec ±0.65% (0.001ms)
current memberExpression builder: 7_414_300 ops/sec ±0.47% (0ms)

image

PS F:\babel\benchmark\babel-types\builders> node .\memberExpression.mjs
baseline memberExpression builder: 807_823 ops/sec ±0.46% (0.001ms)
current memberExpression builder: 12_744_969 ops/sec ±0.31% (0ms)

image

PS F:\babel\benchmark\babel-types\builders> node .\memberExpression.mjs
baseline memberExpression builder: 813_116 ops/sec ±0.43% (0.001ms)
current memberExpression builder: 10_822_404 ops/sec ±0.35% (0ms)

@liuxingbaoyu liuxingbaoyu added PR: Performance (next major) 🏃‍♀️ A type of pull request used for our changelog categories for next major release and removed i: discussion labels Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: types PR: Performance (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

3 participants