Skip to content

Commit 1d83553

Browse files
authored
ZJIT: Inline << and push for Array in single arg case (ruby#14926)
Fixes Shopify#813
1 parent b3fb91f commit 1d83553

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

zjit/src/cruby_methods.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ pub fn init() -> Annotations {
203203
annotate!(rb_cArray, "reverse", types::ArrayExact, leaf, elidable);
204204
annotate!(rb_cArray, "join", types::StringExact);
205205
annotate!(rb_cArray, "[]", inline_array_aref);
206+
annotate!(rb_cArray, "<<", inline_array_push);
207+
annotate!(rb_cArray, "push", inline_array_push);
206208
annotate!(rb_cHash, "[]", inline_hash_aref);
207209
annotate!(rb_cHash, "size", types::Fixnum, no_gc, leaf, elidable);
208210
annotate!(rb_cHash, "empty?", types::BoolExact, no_gc, leaf, elidable);
@@ -266,6 +268,15 @@ fn inline_array_aref(fun: &mut hir::Function, block: hir::BlockId, recv: hir::In
266268
None
267269
}
268270

271+
fn inline_array_push(fun: &mut hir::Function, block: hir::BlockId, recv: hir::InsnId, args: &[hir::InsnId], state: hir::InsnId) -> Option<hir::InsnId> {
272+
// Inline only the case of `<<` or `push` when called with a single argument.
273+
if let &[val] = args {
274+
let _ = fun.push_insn(block, hir::Insn::ArrayPush { array: recv, val, state });
275+
return Some(recv);
276+
}
277+
None
278+
}
279+
269280
fn inline_hash_aref(fun: &mut hir::Function, block: hir::BlockId, recv: hir::InsnId, args: &[hir::InsnId], state: hir::InsnId) -> Option<hir::InsnId> {
270281
if let &[key] = args {
271282
let result = fun.push_insn(block, hir::Insn::HashAref { hash: recv, key, state });

zjit/src/hir.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14008,7 +14008,69 @@ mod opt_tests {
1400814008
PatchPoint MethodRedefined(Array@0x1000, <<@0x1008, cme:0x1010)
1400914009
PatchPoint NoSingletonClass(Array@0x1000)
1401014010
v26:ArrayExact = GuardType v9, ArrayExact
14011-
v27:BasicObject = CCallWithFrame <<@0x1038, v26, v13
14011+
ArrayPush v26, v13
14012+
IncrCounter inline_cfunc_optimized_send_count
14013+
CheckInterrupts
14014+
Return v26
14015+
");
14016+
}
14017+
14018+
#[test]
14019+
fn test_optimize_array_push_single_arg() {
14020+
eval("
14021+
def test(arr)
14022+
arr.push(1)
14023+
end
14024+
test([])
14025+
");
14026+
assert_snapshot!(hir_string("test"), @r"
14027+
fn test@<compiled>:3:
14028+
bb0():
14029+
EntryPoint interpreter
14030+
v1:BasicObject = LoadSelf
14031+
v2:BasicObject = GetLocal l0, SP@4
14032+
Jump bb2(v1, v2)
14033+
bb1(v5:BasicObject, v6:BasicObject):
14034+
EntryPoint JIT(0)
14035+
Jump bb2(v5, v6)
14036+
bb2(v8:BasicObject, v9:BasicObject):
14037+
v13:Fixnum[1] = Const Value(1)
14038+
PatchPoint MethodRedefined(Array@0x1000, push@0x1008, cme:0x1010)
14039+
PatchPoint NoSingletonClass(Array@0x1000)
14040+
v24:ArrayExact = GuardType v9, ArrayExact
14041+
ArrayPush v24, v13
14042+
IncrCounter inline_cfunc_optimized_send_count
14043+
CheckInterrupts
14044+
Return v24
14045+
");
14046+
}
14047+
14048+
#[test]
14049+
fn test_do_not_optimize_array_push_multi_arg() {
14050+
eval("
14051+
def test(arr)
14052+
arr.push(1,2,3)
14053+
end
14054+
test([])
14055+
");
14056+
assert_snapshot!(hir_string("test"), @r"
14057+
fn test@<compiled>:3:
14058+
bb0():
14059+
EntryPoint interpreter
14060+
v1:BasicObject = LoadSelf
14061+
v2:BasicObject = GetLocal l0, SP@4
14062+
Jump bb2(v1, v2)
14063+
bb1(v5:BasicObject, v6:BasicObject):
14064+
EntryPoint JIT(0)
14065+
Jump bb2(v5, v6)
14066+
bb2(v8:BasicObject, v9:BasicObject):
14067+
v13:Fixnum[1] = Const Value(1)
14068+
v14:Fixnum[2] = Const Value(2)
14069+
v15:Fixnum[3] = Const Value(3)
14070+
PatchPoint MethodRedefined(Array@0x1000, push@0x1008, cme:0x1010)
14071+
PatchPoint NoSingletonClass(Array@0x1000)
14072+
v26:ArrayExact = GuardType v9, ArrayExact
14073+
v27:BasicObject = CCallVariadic push@0x1038, v26, v13, v14, v15
1401214074
CheckInterrupts
1401314075
Return v27
1401414076
");

0 commit comments

Comments
 (0)