Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Shopify Engineering 13 min read Deep dive mediumAdept

Diggin’ and Fetchin’ with TruffleRuby

Refactoring a chained Hash#fetch with default values leads to prototyping a new Hash#dig_fetch method in TruffleRuby, combining fetch's KeyError/default semantics with dig's nested traversal. Benchmarks with benchmark-ips showed that both the recursive dig_fetch and TruffleRuby's existing recursive Hash#dig fell off a performance cliff as nesting depth grew, because the compiler cannot inline recursive calls indefinitely. Rewriting dig as an iterative loop in a shared Diggable module lifted nine-level lookups from about 2.5M to about 16M iterations per second, and that fix shipped upstream in TruffleRuby.

Assumes: Ruby Hash methodsrecursion vs iterationbasic JIT inlining concepts

Related posts

  • Shopify’s journey to faster breadth-first GraphQL execution

    Shopify Engineering hardMaster

    Shopify built GraphQL Cardinal, a breadth-first GraphQL execution engine that calls each field resolver once with the full set of objects at that level instead of once per object. Depth-first execution, used by graphql-ruby and graphql-js, multiplies per-field overhead from tracing hooks, authorization and dataloader promises by response breadth. Cardinal builds a typed execution tree, runs a bottom-up lookbehind planning pass inspired by Grafast, and executes generation by generation with flat object/result sets in a queue-driven loop rather than recursion. Large list queries ran up to 15x faster with 90% less memory and saved over 4s at P50. The tradeoffs are that errors are inlined and located by a final depth traversal, and that migrating a monolith from one-in/one-out resolvers requires an incremental bridge.

  • Code Ranges: A Deeper Look at Ruby Strings

    Shopify Engineering hardMaster

    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.

  • How we built a Ruby library that saves 50% in testing time

    Datadog Engineering hardMaster

    Datadog built a Ruby test impact analysis library for its Intelligent Test Runner that maps each test to the source files it executes, so tests unrelated to a commit's changed files can be skipped, cutting testing time roughly in half. Ruby's Coverage module (up to 300% overhead, conflicts with simplecov) and TracePoint (~200–400%) were too slow. A C extension hooking RUBY_EVENT_LINE got overhead to 60–80%, and further gains came from rb_profile_frames to avoid string copies and from caching the rb_sourcefile pointer to skip repeated prefix checks. Code-less classes such as Active Record models emit no line events, so a small allocation profiler on RUBY_INTERNAL_EVENT_NEWOBJ adds the source files of instantiated classes. External services, processes and data files remain untracked and are handled through tracked-file lists and unskippable tests.

  • parking_lot: ffffffffffffffff...

    Fly.io hardMaster

    Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading. The cause was traced to parking_lot RwLock words being set to 0xFFFFFFFFFFFFFFFF. The hunt ran from a known if-let lock-lifetime footgun through closure-scoped lock refactors with timeouts and telemetry, parking_lot's deadlock detector, gdb core-dump inspection, read_recursive experiments and miri. Core dumps showed threads waiting on a lock nobody held, and logging the raw lock word revealed every bit set, including the unused UPGRADEABLE flag. The post also covers Corrosion, Fly.io's CRDT-structured SQLite replicated over SWIM gossip, and the watchdog that bounces stuck proxies to keep deadlocks survivable.

  • After the Refactor: A Path to Faster Rendering with Liquid-C

    Shopify Engineering mediumAdept

    Liquid-C's bytecode VM was refactored to replace its sequential constant pointer with per-instruction indexes into the constants array, so the instruction pointer can jump freely without recomputing constant offsets. This is a prerequisite for compiling Liquid's If and For tags via new GOTO/JMP-style instructions. A hash table now deduplicates constants (e.g. a cart template dropped from 157 to 104 constants), but the change slightly increased memory use and parse time and did not noticeably speed up theme rendering on its own.

The quest for software engineering knowledge.

Look:
Layout: