perf: avoid allocating static strings via custom builder methods#10421
Conversation
How to use the Graphite Merge QueueAdd 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. |
There was a problem hiding this comment.
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(..)withExpression::new_identifier(..)for string-literal identifiers to avoid allocating into the arena. - Replaced
BindingIdentifier::new_id(..)/IdentifierName::new_id_name(..)withBindingIdentifier::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. |
Merging this PR will not alter performance
Comparing Footnotes
|
b0f8850 to
45449fc
Compare
ea4ede0 to
66a13fa
Compare
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) ```
66a13fa to
f38313a
Compare
45449fc to
c6cf426
Compare
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 } ```
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.

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:
After: