---
title: "Code Ranges: A Deeper Look at Ruby Strings"
description: "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."
url: "https://swe.quest/articles/code-ranges-a-deeper-look-at-ruby-strings-b87e06d5cb44fa84"
canonical: "https://shopify.engineering/code-ranges-ruby-strings"
source: "Shopify Engineering"
published: "2022-04-14"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 14
tags: ["ruby","runtimes","data-structures","performance"]
---

# Code Ranges: A Deeper Look at Ruby Strings

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-04-14, 14 min read, Deep dive. Original: [https://shopify.engineering/code-ranges-ruby-strings](https://shopify.engineering/code-ranges-ruby-strings)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [Data structures](https://swe.quest/tags/data-structures.md) (Architecture & design)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- Ruby String API ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- character encodings and UTF-8
- C extensions or VM internals ([easy posts on Virtualization](https://swe.quest/tags/virtualization.md?difficulty=easy))

## Keywords

MRI, JRuby, TruffleRuby, code range, UTF-8, ropes, GraalVM

## Related posts

- [Optimizing Ruby’s Memory Layout: Variable Width Allocation](https://swe.quest/articles/optimizing-ruby-s-memory-layout-variable-width-allocation-7db49cee151f4d44.md): Shopify Engineering · 2022-12-25 · Hard · 13 min read · Memory management, Ruby, Runtimes & VMs, Performance engineering
  Ruby 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.
- [Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions](https://swe.quest/articles/year-in-review-2022-tenderlove-s-ruby-and-rails-reflections-and-predictions-d873d71ac3e91545.md): Shopify Engineering · 2022-12-22 · Easy · 8 min read · Ruby, Runtimes & VMs, Memory management, Developer tools, Compilers
  Ruby'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.
- [To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models](https://swe.quest/articles/to-thread-or-not-to-thread-an-in-depth-look-at-ruby-s-execution-models-18ca691643d5cc44.md): Shopify Engineering · 2022-05-31 · Medium · 15 min read · Ruby, Concurrency, Memory management, Performance engineering, Runtimes & VMs
  Threaded 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.
- [Introducing Ruvy](https://swe.quest/articles/introducing-ruvy-a780cb3b4891bef0.md): Shopify Engineering · 2023-10-18 · Medium · 5 min read · WebAssembly, Ruby, Performance engineering, Open source
  Ruvy 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.
- [Don’t stop early: Case-folding source code at memory speed](https://swe.quest/articles/don-t-stop-early-case-folding-source-code-at-memory-speed-d9a0d3aa3df1f3f4.md): GitHub Engineering · 2026-07-31 · Hard · 18 min read · Performance engineering, Rust, Search, Algorithms, Data structures
  GitHub'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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
