Skip to content

perf: reduce JSX annotation string churn#8

Merged
scttcper merged 3 commits into
mainfrom
scttcper/trace-driven-perf
Mar 15, 2026
Merged

perf: reduce JSX annotation string churn#8
scttcper merged 3 commits into
mainfrom
scttcper/trace-driven-perf

Conversation

@scttcper
Copy link
Copy Markdown
Owner

@scttcper scttcper commented Mar 15, 2026

Summary

  • use trace-driven optimizations in the JSX annotation hot path
  • cache static JSX attribute identifiers and source-file/source-path string values to cut repeated hstr atom creation
  • share the intrinsic element set through OnceLock and preallocate member-expression name strings to reduce allocation churn

Benchmarks

Scenario Workload Before (median) After (median) Speedup Improvement
default-config 800 generated React components, 9 samples, 40 iterations/sample 2.604 ms 2.128 ms 1.22x 18.3% faster
source-path-config Same workload with source_path_attr enabled 3.092 ms 2.367 ms 1.31x 23.4% faster

Benchmark Setup

  • Host: Apple Silicon macOS 26.3.1
  • Mode: release binaries
  • Method: isolated before/after benchmark harnesses run against HEAD and this branch
  • Profiler used to identify hot spots: macOS sample
  • Hot spots addressed: repeated hstr atom creation while building JSX attrs and repeated string growth while building member-expression names

Validation

  • cargo test

Copy link
Copy Markdown

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 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 OnceLock to 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, the data-element attribute still uses create_jsx_attr(...), which rebuilds the IdentName each time. Since self.element_attr_ident is already cached for this purpose, consider switching this call to create_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.

@scttcper scttcper merged commit 32c058d into main Mar 15, 2026
2 checks passed
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.

2 participants