TruffleRuby Regexps: 200x Faster Than C and SIMD

2025-03-18
TruffleRuby Regexps: 200x Faster Than C and SIMD

This blog post explores performance optimization for JSON string escaping in Ruby. Benchmarks compare three approaches: a pure Ruby version, a C extension with SIMD instructions, and a pure Ruby version on TruffleRuby. Surprisingly, TruffleRuby's pure Ruby version, leveraging its advanced JIT compiler and TRegex engine, is 20 times faster than the C extension and SIMD, and over 200 times faster than the baseline C code in some cases. This stems from TruffleRuby's TRegex engine, which compiles regexps into deterministic finite automata, avoiding backtracking and utilizing SIMD instructions for optimization. Similar comparisons are shown for `Time.new(String)` and `StringScanner#scan_integer`, where TruffleRuby's regexp implementations significantly outperform CRuby's C implementations. This demonstrates that in some cases, concise pure Ruby code, combined with an advanced JIT compiler, can surpass the performance of lower-level languages.

Read more
Development Regexps