Skip to content

fix(parser): correct capacity for tokens Vec#19967

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

fix(parser): correct capacity for tokens Vec#19967
graphite-app[bot] merged 1 commit intomainfrom
om/03-03-fix_parser_correct_capacity_for_tokens_vec_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 3, 2026

When tokens are enabled, parser reserves capacity upfront for the Vec<Token> so that it never needs to grow. Add 1 to the reserved capacity to account for the final Eof token.

In rare cases of minified files which have no whitespace whatsoever between tokens, the reserved capacity could have been too low, causing expensive growth of the Vec<Token> when the Eof token is pushed to the Vec.

@github-actions github-actions bot added the A-parser Area - Parser label Mar 3, 2026
@github-actions github-actions bot added the C-bug Category - Bug label Mar 3, 2026
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.

@overlookmotel overlookmotel marked this pull request as ready for review March 3, 2026 13:48
Copilot AI review requested due to automatic review settings March 3, 2026 13:48
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 fixes an edge-case under-allocation when the lexer is configured to collect tokens by reserving capacity for the final Eof token as well, preventing a late Vec<Token> growth in tightly packed/minified or empty inputs.

Changes:

  • Reserve source_text.len() + 1 capacity for the collected token vector to account for the terminal Eof token.
  • Update the inline rationale/comment to reflect the corrected bound and explain the minified/empty-file cases.

@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-fix_parser_correct_capacity_for_tokens_vec_ (bc6f33d) with om/03-03-perf_parser_make_pushing_tokens_faster (68eecce)

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.

@overlookmotel overlookmotel self-assigned this Mar 3, 2026
@overlookmotel overlookmotel added A-linter-plugins Area - Linter JS plugins 0-merge Merge with Graphite Merge Queue labels Mar 3, 2026
Copy link
Member Author

overlookmotel commented Mar 3, 2026

Merge activity

When tokens are enabled, parser reserves capacity upfront for the `Vec<Token>` so that it never needs to grow. Add 1 to the reserved capacity to account for the final `Eof` token.

In rare cases of minified files which have no whitespace whatsoever between tokens, the reserved capacity could have been too low, causing expensive growth of the `Vec<Token>` when the `Eof` token is pushed to the `Vec`.
@graphite-app graphite-app bot force-pushed the om/03-03-perf_parser_make_pushing_tokens_faster branch from 68eecce to 8ba61dd Compare March 3, 2026 13:56
@graphite-app graphite-app bot force-pushed the om/03-03-fix_parser_correct_capacity_for_tokens_vec_ branch from bc6f33d to 7502afe Compare March 3, 2026 13:56
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 3, 2026
Base automatically changed from om/03-03-perf_parser_make_pushing_tokens_faster to main March 3, 2026 14:04
@graphite-app graphite-app bot merged commit 7502afe into main Mar 3, 2026
23 checks passed
@graphite-app graphite-app bot deleted the om/03-03-fix_parser_correct_capacity_for_tokens_vec_ branch March 3, 2026 14:05
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

A-linter-plugins Area - Linter JS plugins A-parser Area - Parser C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants