Skip to content

Reserve extra space before elements when unshifting#9059

Merged
headius merged 1 commit intojruby:masterfrom
headius:unshift_optz
Nov 10, 2025
Merged

Reserve extra space before elements when unshifting#9059
headius merged 1 commit intojruby:masterfrom
headius:unshift_optz

Conversation

@headius
Copy link
Member

@headius headius commented Nov 4, 2025

This change expands the array for an unshift by sliding elements forward, leaving additional space for future unshifts to proceed without copying or reallocating. When expanding, the existing elements are moved to the end of the native array.

This assumes that if one value is unshifted, future values are also likely to be unshifted rather than pushed at the end. This could reduce performance if values are being both unshifted (at begin) and pushed (at end), but the trade-off to have the more common case of all unshifts seems worth it.

Fixes #9058

@headius headius added this to the JRuby 10.0.3.0 milestone Nov 4, 2025
This change expands the array for an unshift by sliding elements
forward, leaving additional space for future unshifts to proceed
without copying or reallocating. When expanding, the existing
elements are moved to the end of the native array.

This assumes that if one value is unshifted, future values are also
likely to be unshifted rather than pushed at the end. This could
reduce performance if values are being both unshifted (at begin)
and pushed (at end), but the trade-off to have the more common case
of all unshifts seems worth it.

Fixes jruby#9058
@headius headius merged commit d1af288 into jruby:master Nov 10, 2025
75 checks passed
@headius headius deleted the unshift_optz branch November 10, 2025 22:51
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.

Implement efficient unshifting

1 participant