Optimize String#getbyte

This commit is contained in:
Takashi Kokubun 2023-03-04 23:53:21 -08:00
parent 31babc5cea
commit f0218303e0
Notes: git 2023-03-06 07:29:31 +00:00
3 changed files with 27 additions and 1 deletions

View File

@ -2278,6 +2278,24 @@ module RubyVM::MJIT
false false
end end
# @param jit [RubyVM::MJIT::JITState]
# @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::Assembler]
def jit_rb_str_getbyte(jit, ctx, asm, argc, _known_recv_class)
return false if argc != 1
asm.comment('rb_str_getbyte')
index_opnd = ctx.stack_pop
str_opnd = ctx.stack_pop
asm.mov(C_ARGS[0], str_opnd)
asm.mov(C_ARGS[1], index_opnd)
asm.call(C.rb_str_getbyte)
ret_opnd = ctx.stack_push
asm.mov(ret_opnd, C_RET)
true
end
# @param jit [RubyVM::MJIT::JITState] # @param jit [RubyVM::MJIT::JITState]
# @param ctx [RubyVM::MJIT::Context] # @param ctx [RubyVM::MJIT::Context]
# @param asm [RubyVM::MJIT::Assembler] # @param asm [RubyVM::MJIT::Assembler]
@ -2364,6 +2382,7 @@ module RubyVM::MJIT
register_cfunc_method(Integer, :*, :jit_rb_int_mul) register_cfunc_method(Integer, :*, :jit_rb_int_mul)
register_cfunc_method(Integer, :/, :jit_rb_int_div) register_cfunc_method(Integer, :/, :jit_rb_int_div)
register_cfunc_method(Integer, :[], :jit_rb_int_aref) register_cfunc_method(Integer, :[], :jit_rb_int_aref)
register_cfunc_method(String, :getbyte, :jit_rb_str_getbyte)
end end
def register_cfunc_method(klass, mid_sym, func) def register_cfunc_method(klass, mid_sym, func)

View File

@ -420,6 +420,13 @@ module RubyVM::MJIT # :nodoc: all
rb_proc_t.new(proc_t_addr) rb_proc_t.new(proc_t_addr)
end end
def rb_str_getbyte
Primitive.cstmt! %{
extern VALUE rb_str_getbyte(VALUE str, VALUE index);
return SIZET2NUM((size_t)rb_str_getbyte);
}
end
#======================================================================================== #========================================================================================
# #
# Old stuff # Old stuff

View File

@ -6072,7 +6072,7 @@ rb_str_chr(VALUE str)
* *
* Related: String#setbyte. * Related: String#setbyte.
*/ */
static VALUE VALUE
rb_str_getbyte(VALUE str, VALUE index) rb_str_getbyte(VALUE str, VALUE index)
{ {
long pos = NUM2LONG(index); long pos = NUM2LONG(index);