---
title: "Diggin’ and Fetchin’ with TruffleRuby"
description: "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."
url: "https://swe.quest/articles/diggin-and-fetchin-with-truffleruby-52a4362b84ef4768"
canonical: "https://shopify.engineering/dig-fetch-truffleruby"
source: "Shopify Engineering"
published: "2021-08-20"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 13
tags: ["ruby","runtimes","performance","compilers"]
---

# Diggin’ and Fetchin’ with TruffleRuby

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2021-08-20, 13 min read, Deep dive. Original: [https://shopify.engineering/dig-fetch-truffleruby](https://shopify.engineering/dig-fetch-truffleruby)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

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

## Prerequisites

- Ruby Hash methods ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- recursion vs iteration
- basic JIT inlining concepts ([easy posts on Compilers](https://swe.quest/tags/compilers.md?difficulty=easy))

## Keywords

TruffleRuby, GraalVM, Truffle, Hash#dig, Hash#fetch, benchmark-ips, MRI

## Related posts

- [Shopify’s journey to faster breadth-first GraphQL execution](https://swe.quest/articles/shopify-s-journey-to-faster-breadth-first-graphql-execution-83f0f58b8c4dabd1.md): Shopify Engineering · 2026-03-12 · Hard · 14 min read · GraphQL, Performance engineering, Ruby, Query engines
  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.
- [Code Ranges: A Deeper Look at Ruby Strings](https://swe.quest/articles/code-ranges-a-deeper-look-at-ruby-strings-b87e06d5cb44fa84.md): Shopify Engineering · 2022-04-14 · Hard · 14 min read · Ruby, Runtimes & VMs, Data structures, Performance engineering
  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.
- [How we built a Ruby library that saves 50% in testing time](https://swe.quest/articles/how-we-built-a-ruby-library-that-saves-50-in-testing-time-128208a842ec0eb3.md): Datadog Engineering · 2024-11-01 · Hard · 12 min read · Ruby, Testing, Performance engineering, Runtimes & VMs, CI/CD
  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.
- [parking\_lot: ffffffffffffffff...](https://swe.quest/articles/parking-lot-ffffffffffffffff-e7cd36eac4ddf5cc.md): Fly.io · 2025-05-28 · Hard · 16 min read · Concurrency, Rust, Debugging, Incidents & postmortems, Load balancing
  Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading.
- [After the Refactor: A Path to Faster Rendering with Liquid-C](https://swe.quest/articles/after-the-refactor-a-path-to-faster-rendering-with-liquid-c-69e5ad9480eaeead.md): Shopify Engineering · 2022-03-10 · Medium · 4 min read · Runtimes & VMs, Compilers, Ruby, C & C++, Refactoring & migrations
  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.

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)
