Skip to content

Array experiments#9055

Merged
headius merged 2 commits intojruby:masterfrom
headius:abstract_ruby_array
Feb 24, 2026
Merged

Array experiments#9055
headius merged 2 commits intojruby:masterfrom
headius:abstract_ruby_array

Conversation

@headius
Copy link
Copy Markdown
Member

@headius headius commented Nov 4, 2025

This PR will encompass some experiments with the implementation of Array:

  • Abstract RubyArray supertype to make it easier to support different backend implementations.
  • Concurrency-safe RubyArray backends that can efficiently handle multi-threaded mutation without raising errors.
  • Efficient specialized RubyArray backends for fixed sizes, immutability, or specialized data types (e.g. long[] for a homogeneous Array of Fixnums).

@headius headius force-pushed the abstract_ruby_array branch 6 times, most recently from cf09b04 to e8e1639 Compare November 4, 2025 02:25
@headius
Copy link
Copy Markdown
Member Author

headius commented Nov 4, 2025

The abstract work is proceeding; I have been force-pushing as I find broken ends of my refactoring.

I have tried to preserve as much of the public API of RubyArray as possible, even leaving most of the deprecated methods in place (they are apparently used by many of our extensions!) One area that is impossible to maintain is constructors, since they cannot be callable against an abstract class.

I believe only the following forms are public:

  • (Ruby, int)
  • (Ruby, IRubyObject[], int, int)
  • (Ruby, RubyClass, IRubyObject[])
  • (Ruby, RubyClass, IRubyObject[], int, int, boolean)

These are basically in order of risk, with the sized (Ruby, int) form being the most likely to be used in the wild.

The risk here is small, because these are peculiar forms and we have generally discouraged direct construction of core classes, but if some code out there does use these constructors there's no way to support that usage. The external code would have to be updated to use one of our factory methods.

Other risks largely revolve around any code that expects getClass() to be org.jruby.RubyArray, as I found in a few of our own tests, but this is less likely to be found in the wild.

@headius headius force-pushed the abstract_ruby_array branch 2 times, most recently from 2819431 to d74cb7a Compare November 4, 2025 03:10
@headius
Copy link
Copy Markdown
Member Author

headius commented Nov 4, 2025

I believe I have the abstractification green now with a minimal diff (outside of RubyArray.java). The remaining diffs are largely internal cases where we work directly with the "native" implementation from other core classes, such as to construct an uninitialized array for immediate population. These could be replaced with alternative forms.

The risks are outlined in my comment above, and mostly revolve around the irreplaceable constructor forms.

@headius headius force-pushed the abstract_ruby_array branch 2 times, most recently from 2e5b6a5 to 956b746 Compare November 4, 2025 19:56
RubyArray will be split into RubyArray (abstract) and
RubyArrayNative (concrete) to support alternative implementations.
This commit makes the first stage a move, so that history can be
more easily tracked.
This is a first stab at making RubyArray an abstract class, which
will allow us to represent many different storage strategies as
Array as seen from Ruby code:

* The standard implementation, based on the CRuby logic, uses a
  native Java IRubyObject[] and supports full mutability and CoW
  sharing of that array.
* Two specialized implementations extend from this and provide one-
  and two-element versions of Array without an IRubyObject[] field.
  Making RubyArray itself abstract will make it easier to add more
  specializations.
* There are many useful collections provided by the JDK that could
  be used to implement Array. This change will allow them all to
  appear as the same Ruby type but with custom behaviors for
  specific use cases.
* This will make for an easier path to thread-safe Array, since we
  can have specialized implementations that all look like Array.

This is very much a prototype. I have tried to limit the changes
throughout the codebase by making RubyArray abstract rather than
introducing a new supertype (which would have to propagate through
all RubyArray signatures in the system). The goal is to keep all
RubyArray-consuming code the same as it is today while providing
multiple backend implementations.
@headius headius force-pushed the abstract_ruby_array branch from 956b746 to dfe037a Compare December 10, 2025 07:22
@headius headius changed the base branch from master to 10.1-dev December 10, 2025 07:22
@headius headius added this to the JRuby 10.1.0.0 milestone Feb 3, 2026
@headius headius marked this pull request as ready for review February 3, 2026 18:52
@headius headius changed the base branch from 10.1-dev to master February 24, 2026 22:13
@headius
Copy link
Copy Markdown
Member Author

headius commented Feb 24, 2026

After discussion with @enebo, we're going to go forward with this. JRuby 10.1 will no longer allow direct construction of RubyArray and users will have to move to one of the many factory methods in that class or in org.jruby.api.Create.

Full speed ahead!

@headius headius merged commit 4623fc5 into jruby:master Feb 24, 2026
77 checks passed
@headius headius deleted the abstract_ruby_array branch February 24, 2026 22:16
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