Repository navigation
Stride aware Fixnum Arrays - #1087
Draft
AlexaCampusano wants to merge 5 commits into
Draft
AlexaCampusano wants to merge 5 commits into
AlexaCampusano wants to merge 5 commits into
Conversation
AlexaCampusano
commented
Oct 21, 2026
AlexaCampusano
force-pushed
the
nc/add-array-element-stride-all-arrays
branch
from
October 21, 2026 19:05
37586b0 to
3bc9d14
Compare
AlexaCampusano
commented
Oct 21, 2026
AlexaCampusano
force-pushed
the
nc/add-array-element-stride-all-arrays
branch
from
October 21, 2026 21:56
3bc9d14 to
7d5b74b
Compare
AlexaCampusano
force-pushed
the
nc/add-array-element-stride-all-arrays
branch
from
October 28, 2026 18:29
1af7cd0 to
e00398f
Compare
AlexaCampusano
force-pushed
the
nc/add-array-element-stride-all-arrays
branch
2 times, most recently
from
November 3, 2026 15:23
c721298 to
7ba849e
Compare
AlexaCampusano
force-pushed
the
nc/add-array-element-stride-all-arrays
branch
from
November 3, 2026 20:10
7ba849e to
03e3015
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aVALUEon the fly, so nothing aboveArrayhas 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
push,Array.new(n, x),concatinto[],a + b, a literal), that value picks the width:[] << 1is a 1-byte array.nil, a big number) widens the array back toVALUEs, once, and it stays wide.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.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.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:freezekeeps a small embedded array asVALUEs 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_PTRmust return realVALUEs, 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 inlineRARRAY_PTRand would read packed bytes asVALUEs, 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 forArray#[]/[]=and builds literals asVALUEs. 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 pera[i]on a packed array.Numbers
Interpreter, this branch vs. master. Apple M4 Pro, ruby-bench headline+other.
addressable,rack,sequel,sudoku3–9%. No benchmark's array memory grew.include?/index/count−85..−95%,sum−90%,min/max−90..−98%,hash−10%;eachanda[i]within ±7%. Wide arrays unchanged exceptcount/sum(+15–30%, the extra width test on the generic loop).Reads
Not in this PR
sort!sorts a hidden copy that the receiver views while the block runs (the block may mutate the receiver), and the generic comparator handsVALUEs 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.