From 95c4ced39eb0d25925724456b222a56206b633db Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 19 Mar 2023 13:41:04 -0700 Subject: [PATCH] RJIT: Optimize Array#empty? --- lib/ruby_vm/rjit/insn_compiler.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/ruby_vm/rjit/insn_compiler.rb b/lib/ruby_vm/rjit/insn_compiler.rb index 62b4f5c58f..58028d51ab 100644 --- a/lib/ruby_vm/rjit/insn_compiler.rb +++ b/lib/ruby_vm/rjit/insn_compiler.rb @@ -2905,6 +2905,25 @@ module RubyVM::RJIT true end + # @param jit [RubyVM::RJIT::JITState] + # @param ctx [RubyVM::RJIT::Context] + # @param asm [RubyVM::RJIT::Assembler] + def jit_rb_ary_empty_p(jit, ctx, asm, argc, _known_recv_class) + array_reg = :rax + asm.mov(array_reg, ctx.stack_pop(1)) + len_opnd = jit_array_len(asm, array_reg, :rcx) + + asm.test(:rcx, :rcx) + asm.mov(:rax, Qfalse) + asm.mov(:rcx, Qtrue) + asm.cmovz(:rax, :rcx) + + out_opnd = ctx.stack_push + asm.mov(out_opnd, :rax) + + return true + end + # @param jit [RubyVM::RJIT::JITState] # @param ctx [RubyVM::RJIT::Context] # @param asm [RubyVM::RJIT::Assembler] @@ -2978,7 +2997,7 @@ module RubyVM::RJIT register_cfunc_method(String, :+@, :jit_rb_str_uplus) # rb_ary_empty_p() method in array.c - #register_cfunc_method(Array, :empty?, :jit_rb_ary_empty_p) + register_cfunc_method(Array, :empty?, :jit_rb_ary_empty_p) #register_cfunc_method(Kernel, :respond_to?, :jit_obj_respond_to) #register_cfunc_method(Kernel, :block_given?, :jit_rb_f_block_given_p)