Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3241,11 +3241,27 @@ func (vm *VM) bootstrap() {
return object.NewArrayFromSlice(out)
})
vm.cArray.define("<=>", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
a := self.(*object.Array).Elems
sa := self.(*object.Array)
b, ok := args[0].(*object.Array)
if !ok {
return object.NilV
}
// Comparing a pair already being compared means the two descend into
// each other, and MRI calls that equal at this point rather than going
// round again: `a = []; a << a; a <=> a` is 0. The path is on the VM
// because the recursion leaves here and comes back through #<=>
// dispatch, so it cannot be carried in an argument.
pair := [2]*object.Array{sa, b}
if vm.cmpPath[pair] {
return object.IntValue(0)
}
if vm.cmpPath == nil {
vm.cmpPath = map[[2]*object.Array]bool{}
}
vm.cmpPath[pair] = true
defer delete(vm.cmpPath, pair)

a := sa.Elems
be := b.Elems
n := len(a)
if len(be) < n {
Expand Down Expand Up @@ -6278,10 +6294,22 @@ func sign(n int) int {

// flattenDepth flattens nested arrays up to depth levels (-1 = fully).
func flattenDepth(elems []object.Value, depth int) []object.Value {
return flattenDepthIn(elems, depth, map[*object.Array]bool{})
}

// flattenDepthIn is flattenDepth remembering the arrays it is inside. A
// self-referential array has no flattening, and MRI says so rather than trying:
// without this it allocated 1.1 GB and did not finish.
func flattenDepthIn(elems []object.Value, depth int, path map[*object.Array]bool) []object.Value {
var out []object.Value
for _, e := range elems {
if sub, ok := e.(*object.Array); ok && depth != 0 {
out = append(out, flattenDepth(sub.Elems, depth-1)...)
if path[sub] {
raise("ArgumentError", "tried to flatten recursive array")
}
path[sub] = true
out = append(out, flattenDepthIn(sub.Elems, depth-1, path)...)
delete(path, sub)
} else {
out = append(out, e)
}
Expand All @@ -6294,12 +6322,23 @@ func flattenDepth(elems []object.Value, depth int) []object.Value {
// bang form must return nil when nothing changed, which length alone cannot
// detect (e.g. [[1]] flattens to [1] with the same length).
func flattenDepthChanged(elems []object.Value, depth int) ([]object.Value, bool) {
return flattenDepthChangedIn(elems, depth, map[*object.Array]bool{})
}

// flattenDepthChangedIn carries the same cycle path flattenDepthIn does: the
// bang form must refuse a self-referential array for the same reason.
func flattenDepthChangedIn(elems []object.Value, depth int, path map[*object.Array]bool) ([]object.Value, bool) {
var out []object.Value
changed := false
for _, e := range elems {
if sub, ok := e.(*object.Array); ok && depth != 0 {
if path[sub] {
raise("ArgumentError", "tried to flatten recursive array")
}
changed = true
inner, _ := flattenDepthChanged(sub.Elems, depth-1)
path[sub] = true
inner, _ := flattenDepthChangedIn(sub.Elems, depth-1, path)
delete(path, sub)
out = append(out, inner...)
} else {
out = append(out, e)
Expand Down
61 changes: 61 additions & 0 deletions internal/vm/recursive_array_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestAnArrayThatContainsItself covers Array#<=>, Array#hash and Array#flatten
// on a self-referential array.
//
// a = []
// a << a
//
// #inspect and #== already remembered where they had been; these three 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 1.08 GB.
//
// Verified against ruby 3, including that flatten *raises* rather than
// answering.
func TestAnArrayThatContainsItself(t *testing.T) {
cases := []struct{ src, want string }{
// Comparing a pair already being compared is equal at that point.
{`a = []; a << a; p(a <=> a)`, `0`},
{`a = []; a << a; b = []; b << b; p(a <=> b)`, `0`},
{`c = [1, [2]]; c << c; p(c <=> c)`, `0`},
// The spec's own three, which need the shorter/longer rules to survive
// the guard.
{`e = []; e << e; p(e <=> [])`, `1`},
{`e = []; e << e; p([] <=> e)`, `-1`},
// Hashing ends, and is still a hash: a repeat contributes a constant
// rather than being dropped, so these two differ.
{`a = []; a << a; p a.hash.class`, `Integer`},
{`a = []; a << a; b = [a]; c = [a, a]; p(b.hash == c.hash)`, `false`},
// Flattening a ring has no answer, and MRI says so.
{`a = []; a << a
begin; a.flatten; rescue ArgumentError => e; p e.message; end`,
`"tried to flatten recursive array"`},
{`a = []; a << a
begin; a.flatten!; rescue ArgumentError => e; p e.message; end`,
`"tried to flatten recursive array"`},
// An array that merely repeats a sibling is not a ring, and still
// flattens.
{`x = [1]; p [x, x].flatten`, `[1, 1]`},
// None of the ordinary behaviour moves.
{`p([1, [2, [3]]].flatten)`, `[1, 2, 3]`},
{`p([1, [2, [3]]].flatten(1))`, `[1, 2, [3]]`},
{`p([1, 2] <=> [1, 3])`, `-1`},
{`p([1, 2] <=> [1, 2])`, `0`},
{`p([1, 2] <=> [1])`, `1`},
{`p([1, 2] <=> "x")`, `nil`},
{`p([[1]].hash == [[1]].hash)`, `true`},
{`p([1].hash == [2].hash)`, `false`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}
39 changes: 34 additions & 5 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ func (vm *VM) uncaughtBacktrace(e RubyError) []object.Value {

// VM holds I/O, the top-level self, the constant table and the base classes.
type VM struct {
// cmpPath holds the array pairs Array#<=> is in the middle of comparing, so
// two arrays that descend into each other are called equal instead of
// compared for ever. It lives here rather than in an argument because the
// recursion leaves the builtin and returns through method dispatch.
cmpPath map[[2]*object.Array]bool

out io.Writer
errOut io.Writer // $stderr/STDERR sink; defaults to out (no separate stream)
main object.Value
Expand Down Expand Up @@ -664,11 +670,7 @@ func (vm *VM) hashValue(self object.Value) int64 {
// while Complex(1, 2) and Complex(2, 1) differ.
return vm.hashValue(v.Re)*31 + vm.hashValue(v.Im)
case *object.Array:
h := int64(1)
for _, e := range v.Elems {
h = h*31 + vm.hashValue(e)
}
return h
return vm.hashArray(v, nil)
}
// Any other object: a stable hash derived from its identity id.
return vm.refID(self)
Expand All @@ -689,6 +691,33 @@ func (vm *VM) refID(self object.Value) int64 {
return vm.nextObjID
}

// hashArray folds an array's elements, remembering the arrays it is inside so a
// self-referential one ends instead of recursing for ever. `a = []; a << a`
// costs 1.1 GB and never finishes without this; MRI answers an Integer.
//
// A repeat contributes a constant rather than being skipped, so [a] and [a, a]
// still differ.
func (vm *VM) hashArray(a *object.Array, path map[*object.Array]bool) int64 {
if path[a] {
return 9
}
if path == nil {
path = map[*object.Array]bool{}
}
path[a] = true
defer delete(path, a)

h := int64(1)
for _, e := range a.Elems {
if sub, ok := e.(*object.Array); ok {
h = h*31 + vm.hashArray(sub, path)
continue
}
h = h*31 + vm.hashValue(e)
}
return h
}

// fnvHash is a small deterministic 64-bit FNV-1a fold over s, used by hashValue
// for the content-addressed value types.
func fnvHash(s string) int64 {
Expand Down