Skip to content

Array#<=>, #hash and #flatten: remember where they have been - #424

Merged
tannevaled merged 1 commit into
mainfrom
fix/arrays-that-contain-themselves
Aug 21, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/arrays-that-contain-themselves

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor
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, not 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: 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, in the shape arrayJoin already 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 giving nil, and two equal arrays hashing alike.

Full internal/vm suite green.

Follows the five that had rubyspec-ratchet red (#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

	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
tannevaled force-pushed the fix/arrays-that-contain-themselves branch from a934546 to 24ef413 Compare August 21, 2026 18:01
@tannevaled
tannevaled merged commit f04c7dc into main Aug 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant