Navigation Menu

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

Generator performance #13593

Merged
merged 11 commits into from Jul 23, 2021
Merged

Generator performance #13593

merged 11 commits into from Jul 23, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jul 22, 2021

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

This PR improves the @babel/generator performance.

  • The Buffer storage backend is switched to string, it was a micro-optimization in 6 yrs ago. But now the string approach (more intuitive) is faster according to the benchmark
  • The popular path Buffer#_endsWith are rewritten to avoid scanning the _queue
  • String comparison like string[i] === "another one byte string" is replaced by string.charCodeAt(i) === charCodes
  • Access the "isQueries" in @babel/types is extremely slow. In this PR we hoist them to top level. Unfortunately we can not use the import namespace specifiers such as import { isIdentifier } from "@babel/types" because Babel replaces them to types.isIdentifier.

See commit messages for commit-by-commit performance reports.

In a real world case jquery-3.6.txt, we see 50% performance improvements compared to @babel/generator 7.14.5

baseline 1 jquery 3.6: 11.74 ops/sec ±12.2% (85ms)
baseline 2 jquery 3.6: 6.89 ops/sec ±5.4% (145ms)
current 1 jquery 3.6: 19.97 ops/sec ±9.56% (50ms)
current 2 jquery 3.6: 10.27 ops/sec ±4.38% (97ms)
Many empty statements
baseline 256 empty statements: 3_455 ops/sec ±18.71% (0.289ms)
baseline 512 empty statements: 2_029 ops/sec ±1.29% (0.493ms)
baseline 1024 empty statements: 1_006 ops/sec ±1.37% (0.994ms)
baseline 2048 empty statements: 482 ops/sec ±2.01% (2.074ms)
current 256 empty statements: 5_334 ops/sec ±22.11% (0.187ms)
current 512 empty statements: 3_085 ops/sec ±1.73% (0.324ms)
current 1024 empty statements: 1_541 ops/sec ±1.73% (0.649ms)
current 2048 empty statements: 751 ops/sec ±1.95% (1.332ms)
Many 1-length identifiers
baseline 256 1-length identifiers: 1_686 ops/sec ±30.51% (0.593ms)
baseline 512 1-length identifiers: 1_079 ops/sec ±1.38% (0.927ms)
baseline 1024 1-length identifiers: 533 ops/sec ±1.64% (1.876ms)
baseline 2048 1-length identifiers: 267 ops/sec ±1.67% (3.749ms)
current 256 1-length identifiers: 2_788 ops/sec ±25.92% (0.359ms)
current 512 1-length identifiers: 1_587 ops/sec ±1.34% (0.63ms)
current 1024 1-length identifiers: 792 ops/sec ±1.58% (1.262ms)
current 2048 1-length identifiers: 365 ops/sec ±3.4% (2.742ms)
Many 25-length identifiers
baseline 256 25-length identifiers: 1_792 ops/sec ±25.73% (0.558ms)
baseline 512 25-length identifiers: 984 ops/sec ±2.46% (1.016ms)
baseline 1024 25-length identifiers: 520 ops/sec ±2.24% (1.924ms)
baseline 2048 25-length identifiers: 254 ops/sec ±1.91% (3.932ms)
current 256 25-length identifiers: 2_435 ops/sec ±49.59% (0.411ms)
current 512 25-length identifiers: 1_555 ops/sec ±1.78% (0.643ms)
current 1024 25-length identifiers: 751 ops/sec ±1.94% (1.331ms)
current 2048 25-length identifiers: 363 ops/sec ±1.65% (2.753ms)

baseline 256 empty statements: 3_718 ops/sec ±18.65% (0.269ms)
baseline 512 empty statements: 2_070 ops/sec ±0.96% (0.483ms)
baseline 1024 empty statements: 1_012 ops/sec ±1.76% (0.988ms)
baseline 2048 empty statements: 510 ops/sec ±1.53% (1.96ms)
current 256 empty statements: 3_965 ops/sec ±21.14% (0.252ms)
current 512 empty statements: 2_219 ops/sec ±1.29% (0.451ms)
current 1024 empty statements: 1_089 ops/sec ±1.53% (0.918ms)
current 2048 empty statements: 548 ops/sec ±1.87% (1.824ms)
baseline 256 1-length identifiers: 1_947 ops/sec ±25.11% (0.514ms)
baseline 512 1-length identifiers: 1_115 ops/sec ±0.89% (0.897ms)
baseline 1024 1-length identifiers: 537 ops/sec ±1.71% (1.862ms)
baseline 2048 1-length identifiers: 273 ops/sec ±0.57% (3.669ms)
current 256 1-length identifiers: 2_178 ops/sec ±27.17% (0.459ms)
current 512 1-length identifiers: 1_250 ops/sec ±1.19% (0.8ms)
current 1024 1-length identifiers: 622 ops/sec ±0.71% (1.608ms)
current 2048 1-length identifiers: 308 ops/sec ±1.35% (3.251ms)
baseline 256 25-length identifiers: 1_869 ops/sec ±29.4% (0.535ms)
baseline 512 25-length identifiers: 1_092 ops/sec ±1.62% (0.916ms)
baseline 1024 25-length identifiers: 537 ops/sec ±1.29% (1.862ms)
baseline 2048 25-length identifiers: 264 ops/sec ±2% (3.793ms)
current 256 25-length identifiers: 2_462 ops/sec ±23.38% (0.406ms)
current 512 25-length identifiers: 1_401 ops/sec ±0.73% (0.714ms)
current 1024 25-length identifiers: 671 ops/sec ±1.55% (1.491ms)
current 2048 25-length identifiers: 332 ops/sec ±1.44% (3.014ms)
baseline 256 1-length identifiers: 2_678 ops/sec ±24.96% (0.373ms)
baseline 512 1-length identifiers: 1_472 ops/sec ±2.33% (0.68ms)
baseline 1024 1-length identifiers: 737 ops/sec ±1.74% (1.357ms)
baseline 2048 1-length identifiers: 371 ops/sec ±0.79% (2.695ms)
current 256 1-length identifiers: 2_633 ops/sec ±32.44% (0.38ms)
current 512 1-length identifiers: 1_676 ops/sec ±1.49% (0.597ms)
current 1024 1-length identifiers: 803 ops/sec ±1.95% (1.246ms)
current 2048 1-length identifiers: 385 ops/sec ±2.22% (2.596ms)
@JLHwung JLHwung added pkg: generator PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Jul 22, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 22, 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 9321162:

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

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 22, 2021

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

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.

