Skip to content

feat(allocator): add Vec::push_fast method#19959

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/03-03-feat_allocator_add_vec_push_fast_method
Mar 3, 2026
Merged

feat(allocator): add Vec::push_fast method#19959
graphite-app[bot] merged 1 commit intomainfrom
om/03-03-feat_allocator_add_vec_push_fast_method

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 3, 2026

Vec::push considers the "the Vec needs to grow" case as fairly likely. Benchmarking showed this is the best strategy for the parser.

Add a method Vec::push_fast which is optimized for the opposite scenario - the Vec is highly likely to have sufficient capacity to push the element without growing.

It makes the "needs to grow" path #[cold] and #[inline(never)] so Vec::push_fast can be inlined into call sites with minimum of instructions.

Vec::push_fast is the best choice where either:

  1. The Vec is large and grows infrequently.
  2. Sufficient capacity is allocated when initializing the Vec, so it will never need to grow.

Vec::push is a better choice for:

  1. Small Vecs (which will grow on every push).
  2. Vecs which start with zero capacity, so first push will always cause growth.

Copy link
Member Author

overlookmotel commented Mar 3, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Mar 3, 2026
@overlookmotel overlookmotel force-pushed the om/03-03-feat_allocator_add_vec_push_fast_method branch from e31e409 to 5a987e9 Compare March 3, 2026 11:52
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 3, 2026

Merging this PR will not alter performance

✅ 53 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing om/03-03-feat_allocator_add_vec_push_fast_method (5a987e9) with main (160e423)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (c92422b) during the generation of this report, so 160e423 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review March 3, 2026 12:01
Copilot AI review requested due to automatic review settings March 3, 2026 12:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a performance-focused alternative to Vec::push in oxc_allocator’s bump-allocated Vec, optimizing for the common case where the vector already has spare capacity (as expected in parser hot paths).

Changes:

  • Added Vec::push_fast, which keeps the “grow” path out-of-line via a #[cold] + #[inline(never)] slow function.
  • Updated Vec::push rustdoc to cross-reference the new push_fast API.
  • Added rustdoc for push_fast, including usage guidance and an example.

@overlookmotel overlookmotel self-assigned this Mar 3, 2026
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 3, 2026
@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 3, 2026

Merge activity

`Vec::push` considers the "the `Vec` needs to grow" case as fairly likely. Benchmarking showed this is the best strategy for the parser.

Add a method `Vec::push_fast` which is optimized for the opposite scenario - the `Vec` is highly likely to have sufficient capacity to push the element without growing.

It makes the "needs to grow" path `#[cold]` and `#[inline(never)]` so `Vec::push_fast` can be inlined into call sites with minimum of instructions.

`Vec::push_fast` is the best choice where either:

1. The `Vec` is large and grows infrequently.
2. Sufficient capacity is allocated when initializing the `Vec`, so it will *never* need to grow.

`Vec::push` is a better choice for:

1. Small `Vec`s (which will grow on every push).
2. `Vec`s which start with zero capacity, so first push will always cause growth.
@graphite-app graphite-app bot force-pushed the om/03-03-feat_allocator_add_vec_push_fast_method branch from 5a987e9 to f83be30 Compare March 3, 2026 13:55
graphite-app bot pushed a commit that referenced this pull request Mar 3, 2026
Use `Vec::push_fast` method (introduced in #19959) for pushing to the tokens `Vec`. We allocate sufficient capacity upfront in the `Vec`, so it should never need to grow.

`Vec::push_fast` makes the "need to grow" path cold and `#[inline(never)]`, which should reduce the size of `Lexer::finish_next`, and help the branch predictor make accurate predictions.

+1% on benchmark for parsing with tokens in CodSpeed. *Possible* it'll have more impact in real world as CodSpeed doesn't take branch predictor into account in its simulations.
@graphite-app graphite-app bot merged commit f83be30 into main Mar 3, 2026
22 checks passed
@graphite-app graphite-app bot deleted the om/03-03-feat_allocator_add_vec_push_fast_method branch March 3, 2026 14:02
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 3, 2026
camc314 pushed a commit that referenced this pull request Mar 9, 2026
### 🚀 Features

- e8547cc parser: Report error for using declarations in ambient
contexts (#19934) (camc314)
- 8345318 allocator: Add methods for boxed slices `ArenaBox<[T]>`
(#19968) (overlookmotel)
- f83be30 allocator: Add `Vec::push_fast` method (#19959)
(overlookmotel)

### 🐛 Bug Fixes

- 291d867 transformer_plugins: Unwrap ChainExpression after define
replacement removes optional markers (#20058) (IWANABETHATGUY)
- 36b2e56 codegen: Print type for TSImportEqualsDeclaration (#20128)
(camc314)
- 5a246ec codegen: Print type arguments for JSXOpeningElement (#20127)
(camc314)
- a40870e codegen: Preserve parens for TSNonNullExpression (#20125)
(camc314)
- ae830b2 codegen: Print `declare` for `TSInterfaceDeclaration` (#20124)
(camc314)
- 92cfb14 linter/plugins: Fix types for `walkProgram` and
`walkProgramWithCfg` (#20081) (overlookmotel)
- ee0491e apps,napi: Explicitly specify libs in tsconfigs (#20071)
(camc314)
- 588009e codegen: Print `static` keyword for TSIndexSignature (#19755)
(Dunqing)
- 5a8799c codegen: Print `with_clause` for `ExportNamedDeclaration`
(#20002) (Dunqing)
- 7502afe parser: Correct capacity for tokens `Vec` (#19967)
(overlookmotel)

### ⚡ Performance

- 4ea8f9a napi: Remove `napi_build::setup()` from `oxc_napi` to avoid
redundant rebuilds (#20094) (Boshen)
- 2baa5fb napi: Unify build-test profile to coverage for cache sharing
(#20090) (Boshen)
- 8ba61dd parser: Make pushing tokens faster (#19960) (overlookmotel)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants