From d0015e4ac6b812ea1681b1f5fa86fbab52a58960 Mon Sep 17 00:00:00 2001 From: watson1978 Date: Wed, 31 May 2017 12:30:57 +0000 Subject: Improve performance of implicit type conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To convert the object implicitly, it has had two parts in convert_type() which are 1. lookink up the method's id 2. calling the method Seems that strncmp() and strcmp() in convert_type() are slightly heavy to look up the method's id for type conversion. This patch will add and use internal APIs (rb_convert_type_with_id, rb_check_convert_type_with_id) to call the method without looking up the method's id when convert the object. Array#flatten -> 19 % up Array#+ -> 3 % up [ruby-dev:50024] [Bug #13341] [Fix GH-1537] ### Before Array#flatten 104.119k (± 1.1%) i/s - 525.690k in 5.049517s Array#+ 1.993M (± 1.8%) i/s - 10.010M in 5.024258s ### After Array#flatten 124.005k (± 1.0%) i/s - 624.240k in 5.034477s Array#+ 2.058M (± 4.8%) i/s - 10.302M in 5.019328s ### Test Code require 'benchmark/ips' class Foo def to_ary [1,2,3] end end Benchmark.ips do |x| ary = [] 100.times { |i| ary << i } array = [ary] x.report "Array#flatten" do |i| i.times { array.flatten } end x.report "Array#+" do |i| obj = Foo.new i.times { array + obj } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58978 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- compile.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index a2a80c919a..a448b2120b 100644 --- a/compile.c +++ b/compile.c @@ -6598,7 +6598,7 @@ register_label(rb_iseq_t *iseq, struct st_table *labels_table, VALUE obj) { LABEL *label = 0; st_data_t tmp; - obj = rb_convert_type(obj, T_SYMBOL, "Symbol", "to_sym"); + obj = rb_convert_type_with_id(obj, T_SYMBOL, "Symbol", idTo_sym); if (st_lookup(labels_table, obj, &tmp) == 0) { label = NEW_LABEL(0); @@ -6651,8 +6651,8 @@ iseq_build_from_ary_exception(rb_iseq_t *iseq, struct st_table *labels_table, LABEL *lstart, *lend, *lcont; unsigned int sp; - v = rb_convert_type(RARRAY_AREF(exception, i), T_ARRAY, - "Array", "to_ary"); + v = rb_convert_type_with_id(RARRAY_AREF(exception, i), T_ARRAY, + "Array", idTo_ary); if (RARRAY_LEN(v) != 6) { rb_raise(rb_eSyntaxError, "wrong exception entry"); } @@ -6840,7 +6840,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor, } break; case TS_GENTRY: - op = rb_convert_type(op, T_SYMBOL, "Symbol", "to_sym"); + op = rb_convert_type_with_id(op, T_SYMBOL, "Symbol", idTo_sym); argv[j] = (VALUE)rb_global_entry(SYM2ID(op)); break; case TS_IC: @@ -6856,8 +6856,8 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor, argv[j] = Qfalse; break; case TS_ID: - argv[j] = rb_convert_type(op, T_SYMBOL, - "Symbol", "to_sym"); + argv[j] = rb_convert_type_with_id(op, T_SYMBOL, + "Symbol", idTo_sym); break; case TS_CDHASH: { @@ -6865,7 +6865,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor, VALUE map = rb_hash_new(); rb_hash_tbl_raw(map)->type = &cdhash_type; - op = rb_convert_type(op, T_ARRAY, "Array", "to_ary"); + op = rb_convert_type_with_id(op, T_ARRAY, "Array", idTo_ary); for (i=0; i