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 builders of @babel/types #16870

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

liuxingbaoyu
Copy link
Member

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

This improves performance by about 1x.

@babel-bot
Copy link
Collaborator

babel-bot commented Sep 27, 2024

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

@liuxingbaoyu liuxingbaoyu added pkg: types PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Sep 27, 2024
Comment on lines 100 to 105
// For performance
const subKey: any = {
toString() {
return `${key}[${i}]`;
},
};
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain this change? I would expect allocating an object to be bad, since then it needs to be garbage collected.

Copy link
Member

Choose a reason for hiding this comment

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

Would this have the same effect?

if (i < val.length) {
  const subKey = `${key}[${i}]`;
  do {
    callback(node, subKey, v);
    childValidator(node, subKey, v);
  } while (++i < val.length);
}

Copy link
Member Author

@liuxingbaoyu liuxingbaoyu Oct 2, 2024

Choose a reason for hiding this comment

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

The performance gap here seems to come from frequently generating strings and not using them, and subKey is only used when an exception is thrown.
I allocated an object instead of concatenating countless unused strings, so it will be faster.

Would this have the same effect?

subKey should be dynamic along with i. :)

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo Oct 2, 2024

Choose a reason for hiding this comment

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

Oh ok. If we want to do this, we should update key's type to be string | { toString(): string }. Or maybe just make it key: () => string.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried key: () => string, which doesn't work for a static key for a non-array element. I'll try string | { toString(): string }.

liuxingbaoyu and others added 2 commits October 3, 2024 04:43
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Comment on lines +100 to +105
// For performance
const subKey = {
toString() {
return `${key}[${i}]`;
},
};
Copy link
Member

Choose a reason for hiding this comment

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

Can you expand a bit the comment here?

  • concatenating the strings is expensive because we are actually concatenating a string and a number, so V8 cannot just create a "rope string" but has to allocate memory for the string resulting from the number
  • this string is very rarely used, only in error paths, so we can skip the concatenation cost in most cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: types PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants