# Eliminating Intermediate Array Allocations

DevFeed: [Eliminating Intermediate Array Allocations](<https://devfeed.tech/articles/eliminating-intermediate-array-allocations-38999.md>)

Original publisher: [Read original article](<https://tenderlovemaking.com/2024/09/29/eliminating-intermediate-array-allocations/>)

Published: 2024-09-30T02:06:28Z

Content type: tutorial

Language: en

Sources: [Aaron Patterson](<https://devfeed.tech/sources/aaron-patterson.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>), [tagged pointers](<https://devfeed.tech/topics/tagged-pointers.md>), [math](<https://devfeed.tech/topics/math.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [code](<https://devfeed.tech/tags/code.md>), [function](<https://devfeed.tech/tags/function.md>), [gc](<https://devfeed.tech/tags/gc.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [tagged-pointers](<https://devfeed.tech/tags/tagged-pointers.md>)

## AI overview

This Ruby article explains how to measure object allocations with GC.stat and distinguishes code that never, sometimes, or always allocates. It focuses on why an intermediate array used in certain min or max calculations can be allocated on the stack and discarded without requesting a new object from the garbage collector.

## Source excerpt

Recently I gave a talk at RailsWorld (hopefully they'll post the video soon), and part of my presentation was about eliminating allocations in tokenizers. I presented a simple function for measuring allocations: def allocations x = GC.stat(:total_allocated_objects) yield GC.stat(:total_allocated_objects) - x end Everything in Ruby is an object, but not all objects actually make allocations. We can use the above function to measure allocations made in a block. Here are some examples of code that never allocate: p allocations { true } # => 0 p allocations { false } # => 0 p allocations { nil } # => 0 p allocations { :hello } # => 0 p allocations { 1 } # => 0 p allocations { 2.3 } # => 0 p allocations { 0xFFFF_FFFF_FFFF_FFFF } # => 0 Literals like booleans, nil, symbols, integers, and floats are represented internally to CRuby as "tagged pointers" and they don't allocate anything when executed. Here is an example of code that sometimes allocates: # Depends on the size of the number p allocations { 1 + 2 } # => 0 p allocations { 0x3FFF_FFFF_FFFF_FFFF + 1 } # => 1 # Depends on `frozen_string_literal` p allocations { "hello!" } # => 0 or 1 Math on integers generally doesn't allocate anything, but it depends on the integer. When a number gets large enough, CRuby will allocate an object to represent that number. On 64 bit platforms, the largest whole number we can represent without allocating is 0x3FFF_FFFF_FFFF_FFFF. String literals will sometimes allocate, but it depends on the frozen_string_literal setting in your program. Here is an example of code that always allocates: p allocations { [1, 2] } # => 1 p allocations { { a: :b } } # => 1 p allocations { Object.new } # => 1 p allocations { "foo"[0, 1] } # => 1 Hopefully these examples are fairly straightforward. Arrays, hashes, objects, string slices, etc will allocate an object. Eliminating Intermediate Array Allocations At the Shopify after-party at RailsWorld, someone asked me a really great question. Their codebase ha