Repository navigation
RubyArray: fix inserts that grow after a shift; O(1) queue pushes and unshifts - #81
Merged
Merged
Conversation
… unshifts All three in code unchanged since the 2016 import. Bug: ResizeForInsertion, when it had to grow the buffer, copied the elements to the front of the new one but kept the old _start, so after a shift an insert that grows read back the wrong elements: a = [1, 2, 3, 4]; a.shift; a.unshift(7, 8); p a # CRuby: [7, 8, 2, 3, 4] IronRuby: [8, 2, 3, 4, nil] and with more shifts an IndexOutOfRangeException out of Array#last. RubyArray_InsertAfterShift covers it: the cases above, and 60,000 random mixes of adds, inserts at the front and in the middle and removals from the front, against List<object>. It fails on the old code. Speed: - ResizeForAppend slid all elements back to the front of the buffer whenever the end was reached, even with one free slot in front. An array used as a queue (push + shift) has exactly that after each shift, so every push copied the whole array. It now slides only when that frees at least half of it, and otherwise grows as if full. - Inserting at the front, when the buffer has to grow, puts the free space in front of the elements, so a run of unshifts copies once per doubling rather than the whole array each time. - Array#reverse copies the RubyArray in one block and reverses it in place, instead of an IList call per element. Release, net8.0 (CRuby 3.3 in brackets): shift + push, 10k elements, 200 x 1000 407 -> 99-106 ms (31) unshift 20k times 36 -> 1.1 ms (1.1) BFS-style queue, 100k 160 -> 119-130 ms (29) reverse 10k, x200 30 -> 11-12 ms (4) push only 1M unchanged A random op mix (push, <<, shift(n), unshift(*n), insert, pop, delete_at, slice!, concat, []= past the end, reverse(!), compact!, clear), 120,000 ops: identical to CRuby; master raises part way. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPwZf5xqgL3eZiUCWPfG2b
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 3 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. CodeRabbit Verify
|
32 of 36 tasks
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.
This PR fixes a correctness bug and three slow paths in
Src/Ruby/Builtins/RubyArray.csandSrc/Libraries/Extensions/IListOps.cs, all in code unchanged since the 2016 import. A survey of ~80 built-in methods against CRuby pointed at them.Bug: an insert that grows the buffer after a
shiftloses elementsResizeForInsertiongrows the buffer when an insert doesn't fit. It copied the elements to the front of the new buffer but kept the old_start:With more shifts it goes further:
a = (1..20).to_a; a.shift(5); a.insert(3, *Array.new(40, 0))leaves 5nils at the end. A longer op sequence raisedIndexOutOfRangeExceptionfromArray#last.The fix sets
_startto where the elements now begin.New test
RubyArray_InsertAfterShift(IronRuby.Tests):List<object>.It fails on the old code (50 failure lines) and passes with the fix.
Speed
push+shift): when the end of the buffer was reached,ResizeForAppendslid all elements back to the front, even with a single free slot there. A queue has exactly one free slot after eachshift, so everypushcopied the whole array. It now slides only when that frees at least half of the buffer; otherwise it grows as if full.unshift: an insert near the front that has to grow the buffer now puts the free space in front of the elements. A run of unshifts then copies once per doubling, not the whole array on every call.Array#reverse: it copies theRubyArrayin one block and reverses it in place, instead of oneIListcall per element.reverse!already did this.Release build, net8.0, best of 3:
shift+push, 10k elements, 200 × 1000unshift20,000 timesreverse10k, × 200pushonly, 1MTesting (Linux, net8.0, Release)
push,<<,shift(n),unshift(*n),insert,pop,delete_at,slice!,concat,[]=past the end,reverse/reverse!,compact!andclear. The result is identical to CRuby;masterraises part way through.Util/run-tests.sh: PASSED, 0 failure lines.language,core/{integer,array,comparable}give 6,485 examples with 1 failure and 17 errors, the same ones asmaster. These are mspec'sfixnum_max/fixnum_minhelpers ("unknown implementation"), size-limit checks and theArray#filltoo-large-size check.core/{enumerable,hash,kernel,struct,string}give 7,461 examples with 0 failures and 2 errors (the samefixnum_maxhelper).Generated by Claude Code