Skip to content

RubyArray: fix inserts that grow after a shift; O(1) queue pushes and unshifts - #81

Merged
Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp
Nov 3, 2026
Merged

Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp

Conversation

@Largo

@Largo Largo commented Nov 3, 2026

Copy link
Copy Markdown
Owner

This PR fixes a correctness bug and three slow paths in Src/Ruby/Builtins/RubyArray.cs and Src/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 shift loses elements

ResizeForInsertion grows the buffer when an insert doesn't fit. It copied the elements to the front of the new buffer but kept the old _start:

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]

With more shifts it goes further: a = (1..20).to_a; a.shift(5); a.insert(3, *Array.new(40, 0)) leaves 5 nils at the end. A longer op sequence raised IndexOutOfRangeException from Array#last.

The fix sets _start to where the elements now begin.

New test RubyArray_InsertAfterShift (IronRuby.Tests):

  • the cases above;
  • 60,000 random mixes of appends, inserts at the front and in the middle, and removals from the front, compared against List<object>.

It fails on the old code (50 failure lines) and passes with the fix.

Speed

  • Queues (push + shift): when the end of the buffer was reached, ResizeForAppend slid all elements back to the front, even with a single free slot there. A queue has exactly one free slot after each shift, so every push copied 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 the RubyArray in one block and reverses it in place, instead of one IList call per element. reverse! already did this.

Release build, net8.0, best of 3:

before after CRuby 3.3
shift + push, 10k elements, 200 × 1000 407 ms 99–106 ms 31 ms
unshift 20,000 times 36 ms 1.1 ms 1.1 ms
BFS-style queue, 100k 160 ms 119–130 ms 29 ms
reverse 10k, × 200 30 ms 11–12 ms 4 ms
push only, 1M 325 ms 294–349 ms (unchanged path, noise) 59 ms

Testing (Linux, net8.0, Release)

  • Random op mix vs CRuby: 120,000 ops with the same sequence on both sides, compared by a per-op digest and the full contents. The ops were push, <<, shift(n), unshift(*n), insert, pop, delete_at, slice!, concat, []= past the end, reverse/reverse!, compact! and clear. The result is identical to CRuby; master raises part way through.
  • Util/run-tests.sh: PASSED, 0 failure lines.
  • ruby/spec: language, core/{integer,array,comparable} give 6,485 examples with 1 failure and 17 errors, the same ones as master. These are mspec's fixnum_max/fixnum_min helpers ("unknown implementation"), size-limit checks and the Array#fill too-large-size check. core/{enumerable,hash,kernel,struct,string} give 7,461 examples with 0 failures and 2 errors (the same fixnum_max helper).

Generated by Claude Code

… 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
@coderabbitai

coderabbitai Bot commented Nov 3, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c8123ee9-4836-4436-a510-bf3f623dfa92
Commits

Reviewing files that changed from the base of the PR and between a460511 and ca13e09.

Files selected for processing: 4
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/RubyArrayTests.cs
  • Src/Libraries/Extensions/IListOps.cs
  • Src/Ruby/Builtins/RubyArray.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@Largo
Largo merged commit 12015af into master Nov 3, 2026
8 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.

2 participants