summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2023-03-18 23:24:57 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2023-03-18 23:24:57 -0700
commit106cca5111981ce30bb6f4534ff72e8c09ace162 (patch)
tree1380d8e99b8cad61167ef6dbdddec6af783c5771
parentca9355e173043bf2b403640bd88c48ac0d0d59ed (diff)
downloadruby-106cca5111981ce30bb6f4534ff72e8c09ace162.tar.gz
RJIT: Optimize String#empty?
-rw-r--r--lib/ruby_vm/rjit/insn_compiler.rb25
-rw-r--r--rjit_c.rb25
-rwxr-xr-xtool/rjit/bindgen.rb1
3 files changed, 50 insertions, 1 deletions
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
@@ -2732,6 +2732,29 @@ module RubyVM::RJIT
# @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]
def jit_rb_str_to_s(jit, ctx, asm, argc, known_recv_class)
return false if argc != 0
if known_recv_class == String
@@ -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)
diff --git a/rjit_c.rb b/rjit_c.rb
index f5467699ff..6d855d3ad8 100644
--- a/rjit_c.rb
+++ b/rjit_c.rb
@@ -748,6 +748,31 @@ module RubyVM::RJIT # :nodoc: all
)
end
+ def C.RString
+ @RString ||= CType::Struct.new(
+ "RString", Primitive.cexpr!("SIZEOF(struct RString)"),
+ basic: [self.RBasic, Primitive.cexpr!("OFFSETOF((*((struct RString *)NULL)), basic)")],
+ as: [CType::Union.new(
+ "", Primitive.cexpr!("SIZEOF(((struct RString *)NULL)->as)"),
+ heap: CType::Struct.new(
+ "", Primitive.cexpr!("SIZEOF(((struct RString *)NULL)->as.heap)"),
+ len: [CType::Immediate.parse("long"), Primitive.cexpr!("OFFSETOF(((struct RString *)NULL)->as.heap, len)")],
+ ptr: [CType::Pointer.new { CType::Immediate.parse("char") }, Primitive.cexpr!("OFFSETOF(((struct RString *)NULL)->as.heap, ptr)")],
+ aux: [CType::Union.new(
+ "", Primitive.cexpr!("SIZEOF(((struct RString *)NULL)->as.heap.aux)"),
+ capa: CType::Immediate.parse("long"),
+ shared: self.VALUE,
+ ), Primitive.cexpr!("OFFSETOF(((struct RString *)NULL)->as.heap, aux)")],
+ ),
+ embed: CType::Struct.new(
+ "", Primitive.cexpr!("SIZEOF(((struct RString *)NULL)->as.embed)"),
+ len: [CType::Immediate.parse("long"), Primitive.cexpr!("OFFSETOF(((struct RString *)NULL)->as.embed, len)")],
+ ary: [CType::Pointer.new { CType::Immediate.parse("char") }, Primitive.cexpr!("OFFSETOF(((struct RString *)NULL)->as.embed, ary)")],
+ ),
+ ), Primitive.cexpr!("OFFSETOF((*((struct RString *)NULL)), as)")],
+ )
+ end
+
def C.RStruct
@RStruct ||= CType::Struct.new(
"RStruct", Primitive.cexpr!("SIZEOF(struct RStruct)"),
diff --git a/tool/rjit/bindgen.rb b/tool/rjit/bindgen.rb
index 8dd69109c5..3c25edad09 100755
--- a/tool/rjit/bindgen.rb
+++ b/tool/rjit/bindgen.rb
@@ -556,6 +556,7 @@ generator = BindingGenerator.new(
RBasic
RObject
RStruct
+ RString
attr_index_t
iseq_inline_constant_cache
iseq_inline_constant_cache_entry