From 106cca5111981ce30bb6f4534ff72e8c09ace162 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 18 Mar 2023 23:24:57 -0700 Subject: RJIT: Optimize String#empty? --- lib/ruby_vm/rjit/insn_compiler.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'lib/ruby_vm') diff --git a/lib/ruby_vm/rjit/insn_compiler.rb b/lib/ruby_vm/rjit/insn_compiler.rb index 39d6f07e4c..aa37388a5b 100644 --- a/lib/ruby_vm/rjit/insn_compiler.rb +++ b/lib/ruby_vm/rjit/insn_compiler.rb @@ -2729,6 +2729,29 @@ module RubyVM::RJIT true end + # @param jit [RubyVM::RJIT::JITState] + # @param ctx [RubyVM::RJIT::Context] + # @param asm [RubyVM::RJIT::Assembler] + def jit_rb_str_empty_p(jit, ctx, asm, argc, known_recv_class) + # Assume same offset to len embedded or not so we can use one code path to read the length + assert_equal(C.RString.offsetof(:as, :heap, :len), C.RString.offsetof(:as, :embed, :len)) + + recv_opnd = ctx.stack_pop(1) + out_opnd = ctx.stack_push + + asm.comment('get string length') + asm.mov(:rax, recv_opnd) + str_len_opnd = [:rax, C.RString.offsetof(:as, :heap, :len)] + + asm.cmp(str_len_opnd, 0) + asm.mov(:rax, Qfalse) + asm.mov(:rcx, Qtrue) + asm.cmove(:rax, :rcx) + asm.mov(out_opnd, :rax) + + return true + end + # @param jit [RubyVM::RJIT::JITState] # @param ctx [RubyVM::RJIT::Context] # @param asm [RubyVM::RJIT::Assembler] @@ -2826,7 +2849,7 @@ module RubyVM::RJIT register_cfunc_method(Integer, :===, :jit_rb_int_equal) # rb_str_to_s() methods in string.c - #register_cfunc_method(String, :empty?, :jit_rb_str_empty_p) + register_cfunc_method(String, :empty?, :jit_rb_str_empty_p) register_cfunc_method(String, :to_s, :jit_rb_str_to_s) register_cfunc_method(String, :to_str, :jit_rb_str_to_s) #register_cfunc_method(String, :bytesize, :jit_rb_str_bytesize) -- cgit v1.2.3