ELSEIF
Your brief EB
335 stories from 101 feeds 301 clusters Refreshed 3 minutes ago next pull 05:06

LANGUAGES Signal 409

Ruby's small Hash lookup uses linear O(n) search over an array of up to 8 pairs

Illustration only Photo by Bruno Martins on Unsplash

An analysis of Ruby's internal ar_table implementation reveals that Hashes with 8 or fewer entries are stored as flat arrays of key-value pairs with single-byte hash hints, and lookups perform a linear scan rather than a true hash-table probe.

WHY IT MATTERS

Most Ruby Hashes in real applications are small, so the performance characteristics of this linear search path directly affect everyday code. Understanding the ar_table structure and its O(n) lookup behavior helps engineers reason about when Hash access is cheap and when growing past 8 entries triggers a transition to the st_table implementation.

Written by elseif from the cluster below · every claim links back to a source

The three things worth knowing

01

Ruby Hashes with up to 8 entries use an ar_table, a flat array of pairs, rather than a true hash table.

02

Lookups in ar_table perform a linear scan comparing single-byte hash hints, then fall back to Object#eql? on hint matches to handle collisions.

03

The author identifies this O(n) search as a candidate for optimization after studying the ar_find_entry_hint routine in detail.

THE CLUSTER

Same story, 1 feed.

ORDERED BY FIRST SEEN
byroot.github.io via Lobsters Speeding Up (small) Ruby Hashes Open ↗