-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
perf(swc_ts_fast_strip): remove redundant token sorting after parser capture #11656
Copy link
Copy link
Open
Description
Summary
tokens.sort_by_key(|t| t.span) looks redundant because captured tokens are already kept in source order.
Evidence
- Sorting is done in strip mode:
crates/swc_ts_fast_strip/src/lib.rs:303. - Sorting is also done in transform mode when module checking is enabled:
crates/swc_ts_fast_strip/src/lib.rs:418. Capturing::capturealready preserves monotonic order by removing backtracked tokens before pushing (crates/swc_ecma_parser/src/lexer/capturing.rs:43-58).
Why this is a performance risk
Sorting all tokens adds O(n log n) work per call, even though the vector appears to already satisfy the ordering invariant required by binary search lookups.
Suggested direction
- Remove the unconditional sort.
- Keep a debug-only invariant check (for safety), for example using
windows(2)assertions onspan.loordering. - If any edge case violates ordering, handle it conditionally instead of always sorting.
Benchmark idea
Measure total strip-only runtime and transform runtime (with deprecated module check enabled) before/after removing sort.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels