Code Ranges: A Deeper Look at Ruby Strings - Shopify
Apr 14, 202214 min read
Opens shopify.engineeringWhat the post covers
This post provides a deep dive into Ruby's string implementation, specifically focusing on encoding-aware strings and the 'code range' mechanism.
- It explains how the code range is used to optimize string operations by caching information about character validity and boundaries.
- The post details the four code range values (`ENC_CODERANGE_UNKNOWN`, `ENC_CODERANGE_7BIT`, `ENC_CODERANGE_VALID`, `ENC_CODERANGE_BROKEN`), their mapping to Ruby methods, and how they are stored and managed across different Ruby implementations (MRI, JRuby, TruffleRuby).
- It also discusses how string operations can affect the code range and the strategies used to avoid unnecessary recalculations, such as lazy calculation and reasoning about code range preservation during operations.
- The post also touches upon the concept of 'single-byte optimizable' strings and how it relates to code range optimizations.
More from the YJIT Compiler for Ruby trail
Contributing support for a Wasm instruction to Winch - Shopify
Ruby 3.2’s YJIT is Production-Ready - Shopify
Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions - Shopify
Our Experience Porting the YJIT Ruby Compiler to Rust - Shopify
After the Refactor: A Path to Faster Rendering with Liquid-C - Shopify