Access the "isQueries" in @babel/types is extremely slow. In this PR we hoist them to top level. Unfortunately we can not use the import namespace specifiers such as import { isIdentifier } from "@babel/types" because Babel replaces them to types.isIdentifier.

We might use a custom plugin to replace import { isIdentifier } from "@babel/types" to import * as _t from "@babel/types"; const { isIdentifier } = _t in every package.

isVariableDeclarator,
isWhileStatement,
isYieldExpression,
} = t;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: It would probably make sense to run this as a babel transformer automatically? That would let us take care of charCodes.foo, too.

Copy link
Member

Choose a reason for hiding this comment

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

The transform-charcodes plugin replaces things like charCodes.lineFeed with 10 so it's even better!

JLHwung and others added 2 commits July 22, 2021 15:53
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@JLHwung JLHwung merged commit 03e8476 into babel:main Jul 23, 2021
@JLHwung JLHwung deleted the generator-perf branch July 23, 2021 03:01
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
* bench: setup benchmarks

* add charcodes

* perf: use string as buffer backend

baseline 256 empty statements: 3_718 ops/sec ±18.65% (0.269ms)
baseline 512 empty statements: 2_070 ops/sec ±0.96% (0.483ms)
baseline 1024 empty statements: 1_012 ops/sec ±1.76% (0.988ms)
baseline 2048 empty statements: 510 ops/sec ±1.53% (1.96ms)
current 256 empty statements: 3_965 ops/sec ±21.14% (0.252ms)
current 512 empty statements: 2_219 ops/sec ±1.29% (0.451ms)
current 1024 empty statements: 1_089 ops/sec ±1.53% (0.918ms)
current 2048 empty statements: 548 ops/sec ±1.87% (1.824ms)

* perf: add endsWithCharAndNewline

baseline 256 1-length identifiers: 1_947 ops/sec ±25.11% (0.514ms)
baseline 512 1-length identifiers: 1_115 ops/sec ±0.89% (0.897ms)
baseline 1024 1-length identifiers: 537 ops/sec ±1.71% (1.862ms)
baseline 2048 1-length identifiers: 273 ops/sec ±0.57% (3.669ms)
current 256 1-length identifiers: 2_178 ops/sec ±27.17% (0.459ms)
current 512 1-length identifiers: 1_250 ops/sec ±1.19% (0.8ms)
current 1024 1-length identifiers: 622 ops/sec ±0.71% (1.608ms)
current 2048 1-length identifiers: 308 ops/sec ±1.35% (3.251ms)

* perf: avoid one byte string compare

* perf: avoid scaning word for /

* perf: hoist babel type methods

baseline 256 25-length identifiers: 1_869 ops/sec ±29.4% (0.535ms)
baseline 512 25-length identifiers: 1_092 ops/sec ±1.62% (0.916ms)
baseline 1024 25-length identifiers: 537 ops/sec ±1.29% (1.862ms)
baseline 2048 25-length identifiers: 264 ops/sec ±2% (3.793ms)
current 256 25-length identifiers: 2_462 ops/sec ±23.38% (0.406ms)
current 512 25-length identifiers: 1_401 ops/sec ±0.73% (0.714ms)
current 1024 25-length identifiers: 671 ops/sec ±1.55% (1.491ms)
current 2048 25-length identifiers: 332 ops/sec ±1.44% (3.014ms)

* perf: hoist parens methods

baseline 256 1-length identifiers: 2_678 ops/sec ±24.96% (0.373ms)
baseline 512 1-length identifiers: 1_472 ops/sec ±2.33% (0.68ms)
baseline 1024 1-length identifiers: 737 ops/sec ±1.74% (1.357ms)
baseline 2048 1-length identifiers: 371 ops/sec ±0.79% (2.695ms)
current 256 1-length identifiers: 2_633 ops/sec ±32.44% (0.38ms)
current 512 1-length identifiers: 1_676 ops/sec ±1.49% (0.597ms)
current 1024 1-length identifiers: 803 ops/sec ±1.95% (1.246ms)
current 2048 1-length identifiers: 385 ops/sec ±2.22% (2.596ms)

* cleanup unused benchcase

* Update packages/babel-generator/src/buffer.ts

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>

* cleanup benchmarks

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@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 Oct 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 22, 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: generator 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.

None yet

4 participants