RJIT: Optimize String#bytesize

This commit is contained in:
Takashi Kokubun 2023-03-18 23:33:10 -07:00
parent 106cca5111
commit 32e0c97dfa
6 changed files with 24 additions and 9 deletions

View File

@ -2766,6 +2766,22 @@ module RubyVM::RJIT
false
end
# @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]
@ -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)

View File

@ -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"

View File

@ -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

View File

@ -2186,7 +2186,7 @@ rb_str_length(VALUE str)
*
*/
static VALUE
VALUE
rb_str_bytesize(VALUE str)
{
return LONG2NUM(RSTRING_LEN(str));

View File

@ -545,6 +545,7 @@ generator = BindingGenerator.new(
rb_ary_clear
rb_str_intern
rb_vm_setclassvariable
rb_str_bytesize
],
types: %w[
CALL_DATA

7
yjit.c
View File

@ -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)
{