Data Structures (2 blogmarks)

← Blogmarks

Shrinking Ruby Hashes

https://byroot.github.io/ruby/performance/2026/08/05/shrinking-ruby-hashes.html

I had no idea Ruby hashes had this much of a memory footprint (relative to struct).

It’s super impressive to see the small steady improvements the Ruby core team / contributors are able to squeeze out by doing hyper-specific, low-level optimizations.

Reading this post reminded me of some of the Aaron Patterson talks I’ve seen.

I don't have much practical use for this kind of under-the-hood perf optimization analysis in my day-to-day. However, I do find it interesting to see the bits of Ruby code one can use to pull out these kinds of memory footprint numbers -- primarily ObjectSpace.memsize_of.

Here is some of my own basic fiddling:

> require 'objspace'
=> true
> ObjectSpace.memsize_of(1)
=> 0
> ObjectSpace.memsize_of("Hello, World!")
=> 40
> ObjectSpace.memsize_of([1,2,3])
=> 40
> ObjectSpace.memsize_of({[1,2,3] => :abc})
=> 160

And here is a snippet of measuring code from the post:

require 'objspace'
puts "Ruby: #{RUBY_VERSION}"

11.times do |size|
  struct_class = size.zero? ? Object : Struct.new(*size.times.map { |i| :"m_#{i}" })
  struct = ObjectSpace.memsize_of(struct_class.new)
  hash = ObjectSpace.memsize_of(Hash[size.times.map { |i| [i, i] }])
  diff = (hash.to_f / struct).round(1)
  puts "size: #{size} \tstruct: #{struct} \thash: #{hash} \tdiff: #{diff}x"
end

Rob Pike's 5 Rules of Programming

https://www.cs.unc.edu/~stotts/COMP590-059-f24/robsrules.html

The general theme here is "less is more". Don't write anticipatory code for potential bottlenecks, you need to measure and identify them first. Start with simpler algorithms. Start with simpler data structures.

Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.

Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.

Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)

Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.

Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

This last bit of Rule 5 is particularly interesting: "Data structures, not algorithms, are central to programming."

It is so much easier to reason about and build a flow of logic when you've modeled the problem well and gotten the data structure right.