Skip to content

perf: avoid allocating static strings via custom builder methods#10421

Merged
graphite-app[bot] merged 1 commit into
mainfrom
om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods
Jul 24, 2026
Merged

perf: avoid allocating static strings via custom builder methods#10421
graphite-app[bot] merged 1 commit into
mainfrom
om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Follow-on after #10018, continuation of #10420.

Custom AST builder methods (previously methods of AstFactory) take &strs. They allocate the string into arena, to obtain a &'ast str (allocator lifetime). Where the string is a &'static str, it's unnecessary to allocate into arena to extend the lifetime.

In these cases, use standard AST builder methods provided by Oxc, and skip the allocation.

Before:

BindingIdentifier::new_id(SPAN, "__rolldown_default__", &self.ast_builder)

After:

BindingIdentifier::new(SPAN, "__rolldown_default__", &self.ast_builder)

overlookmotel commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

How to use the Graphite Merge Queue

Add the label graphite: merge-when-ready to this PR to add it to the merge queue.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 continues the AST-construction perf work by avoiding arena allocations when constructing AST nodes with &'static str names. It replaces rolldown’s custom *_id_* builder extension helpers (which copy &str into the arena) with Oxc’s per-type constructors that can accept string literals directly.

Changes:

  • Replaced Expression::new_id_ref_expr(..) with Expression::new_identifier(..) for string-literal identifiers to avoid allocating into the arena.
  • Replaced BindingIdentifier::new_id(..) / IdentifierName::new_id_name(..) with BindingIdentifier::new(..) / IdentifierName::new(..) for string-literal names.
  • Cleaned up now-unneeded extension-trait imports in affected modules.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
crates/rolldown/src/hmr/impl_traverse_for_hmr_ast_finalizer.rs Avoids arena allocation when constructing the runtime registerFactory callee expression from a string literal.
crates/rolldown/src/hmr/hmr_ast_finalizer.rs Avoids arena allocations for several runtime helper references and "__rolldown_default__" identifier construction.
crates/rolldown_plugin_vite_web_worker_post/src/ast_visitor.rs Switches member/identifier constructions to Oxc constructors using string literals; removes unused imports.
crates/rolldown_plugin_vite_build_import_analysis/src/ast_utils.rs Switches identifier/name construction to Oxc constructors using string literals; removes unused imports.

@codspeed-hq

codspeed-hq Bot commented Jul 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods (45449fc) with om/07-23-perf_pass_string_literals_to_ast_builder_methods (ea4ede0)2

Open in CodSpeed

Footnotes

  1. 10 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 om/07-23-perf_pass_string_literals_to_ast_builder_methods (66a13fa) during the generation of this report, so ad8882d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel
overlookmotel force-pushed the om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods branch from b0f8850 to 45449fc Compare July 24, 2026 00:02
@overlookmotel
overlookmotel force-pushed the om/07-23-perf_pass_string_literals_to_ast_builder_methods branch from ea4ede0 to 66a13fa Compare July 24, 2026 00:02

@hyfdev hyfdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

— Review by @hyfdev, assisted by GPT-5

@graphite-app

graphite-app Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Merge activity

)

Follow-on after #10018, continuation of #10420.

Custom AST builder methods (previously methods of `AstFactory`) take `&str`s. They allocate the string into arena, to obtain a `&'ast str` (allocator lifetime). Where the string is a `&'static str`, it's unnecessary to allocate into arena to extend the lifetime.

In these cases, use standard AST builder methods provided by Oxc, and skip the allocation.

Before:

```rust
BindingIdentifier::new_id(SPAN, "__rolldown_default__", &self.ast_builder)
```

After:

```rust
BindingIdentifier::new(SPAN, "__rolldown_default__", &self.ast_builder)
```
@graphite-app
graphite-app Bot force-pushed the om/07-23-perf_pass_string_literals_to_ast_builder_methods branch from 66a13fa to f38313a Compare July 24, 2026 03:44
@graphite-app
graphite-app Bot requested a review from h-a-n-a as a code owner July 24, 2026 03:44
@graphite-app
graphite-app Bot force-pushed the om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods branch from 45449fc to c6cf426 Compare July 24, 2026 03:45
graphite-app Bot pushed a commit that referenced this pull request Jul 24, 2026
Follow-on after #10018, continuation of #10420 and #10421.

When converting `&'static str`s to `Str`s, use `Str::from` instead of `Str::from_str_in`. The difference is that `from_str_in` performs an allocation (in arena), whereas `from` is a zero-cost no-op conversion at runtime. When the `&str` already has a `'static` lifetime, it's unnecessary to extend it by allocating it into arena.

Before:

```rs
TemplateElementValue {
  raw: Str::from_str_in("/@vite/lazy?id=", &self.ast_builder),
  cooked: None,
}
```

After:

```rs
TemplateElementValue { raw: Str::from("/@vite/lazy?id="), cooked: None }
```
graphite-app Bot pushed a commit that referenced this pull request Jul 24, 2026
Follow-on after #10018, continuation of #10420 and #10421.

Where we already have a `Str<'ast>` or `Ident<'ast>`, avoid converting it into a `&str` and then allocating it back into arena again to convert back to `Str` or `Ident`. Just re-use the existing `Str` / `Ident`. This avoids unnecessary arena allocations, and in the case of `Ident`, it avoids re-hashing the string too.
Base automatically changed from om/07-23-perf_pass_string_literals_to_ast_builder_methods to main July 24, 2026 03:55
@graphite-app
graphite-app Bot merged commit c6cf426 into main Jul 24, 2026
32 checks passed
@graphite-app
graphite-app Bot deleted the om/07-23-perf_avoid_allocating_static_strings_via_custom_builder_methods branch July 24, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants