oxc-project
json-strip-comments
BlogDocsChangelog

perf: optimize state machine for better branch prediction

#94Closed
Comparing
perf/assembly-optimizations
(
f7bd12c
) with
main
(
efc97dc
)
CodSpeed Performance Gauge
+1%
Untouched
4

Benchmarks

Passed

minimal_comments
benches/strip.rs::strip::bench
CodSpeed Performance Gauge
+2%
2.5 µs2.5 µs
no_comments
benches/strip.rs::strip::bench
CodSpeed Performance Gauge
+2%
3.9 µs3.8 µs
tsconfig
benches/strip.rs::strip::bench
CodSpeed Performance Gauge
0%
19.3 µs19.2 µs
large_with_comments
benches/strip.rs::strip::bench
CodSpeed Performance Gauge
0%
58.3 µs58.3 µs

Commits

Click on a commit to change the comparison range
Base
main
efc97dc
+1.04%
perf: optimize state machine for better branch prediction Analyzed assembly output and implemented several optimizations to improve branch prediction and reduce instruction count in hot paths: 1. **Explicit enum discriminants**: Added explicit u8 discriminants (0-6) to State enum for more efficient comparisons 2. **Integer-based state matching**: Changed from enum pattern matching to integer discriminant matching with unreachable_unchecked hint for impossible cases 3. **Fast-path optimization**: Added early-return fast paths in top() and in_string() functions for the most common case (non-special characters) Performance improvements (measured with criterion): - tsconfig: 3.5-4.7% faster (2.219µs → 2.129µs) - no_comments: 4.5-5.4% faster (472ns → 451ns) - minimal_comments: 6.3-8.2% faster (121ns → 112ns) - large_with_comments: 2.5-3.6% faster (11.375µs → 11.109µs) All tests pass. Assembly analysis shows simpler integer comparisons and better instruction cache locality with more predictable branching. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
f7bd12c
27 days ago
by Boshen
© 2025 CodSpeed Technology
Home Terms Privacy Docs