Skip to content

Commit fd02356

Browse files
authored
ZJIT: Optimize NewArray to use rb_ec_ary_new_from_values (ruby#15391)
1 parent d5c7cf0 commit fd02356

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

zjit/src/codegen.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,15 +1451,16 @@ fn gen_new_array(
14511451
) -> lir::Opnd {
14521452
gen_prepare_leaf_call_with_gc(asm, state);
14531453

1454-
let length: c_long = elements.len().try_into().expect("Unable to fit length of elements into c_long");
1454+
let num: c_long = elements.len().try_into().expect("Unable to fit length of elements into c_long");
14551455

1456-
let new_array = asm_ccall!(asm, rb_ary_new_capa, length.into());
1457-
1458-
for val in elements {
1459-
asm_ccall!(asm, rb_ary_push, new_array, val);
1456+
if elements.is_empty() {
1457+
asm_ccall!(asm, rb_ec_ary_new_from_values, EC, 0i64.into(), Opnd::UImm(0))
1458+
} else {
1459+
let argv = gen_push_opnds(asm, &elements);
1460+
let new_array = asm_ccall!(asm, rb_ec_ary_new_from_values, EC, num.into(), argv);
1461+
gen_pop_opnds(asm, &elements);
1462+
new_array
14601463
}
1461-
1462-
new_array
14631464
}
14641465

14651466
/// Compile array access (`array[index]`)

0 commit comments

Comments
 (0)