Skip to content

ZJIT: Add fast-paths for Array#length and Array#size #1

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions zjit/src/cruby_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub fn init() -> Annotations {
annotate!(rb_cString, "bytesize", types::Fixnum, no_gc, leaf);
annotate!(rb_cModule, "name", types::StringExact.union(types::NilClassExact), no_gc, leaf, elidable);
annotate!(rb_cModule, "===", types::BoolExact, no_gc, leaf);
annotate!(rb_cArray, "length", types::Fixnum, no_gc, leaf, elidable);
annotate!(rb_cArray, "size", types::Fixnum, no_gc, leaf, elidable);

Annotations {
cfuncs: std::mem::take(cfuncs)
Expand Down
44 changes: 40 additions & 4 deletions zjit/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4366,6 +4366,40 @@ mod opt_tests {
"#]]);
}

#[test]
fn eliminate_array_length() {
eval("
def test
x = [].length
5
end
");
assert_optimized_method_hir("test", expect![[r#"
fn test:
bb0():
PatchPoint MethodRedefined(Array@0x1000, length@0x1008)
v6:Fixnum[5] = Const Value(5)
Return v6
"#]]);
}

#[test]
fn eliminate_array_size() {
eval("
def test
x = [].length
5
end
");
assert_optimized_method_hir("test", expect![[r#"
fn test:
bb0():
PatchPoint MethodRedefined(Array@0x1000, length@0x1008)
v6:Fixnum[5] = Const Value(5)
Return v6
"#]]);
}

#[test]
fn kernel_itself_argc_mismatch() {
eval("
Expand Down Expand Up @@ -4563,8 +4597,9 @@ mod opt_tests {
fn test:
bb0(v0:BasicObject, v1:BasicObject):
v4:ArrayExact = NewArray v0, v1
v6:BasicObject = SendWithoutBlock v4, :length
Return v6
PatchPoint MethodRedefined(Array@0x1000, length@0x1008)
v9:Fixnum = CCall length@0x1010, v4
Return v9
"#]]);
}

Expand All @@ -4577,8 +4612,9 @@ mod opt_tests {
fn test:
bb0(v0:BasicObject, v1:BasicObject):
v4:ArrayExact = NewArray v0, v1
v6:BasicObject = SendWithoutBlock v4, :size
Return v6
PatchPoint MethodRedefined(Array@0x1000, size@0x1008)
v9:Fixnum = CCall size@0x1010, v4
Return v9
"#]]);
}

Expand Down
Loading