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::capture already 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 on span.lo ordering.
- 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.
Summary
tokens.sort_by_key(|t| t.span)looks redundant because captured tokens are already kept in source order.Evidence
crates/swc_ts_fast_strip/src/lib.rs:303.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
windows(2)assertions onspan.loordering.Benchmark idea
Measure total strip-only runtime and transform runtime (with deprecated module check enabled) before/after removing sort.