Code Ranges: A Deeper Look at Ruby Strings
Ruby strings are mutable and encoding-aware across 100+ encodings, so runtimes cache a per-string "code range" (UNKNOWN, 7BIT, VALID, BROKEN) to avoid repeated O(n) validity scans. In MRI and JRuby the value occupies two bits in the object header and is mutually exclusive rather than a true bitmask, so combining masks yields confusing bugs. MRI avoids rescans by computing code ranges lazily and by deriving result code ranges from operation semantics (e.g. 7BIT stays 7BIT, single-byte-optimizable strings). TruffleRuby instead computes them eagerly and stores them on rope nodes, amortizing the cost through rope reuse and deriving results without byte scans.
Related posts
- Optimizing Ruby’s Memory Layout: Variable Width Allocation
Shopify Engineering hardMasterRuby 3.2's Variable Width Allocation replaces CRuby's single 40-byte RVALUE slot with five size pools (40, 80, 160, 320 and 640 bytes) so classes, strings, arrays and objects can store their data adjacent to the object instead of in separate malloc'd memory. Heap pages grew from 16 KB to 64 KB, and compaction moves resized objects into the best-fitting pool to restore data locality. Embedded strings now go up to 615 bytes and arrays and objects up to 78 elements or instance variables. Microbenchmarks show up to 35% speedups and railsbench gains 2.1%. The change also adds GC.stat_heap, offers a build flag to disable the feature, and opens the door to MMTk-based garbage collector research.
- Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions
Shopify Engineering easyNoviceRuby's 2022 highlights include YJIT's rewrite in Rust and its new ARM support, so `ruby --yjit` now runs on Apple Silicon development machines as well as production. Variable Width Allocation became the default: Ruby learns when a class needs more instance-variable slots than the fixed 40-byte object provides and allocates later instances from wider slot pools, which improves cache locality. Ruby 3.2's Object Shapes give inline caches a class-independent key based on which instance variables were set and in what order, which helps deep hierarchies like Active Record. On the tooling side, Ruby LSP brings editor features to Ruby, and Error Highlight's column-level error pointers now appear on Rails error pages, part of a broader push for developer ergonomics in 2023.
- To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models
Shopify Engineering mediumAdeptThreaded Ruby servers like Puma and Sidekiq save memory over forking servers, but the gap is smaller than RSS suggests once copy-on-write sharing is measured properly with PSS and /proc/$PID/smaps_rollup. In one Shopify production app an extra Unicorn worker cost about 427MiB versus about 257MiB for an extra thread, not the ~750MiB RSS implied, and preloading plus eager loading improves CoW efficiency. Processes still offer clean request timeouts and avoid Global VM Lock contention, which hurts latency more than commonly assumed because Ruby web requests are less I/O-heavy than believed and GC also holds the GVL. Ractors and Fibers are not drop-in replacements, and a proposed GVL instrumentation API for Ruby 3.2 would let teams measure the latency cost of threads.
- Introducing Ruvy
Shopify Engineering mediumAdeptRuvy is an open-source toolchain built on ruby.wasm that compiles Ruby scripts into WebAssembly modules, pre-initializing the Ruby VM and preloaded Ruby files at build time instead of at startup. Compared with ruby.wasm plus wasi-vfs, Ruvy modules instantiate and run about 20% faster (roughly 44 ms vs 56 ms in Wasmtime) and compile to native code with Cranelift about 70% faster (about 0.45 s vs 1.65 s). Because the script is embedded, Ruvy modules need no WASI arguments at runtime, which suits edge platforms that can't pass them. Ruvy is currently built from source, and contributions are invited, including Shopify Functions compatibility work.
- Don’t stop early: Case-folding source code at memory speed
GitHub Engineering hardMasterGitHub's Blackbird code search case-folds every byte of 480TB of source, and the fastest ASCII path came from removing the early exit at the first non-ASCII byte. A branchless sweep that ORs bytes into an accumulator and lowercases arithmetically lets LLVM vectorize to over 45 GiB/s on an Apple M4, versus 3.1 GiB/s for the naive break loop. A two-pass chunked scan reaches about 23 GiB/s, and branchless code without vectorization is actually slower. The open-source Rust crate casefold also avoids heap allocation by reusing the input buffer. For non-ASCII text, it rejects non-folding characters with a 64-code-point page bitmap read straight from UTF-8 lead bytes, and run-length/stride-encoded fold ranges shrink Unicode's 1484 simple folds to 1776 bytes.