summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2023-03-18 23:33:10 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2023-03-18 23:35:42 -0700
commit32e0c97dfaa7973032e6ab93260be8eb4ad1458a (patch)
tree267a6f988cd4d3f1922f1a27c65114bbfd4c82e5
parent106cca5111981ce30bb6f4534ff72e8c09ace162 (diff)
RJIT: Optimize String#bytesize
-rw-r--r--lib/ruby_vm/rjit/insn_compiler.rb18
-rw-r--r--rjit_c.c1
-rw-r--r--rjit_c.rb4
-rw-r--r--string.c2
-rwxr-xr-xtool/rjit/bindgen.rb1
-rw-r--r--yjit.c7
6 files changed, 24 insertions, 9 deletions
diff --git a/lib/ruby_vm/rjit/insn_compiler.rb b/lib/ruby_vm/rjit/insn_compiler.rb
index aa37388a5b..bbfc8c3d0a 100644
--- a/lib/ruby_vm/rjit/insn_compiler.rb
+++ b/lib/ruby_vm/rjit/insn_compiler.rb
@@ -2769,6 +2769,22 @@ module RubyVM::RJIT
# @param jit [RubyVM::RJIT::JITState]
# @param ctx [RubyVM::RJIT::Context]
# @param asm [RubyVM::RJIT::Assembler]
+ def jit_rb_str_bytesize(jit, ctx, asm, argc, known_recv_class)
+ asm.comment('String#bytesize')
+
+ recv = ctx.stack_pop(1)
+ asm.mov(C_ARGS[0], recv)
+ asm.call(C.rb_str_bytesize)
+
+ out_opnd = ctx.stack_push
+ asm.mov(out_opnd, C_RET)
+
+ true
+ end
+
+ # @param jit [RubyVM::RJIT::JITState]
+ # @param ctx [RubyVM::RJIT::Context]
+ # @param asm [RubyVM::RJIT::Assembler]
def jit_rb_str_getbyte(jit, ctx, asm, argc, _known_recv_class)
return false if argc != 1
asm.comment('rb_str_getbyte')
@@ -2852,7 +2868,7 @@ module RubyVM::RJIT
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)
+ register_cfunc_method(String, :bytesize, :jit_rb_str_bytesize)
#register_cfunc_method(String, :<<, :jit_rb_str_concat)
#register_cfunc_method(String, :+@, :jit_rb_str_uplus)
diff --git a/rjit_c.c b/rjit_c.c
index 1a2aa0b1b1..dd6067f334 100644
--- a/rjit_c.c
+++ b/rjit_c.c
@@ -497,6 +497,7 @@ extern void rb_vm_setinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, V
extern VALUE rb_vm_throw(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t throw_state, VALUE throwobj);
extern VALUE rb_reg_new_ary(VALUE ary, int opt);
extern void rb_vm_setclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *cfp, ID id, VALUE val, ICVARC ic);
+extern VALUE rb_str_bytesize(VALUE str);
#include "rjit_c.rbinc"
diff --git a/rjit_c.rb b/rjit_c.rb
index 6d855d3ad8..ea68fcf4fb 100644
--- a/rjit_c.rb
+++ b/rjit_c.rb
@@ -607,6 +607,10 @@ module RubyVM::RJIT # :nodoc: all
Primitive.cexpr! %q{ SIZET2NUM((size_t)rb_reg_nth_match) }
end
+ def C.rb_str_bytesize
+ Primitive.cexpr! %q{ SIZET2NUM((size_t)rb_str_bytesize) }
+ end
+
def C.rb_str_concat_literals
Primitive.cexpr! %q{ SIZET2NUM((size_t)rb_str_concat_literals) }
end
diff --git a/string.c b/string.c
index d661cfa003..a9359a7e3e 100644
--- a/string.c
+++ b/string.c
@@ -2186,7 +2186,7 @@ rb_str_length(VALUE str)
*
*/
-static VALUE
+VALUE
rb_str_bytesize(VALUE str)
{
return LONG2NUM(RSTRING_LEN(str));
diff --git a/tool/rjit/bindgen.rb b/tool/rjit/bindgen.rb
index 3c25edad09..b68123b38a 100755
--- a/tool/rjit/bindgen.rb
+++ b/tool/rjit/bindgen.rb
@@ -545,6 +545,7 @@ generator = BindingGenerator.new(
rb_ary_clear
rb_str_intern
rb_vm_setclassvariable
+ rb_str_bytesize
],
types: %w[
CALL_DATA
diff --git a/yjit.c b/yjit.c
index 85401318ad..d0d8ae463a 100644
--- a/yjit.c
+++ b/yjit.c
@@ -442,13 +442,6 @@ rb_iseq_opcode_at_pc(const rb_iseq_t *iseq, const VALUE *pc)
return rb_vm_insn_addr2opcode((const void *)at_pc);
}
-// used by jit_rb_str_bytesize in codegen.rs
-VALUE
-rb_str_bytesize(VALUE str)
-{
- return LONG2NUM(RSTRING_LEN(str));
-}
-
unsigned long
rb_RSTRING_LEN(VALUE str)
{