Skip to content

ZJIT: Optimize frozen array subscript #13666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tekknolagi
Copy link
Contributor

@tekknolagi tekknolagi commented Jun 20, 2025

If we have a frozen array [..., a, ...] and a compile-time fixnum index i, we can do the array load at compile-time.

@tekknolagi tekknolagi force-pushed the mb-rewrite-array-subscript branch from a40bbd4 to 02037e2 Compare June 20, 2025 03:55
@tekknolagi tekknolagi marked this pull request as ready for review June 20, 2025 05:18
@matzbot matzbot requested a review from a team June 20, 2025 05:18
@@ -1309,6 +1310,25 @@ impl Function {
}
}

fn try_rewrite_subscript(&mut self, block: BlockId, orig_insn_id: InsnId, self_val: InsnId, idx_val: InsnId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn try_rewrite_subscript(&mut self, block: BlockId, orig_insn_id: InsnId, self_val: InsnId, idx_val: InsnId) {
fn try_rewrite_aref(&mut self, block: BlockId, orig_insn_id: InsnId, self_val: InsnId, idx_val: InsnId) {

#[] is called aref very consistently in the CRuby codebase, so calling it differently could confuse contributors, at least including myself.

bb0(v0:BasicObject):
PatchPoint BOPRedefined(ARRAY_REDEFINED_OP_FLAG, BOP_FREEZE)
PatchPoint BOPRedefined(ARRAY_REDEFINED_OP_FLAG, BOP_AREF)
v11:NilClassExact = Const Value(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that the result is nil? -1 is 6, -2 is 5, so -3 should be 4.

$ ruby -e 'p [4,5,6].freeze[-3]'
4

You picked rb_ary_elt for your implementation, but as the comment above the function says, the behavior is different from Array#[].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thank you

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