Repository navigation
Array#<=>, #hash and #flatten: remember where they have been - #424
Merged
Merged
Conversation
a = []
a << a
#inspect and #== already guard against this; #<=>, #hash and #flatten did
not, and each cost about 1.1 GB and never finished.
core/array/comparison_spec.rb, hash_spec.rb and flatten_spec.rb all build
one through ArraySpecs.empty_recursive_array, which is why all three read
exactly 1.08 GB. They read 78 MB now.
Each takes MRI's answer rather than a convenient one:
#<=> a pair already being compared is equal at that point, so
a <=> a is 0 while a <=> [] is still 1.
#hash a repeat contributes a constant rather than being skipped,
so [a] and [a, a] still hash differently.
#flatten a ring has no flattening, and raises ArgumentError,
"tried to flatten recursive array" — including flatten!.
#<=>'s path lives on the VM rather than in an argument, because that
recursion leaves the builtin and comes back through method dispatch;
#hash and #flatten recurse directly and carry theirs.
Checked against ruby 3, and so is everything ordinary that had to keep
working: depth-limited flatten, an array repeating a sibling (not a ring,
still flattens), the shorter/longer comparison rules, an incomparable
argument, and two equal arrays hashing alike.
tannevaled
force-pushed
the
fix/arrays-that-contain-themselves
branch
from
August 21, 2026 18:01
a934546 to
24ef413
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.
#inspectand#==already guard against this.#<=>,#hashand#flattendid not, and each cost about 1.1 GB and never finished.core/array/comparison_spec.rb,hash_spec.rbandflatten_spec.rball build one throughArraySpecs.empty_recursive_array— which is why all three read exactly 1.08 GB. They read 78 MB now.Each takes MRI's answer, not a convenient one
#<=>a <=> ais0whilea <=> []is still1#hash[a]and[a, a]still hash differently#flattenArgumentError, "tried to flatten recursive array"— includingflatten!#<=>'s path lives on the VM rather than in an argument, because that recursion leaves the builtin and comes back through method dispatch.#hashand#flattenrecurse directly and carry theirs, in the shapearrayJoinalready uses.Checked against ruby 3
Both the new behaviour and everything ordinary that had to keep working: depth-limited
flatten(1), an array merely repeating a sibling ([x, x]— not a ring, still flattens), the shorter/longer comparison rules, an incomparable argument givingnil, and two equal arrays hashing alike.Full
internal/vmsuite green.Follows the five that had
rubyspec-ratchetred (#415, #416, #417, #418, #421). These two were never fatal on a 16 GB runner — they are the last of the headroom.🤖 Generated with Claude Code