perf: reduce JSX annotation string churn#8
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes the SWC plugin’s JSX annotation hot path by caching commonly reused AST string/identifier data and reducing repeated allocations while constructing JSX attributes and member-expression names.
Changes:
- Cache JSX attribute identifiers (
IdentName) and source file/path strings (Str) inside the visitor to cut repeated atom creation during annotation. - Share the default ignored-intrinsic-elements set via
OnceLockto avoid per-visitor set construction. - Reduce allocation churn when building JSX member-expression names by precomputing capacity and pushing into a single
String.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/lib.rs |
Cache attribute identifiers and source file/path strings; reserve attr capacity before pushing. |
src/jsx_utils.rs |
Optimize fragment detection and member-expression name building; add helpers that reuse cached IdentName/Str. |
src/constants.rs |
Initialize and reuse the ignored element set via a OnceLock static. |
package.json |
Bump npm package version to 1.13.0. |
Comments suppressed due to low confidence (1)
src/lib.rs:313
- In
transform_styled_call, thedata-elementattribute still usescreate_jsx_attr(...), which rebuilds theIdentNameeach time. Sinceself.element_attr_identis already cached for this purpose, consider switching this call tocreate_jsx_attr_with_ident(&self.element_attr_ident, ...)to avoid extra atom/allocation churn on the styled-component rewrite path as well.
// Add data-element attribute using the styled component variable name
attrs.push(create_jsx_attr(
self.config.element_attr_name(),
&styled_component_name,
));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hstratom creationOnceLockand preallocate member-expression name strings to reduce allocation churnBenchmarks
default-configsource-path-configsource_path_attrenabledBenchmark Setup
HEADand this branchsamplehstratom creation while building JSX attrs and repeated string growth while building member-expression namesValidation
cargo test