Skip to content

perf(swc_ts_fast_strip): remove redundant token sorting after parser capture #11656

@kdy1

Description

@kdy1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions