Skip to content

Stride aware Fixnum Arrays - #1087

Draft
AlexaCampusano wants to merge 5 commits into
Shopify:masterfrom
AlexaCampusano:nc/add-array-element-stride-all-arrays
Draft

AlexaCampusano wants to merge 5 commits into
Shopify:masterfrom
AlexaCampusano:nc/add-array-element-stride-all-arrays

Conversation

@AlexaCampusano

@AlexaCampusano AlexaCampusano commented Oct 21, 2026 •

Copy link
Copy Markdown

Related read Storage Strategies for Collectionsin Dynamically Typed Languages

What we're trying to do

Ruby stores every array element as an 8-byte VALUE, even when the element is a small integer that would fit in one byte. This PR lets an array that holds only Integers store them packed: 1, 2 or 4 bytes each, signed or unsigned, with the width recorded in the array's flags. An array of 100,000 byte-sized integers goes from 800 KB to 100 KB. Reading an element decodes it back into a VALUE on the fly, so nothing above Array has to know.

It applies to all arrays: mutable or frozen, inside the object's slot (embedded) or on the heap, owned or shared.

How an array becomes packed

  • At the first write. When the first element is written into an empty array (push, Array.new(n, x), concat into [], a + b, a literal), that value picks the width: [] << 1 is a 1-byte array.
  • It stays packed as it grows. A value that doesn't fit the current width promotes the array (8 → 16 → 32 bits) in place. The first value that isn't a 32-bit Integer (a String, nil, a big number) widens the array back to VALUEs, once, and it stays wide.
  • Embedded arrays use their slot. The 24 bytes inside a small array object hold 3 VALUEs, or 24 one-byte integers. An array stays in its slot until the packed form no longer fits, and only then moves to the heap, still packed. a = []; 24.times { a << it } never allocates a buffer.
  • Copies keep the layout. dup, slices (a[1, 10], first(n), last(n)), replace, reverse, *, shared views and shared roots all carry the width with them. A slice of a packed array is a packed array.
  • Frozen arrays. A frozen packed array never changes representation (other arrays may point into it, other Ractors may read it). C code that asks it for a VALUE* gets a separately built wide copy the array keeps. Because that copy needs a place to live, a frozen array is packed only on the heap: freeze keeps a small embedded array as VALUEs and moves a larger one to the heap packed. Literal templates follow the same rule.

Reading

RARRAY_AREF, rb_ary_entry, each, [] decode in place and never allocate. Operations that scan the whole array (include?, index, count, sum, min, max, ==, hash, sort! without a block) have packed fast paths that work on the integers directly, and they are 10 to 50 times faster on packed arrays.

C extensions

Nothing changes in the API. RARRAY_PTR / RARRAY_CONST_PTR must return real VALUEs, so calling them on a packed array widens it first (or, for a frozen array, returns its wide copy); the extension sees what it always saw and the array simply loses the memory saving. Extensions built against older headers carry the old inline RARRAY_PTR and would read packed bytes as VALUEs, so this is an ABI change for compiled extensions.

JITs

Both JITs already call into C for most array work, and C is stride aware. The few places where generated code reads an array buffer directly now check the width first: YJIT side-exits to the interpreter on a packed array (expandarray, splats, block arg0), ZJIT calls C for Array#[]/[]= and builds literals as VALUEs. Teaching the JITs to decode packed elements inline is the next step and is not in this PR; until then, integer-heavy code under a JIT pays a C call per a[i] on a packed array.

Numbers

Interpreter, this branch vs. master. Apple M4 Pro, ruby-bench headline+other.

  • Memory (all live arrays after GC, incl. hidden literal templates): blurhash −75%, mail −70%, erubi-rails −65%, railsbench −60%, lobsters −53%, chunky-png −30%, rubocop −22%; addressable, rack, sequel, sudoku 3–9%. No benchmark's array memory grew.
  • Wall time: Rails-style apps 4–12% faster in the interpreter (lobsters, mail, railsbench, rubocop, ruby-lsp, shipit); everything else within noise.
  • Reads on packed arrays: include?/index/count −85..−95%, sum −90%, min/max −90..−98%, hash −10%; each and a[i] within ±7%. Wide arrays unchanged except count/sum (+15–30%, the extra width test on the generic loop).
  • Known cost: under YJIT/ZJIT, benchmarks that index packed Integer arrays in tight loops regress (sudoku, nqueens, optcarrot, rubyboy, matmul), 8–40% YJIT, up to 2× ZJIT, until the JITs read packed elements inline. Building small Integer arrays is 5–20% slower in the interpreter (one width check per element).
Memory: master -> patch   (base 053f8da910; patch 03e3015c4a)
     Array/total = ObjectSpace.memsize_of_all after full GC, including hidden literal templates
     (dump_all over every ARRAY); RSS = mean per-iteration after warmup.  Interpreter, harness-gc.

     == Narrow path exercised ==============================================

     blurhash
       1 array x 124,848 elems (pixel buffer, make_shareable)
       Array        1132.7 KiB  ->       278.0 KiB   -75.5%
       total           3.2 MiB  ->         2.4 MiB   -26.0%
       RSS            19.3 MiB  ->        18.5 MiB    -4.2%

     mail
       Ragel parser tables, unfrozen literals
       Array        3238.8 KiB  ->       979.5 KiB   -69.8%
       total           8.6 MiB  ->         6.4 MiB   -25.1%
       RSS            66.6 MiB  ->        65.0 MiB    -2.4%

     erubi-rails
       same mail-gem tables via ActionMailer
       Array        3613.9 KiB  ->      1254.3 KiB   -65.3%
       total          12.0 MiB  ->         9.7 MiB   -19.3%
       RSS           110.1 MiB  ->       111.2 MiB    +1.0%

     railsbench
       same mail-gem tables
       Array        3905.3 KiB  ->      1544.5 KiB   -60.5%
       total          15.4 MiB  ->        13.1 MiB   -15.2%
       RSS           113.2 MiB  ->       112.8 MiB    -0.4%

     lobsters
       same mail-gem tables
       Array        5471.3 KiB  ->      2586.0 KiB   -52.7%
       total          33.7 MiB  ->        30.9 MiB    -8.4%
       RSS           321.5 MiB  ->       358.1 MiB   +11.4%

     chunky-png
       Array         556.3 KiB  ->       387.2 KiB   -30.4%
       total           4.4 MiB  ->         4.2 MiB    -3.9%
       RSS           100.6 MiB  ->        76.4 MiB   -24.1%

     knucleotide-ractor
       Array         245.3 KiB  ->       175.6 KiB   -28.4%
       total           7.7 MiB  ->         5.8 MiB   -24.5%
       RSS           412.2 MiB  ->       310.7 MiB   -24.6%

     rubocop
       Array        2644.7 KiB  ->      2070.2 KiB   -21.7%
       total          13.3 MiB  ->        12.8 MiB    -4.0%
       RSS           107.5 MiB  ->       106.8 MiB    -0.6%

     knucleotide
       Array         194.1 KiB  ->       164.9 KiB   -15.0%
       total           5.1 MiB  ->         4.2 MiB   -16.6%
       RSS            28.2 MiB  ->        26.0 MiB    -8.0%

     lee
       Array         266.3 KiB  ->       227.4 KiB   -14.6%
       total           4.0 MiB  ->         4.0 MiB    -1.0%
       RSS            43.6 MiB  ->        41.6 MiB    -4.5%

     optcarrot
       emulator tables, Array.new(n, 0) / [0] * n, mutable
       Array       30799.3 KiB  ->     28998.8 KiB    -5.8%
       total          31.2 MiB  ->        29.4 MiB    -5.6%
       RSS            60.4 MiB  ->        52.8 MiB   -12.6%

     rack              Array  -8.9%   total  +1.6%   RSS  -0.2%
     sudoku            Array  -7.6%   total  -1.0%   RSS  -1.8%
     liquid-compile    Array  -5.8%   total  +2.2%   RSS  +3.3%
     sequel            Array  -5.6%   total  +1.3%   RSS  -3.8%
     addressable-new   Array  -5.3%   total  -0.4%   RSS  +0.3%
     addressable-setters Array -4.4%  total -13.8%   RSS  -9.3%
     liquid-c          Array  -4.4%   total  +2.0%   RSS  +3.9%
     gvl_release_acquire Array -3.9%  total  +1.6%   RSS  -2.8%
     nbody             Array  -3.9%   total  +0.8%   RSS  -1.1%
     addressable-to-s, -merge, -getters, -parse, -normalize, -join   Array -3.1% .. -3.9%

     == Narrow path not exercised (Array within 3%) ========================

     activerecord, addressable-equality, binarytrees, erubi, etanni, fannkuchredux,
     fluentd, graphql, graphql-native, hexapdf, liquid-il, liquid-render, matmul,
     nqueens, protoboeuf, protoboeuf-encode, psych-load, ruby-json, ruby-lsp, rubyboy,
     rubykon, shipit, splay, symbol-name-ractor, tinygql

     No benchmark's Array memsize grew.  RSS deltas under ~3% are noise; lobsters +11%
     and protoboeuf-encode +8% RSS are on processes over 150 MiB with Array memsize down
     or flat (allocator/GC timing, not retained arrays).

Reads

Reads: master -> patch   (base 053f8da910; patch 03e3015c4a)
     Interpreter, ns per call, best of 3 runs, arrays built by push.
     W8: i%200   W16: i%60_000   W32: i*40_000   VALUE: i*2**40 (past 32 bits, stays wide on both)
     include? miss: an absent value the width can hold (full scan).
     include? miss (out of range): a value the width cannot hold (early exit, no scan).

     == W8 ========================================================================
                                         1,000 elements                    100,000 elements
       include? hit                443 ns  ->     92 ns   -79%      390 ns  ->     85 ns   -78%
       include? miss               1.8 us  ->    275 ns   -85%    180.5 us  ->   22.3 us   -88%
       include? miss (out of range) 1.8 us ->     34 ns   -98%    178.0 us  ->     35 ns  -100%
       index miss                  1.8 us  ->    270 ns   -85%    180.0 us  ->   22.3 us   -88%
       count                       2.1 us  ->    140 ns   -93%    223.4 us  ->   11.1 us   -95%
       sum                         714 ns  ->     71 ns   -90%     67.2 us  ->    4.2 us   -94%
       max                         478 ns  ->     43 ns   -91%     44.9 us  ->    740 ns   -98%
       min                         477 ns  ->     45 ns   -91%     45.0 us  ->    735 ns   -98%
       ==                           35 ns  ->     35 ns    +2%       35 ns  ->     40 ns   +14%
       hash                        2.9 us  ->    2.7 us    -7%    311.6 us  ->  267.7 us   -14%
       uniq                        5.5 us  ->    5.6 us    +3%    538.3 us  ->  515.7 us    -4%
       each                       15.0 us  ->   16.1 us    +8%     1.62 ms  ->   1.58 ms    -2%
       a[i] loop                  16.2 us  ->   17.0 us    +5%     1.66 ms  ->   1.72 ms    +3%
       dup                          69 ns  ->     72 ns    +5%       65 ns  ->     70 ns    +8%
       first(3)                     43 ns  ->     45 ns    +4%       45 ns  ->     45 ns    +0%

     == W16 =======================================================================
       include? hit                1.9 us  ->    270 ns   -86%     88.9 us  ->    9.0 us   -90%
       include? miss               1.9 us  ->    268 ns   -86%    203.0 us  ->   22.3 us   -89%
       include? miss (out of range) 1.8 us ->     32 ns   -98%    201.1 us  ->     35 ns  -100%
       index miss                  2.1 us  ->    269 ns   -87%    205.5 us  ->   22.3 us   -89%
       count                       2.1 us  ->    145 ns   -93%    203.4 us  ->   11.1 us   -95%
       sum                         506 ns  ->     70 ns   -86%     66.7 us  ->    4.2 us   -94%
       max                         478 ns  ->     48 ns   -90%     47.0 us  ->    1.8 us   -96%
       min                         478 ns  ->     50 ns   -89%     45.3 us  ->    1.8 us   -96%
       ==                           37 ns  ->     35 ns    -5%       35 ns  ->     35 ns    +0%
       hash                        3.2 us  ->    2.7 us   -14%    290.5 us  ->  267.4 us    -8%
       uniq                        8.8 us  ->    8.9 us    +1%    847.6 us  ->  844.8 us    +0%
       each                       15.7 us  ->   15.6 us    +0%     1.60 ms  ->   1.63 ms    +2%
       a[i] loop                  15.3 us  ->   16.9 us   +10%     1.73 ms  ->   1.72 ms    -1%
       dup                          68 ns  ->     69 ns    +1%       65 ns  ->     75 ns   +15%
       first(3)                     45 ns  ->     45 ns    +0%       45 ns  ->     45 ns    +0%

     == W32 =======================================================================
       include? hit                2.1 us  ->    269 ns   -87%    223.5 us  ->   22.2 us   -90%
       include? miss               2.1 us  ->    268 ns   -87%    204.7 us  ->   22.3 us   -89%
       include? miss (out of range) 1.9 us ->     34 ns   -98%    203.7 us  ->     35 ns  -100%
       index miss                  2.0 us  ->    271 ns   -86%    205.0 us  ->   22.2 us   -89%
       count                       2.1 us  ->    128 ns   -94%    193.0 us  ->    9.8 us   -95%
       sum                         513 ns  ->     64 ns   -88%     66.6 us  ->    3.3 us   -95%
       max                         477 ns  ->     64 ns   -87%     45.2 us  ->    3.5 us   -92%
       min                         477 ns  ->     64 ns   -86%     45.1 us  ->    3.5 us   -92%
       ==                           36 ns  ->     36 ns    +1%       35 ns  ->     35 ns    +0%
       hash                        3.0 us  ->    2.7 us   -10%    294.9 us  ->  267.5 us    -9%
       uniq                        8.0 us  ->    8.4 us    +5%    945.6 us  ->  972.7 us    +3%
       each                       15.6 us  ->   16.9 us    +9%     1.56 ms  ->   1.64 ms    +6%
       a[i] loop                  16.2 us  ->   16.8 us    +4%     1.71 ms  ->   1.82 ms    +7%
       dup                          66 ns  ->     71 ns    +7%       65 ns  ->     70 ns    +8%
       first(3)                     45 ns  ->     46 ns    +2%       45 ns  ->     45 ns    +0%

     == VALUE (wide on both; control) =============================================
       include? hit                2.3 us  ->    2.1 us    -6%    180.7 us  ->  187.5 us    +4%
       include? miss               2.3 us  ->    2.1 us    -8%    181.6 us  ->  188.6 us    +4%
       include? miss (out of range) 2.3 us ->    2.1 us    -6%    183.3 us  ->  186.1 us    +1%
       index miss                  1.8 us  ->    2.3 us   +24%    177.9 us  ->  184.3 us    +4%
       count                       1.9 us  ->    2.5 us   +33%    181.8 us  ->  207.6 us   +14%
       sum                         545 ns  ->    712 ns   +31%     82.4 us  ->  103.9 us   +26%
       max                         478 ns  ->    479 ns    +0%     45.2 us  ->   45.5 us    +1%
       min                         475 ns  ->    483 ns    +2%     45.1 us  ->   45.5 us    +1%
       ==                           34 ns  ->     34 ns    -1%       35 ns  ->     35 ns    +0%
       hash                        2.9 us  ->    3.2 us    +8%    295.9 us  ->  306.6 us    +4%
       uniq                        6.7 us  ->    7.2 us    +8%    606.8 us  ->  639.2 us    +5%
       each                       15.4 us  ->   15.5 us    +0%     1.59 ms  ->   1.59 ms    +0%
       a[i] loop                  16.6 us  ->   16.6 us    +0%     1.74 ms  ->   1.68 ms    -3%
       dup                          65 ns  ->     70 ns    +7%       70 ns  ->     70 ns    +0%
       first(3)                     44 ns  ->     48 ns   +11%       45 ns  ->     45 ns    +0%

     == small arrays (3 elements, embedded) ====================================
                       W8                 W16                W32                VALUE
       x, y = a        40 -> 44 ns  +9%   41 -> 45 ns +11%   41 -> 44 ns  +6%   42 -> 45 ns  +6%
       f(*a)           53 -> 57 ns  +8%   53 -> 55 ns  +3%   55 -> 58 ns  +6%   54 -> 59 ns  +9%
       a[1]            27 -> 31 ns +13%   30 -> 29 ns  -3%   29 -> 30 ns  +4%   28 -> 30 ns  +6%
       a.include?(2)   35 -> 36 ns  +5%   40 -> 42 ns  +6%   39 -> 40 ns  +4%   37 -> 38 ns  +4%
       a.dup           70 -> 76 ns  +9%   71 -> 74 ns  +5%   69 -> 72 ns  +5%   70 -> 74 ns  +5%
       a.sum           39 -> 43 ns +11%   39 -> 37 ns  -4%   39 -> 42 ns  +7%   39 -> 42 ns  +7%

Not in this PR

  • Teach JITs to inline reads of packed arrays.
  • Sorting a packed array with a block still widens it first. sort! sorts a hidden copy that the receiver views while the block runs (the block may mutate the receiver), and the generic comparator hands VALUEs to Ruby code, so that copy has to be wide. The array is packed again after the sort. sort! without a block sorts the packed buffer directly.

@github-actions github-actions Bot added the jit label Oct 21, 2026
Comment thread array.c Resolved
@AlexaCampusano
AlexaCampusano force-pushed the nc/add-array-element-stride-all-arrays branch from 37586b0 to 3bc9d14 Compare October 21, 2026 19:05
Comment thread array.c Resolved
@AlexaCampusano
AlexaCampusano force-pushed the nc/add-array-element-stride-all-arrays branch from 3bc9d14 to 7d5b74b Compare October 21, 2026 21:56
@AlexaCampusano
AlexaCampusano force-pushed the nc/add-array-element-stride-all-arrays branch from 1af7cd0 to e00398f Compare October 28, 2026 18:29
@AlexaCampusano
AlexaCampusano force-pushed the nc/add-array-element-stride-all-arrays branch 2 times, most recently from c721298 to 7ba849e Compare November 3, 2026 15:23
@AlexaCampusano
AlexaCampusano force-pushed the nc/add-array-element-stride-all-arrays branch from 7ba849e to 03e3015 Compare November 3, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant