perf(swc_ecma_parser): reduce JSX identifier rescan allocations#11671
perf(swc_ecma_parser): reduce JSX identifier rescan allocations#11671
Conversation
|
Binary Sizes
Commit: 98bf428 |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
Performance optimization for JSX identifier rescanning that reduces String allocations by appending into a caller-owned buffer and adding an ASCII fast path for contiguous identifier bytes.
Changes:
- Refactored
scan_identifier_parts→scan_identifier_parts_intoto append into a caller-providedStringbuffer instead of allocating a new one - Replaced
format!/to_string()assembly inscan_jsx_identifierwith directinput_slice_str+push_strto avoid intermediate allocations - Added ASCII run fast path that batches contiguous ASCII identifier bytes into a single slice append
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/swc_ecma_parser/src/lexer/state.rs |
Core refactor: scan_identifier_parts_into, allocation-free prefix building, ASCII fast path |
crates/swc_ecma_parser/tests/jsx/errors/issue-11650/input.js |
New test input with unicode escape in JSX identifier |
crates/swc_ecma_parser/tests/jsx/errors/issue-11650/input.js.swc-stderr |
Expected error output for the new test |
💡 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.
| let mut value = String::new(); | ||
| if token.is_known_ident() || token.is_keyword() { | ||
| let prefix = unsafe { | ||
| // Safety: start and end are valid position because we got them from | ||
| // `self.input` | ||
| self.input_slice_str(start, prefix_end) | ||
| }; | ||
| value.reserve(prefix.len() + v.len()); | ||
| value.push_str(prefix); | ||
| } else if let Some(TokenValue::Word(prefix)) = self.state.token_value.take() { | ||
| value.reserve(prefix.len() + v.len()); | ||
| value.push_str(&prefix); | ||
| } else { | ||
| format!("{token}{v}") | ||
| }; | ||
| self.atom(v) | ||
| let prefix = unsafe { | ||
| // Safety: start and end are valid position because we got them from | ||
| // `self.input` | ||
| self.input_slice_str(start, prefix_end) | ||
| }; | ||
| value.reserve(prefix.len() + v.len()); | ||
| value.push_str(prefix); | ||
| } |
There was a problem hiding this comment.
The is_known_ident() || is_keyword() branch (lines 282-288) and the else fallback branch (lines 293-299) contain identical code for building the prefix via input_slice_str. Consider extracting this into a single code path to reduce duplication — for example, merging them with a single if that handles both cases, falling through to the TokenValue::Word case only when applicable.
PR Review: perf(swc_ecma_parser): reduce JSX identifier rescan allocationsSummaryGood performance optimization that eliminates intermediate
Bug Fix (nice catch)The removal of Code Quality
Minor Observations
PerformanceThe optimization is well-targeted: hot-path JSX identifier scanning no longer allocates intermediate Test Coverage
VerdictLGTM. Clean performance optimization with a nice correctness fix for the |
Summary
format!/to_string()assembly inscan_jsx_identifierscan_identifier_parts_intoStringallocationsTests
git submodule update --init --recursiveUPDATE=1 cargo test -p swc_ecma_parser --features verify --test jsx error -- --ignoredcargo test -p swc_ecma_parser --features verify --test jsx error -- --ignoredcargo test -p swc_ecma_parsercargo fmt --allcargo clippy --all --all-targets -- -D warningsCloses #11650