aboutsummaryrefslogtreecommitdiffstats
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authorwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-31 12:30:57 +0000
committerwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-31 12:30:57 +0000
commit9664b3133048debca63163ebf01f81d916da4a1b (patch)
tree1a83c7c7e483e8fcdce1c14b87725837fe9d894e /vm_insnhelper.c
parent86b63638611518197752c2e3cdbd77c37394f65a (diff)
downloadruby-9664b3133048debca63163ebf01f81d916da4a1b.tar.gz
Improve performance of implicit type conversion
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
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index a6f2943ecd..bb483d2d2c 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -2991,8 +2991,8 @@ static VALUE
vm_concat_array(VALUE ary1, VALUE ary2st)
{
const VALUE ary2 = ary2st;
- VALUE tmp1 = rb_check_convert_type(ary1, T_ARRAY, "Array", "to_a");
- VALUE tmp2 = rb_check_convert_type(ary2, T_ARRAY, "Array", "to_a");
+ VALUE tmp1 = rb_check_convert_type_with_id(ary1, T_ARRAY, "Array", idTo_a);
+ VALUE tmp2 = rb_check_convert_type_with_id(ary2, T_ARRAY, "Array", idTo_a);
if (NIL_P(tmp1)) {
tmp1 = rb_ary_new3(1, ary1);
@@ -3011,7 +3011,7 @@ vm_concat_array(VALUE ary1, VALUE ary2st)
static VALUE
vm_splat_array(VALUE flag, VALUE ary)
{
- VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a");
+ VALUE tmp = rb_check_convert_type_with_id(ary, T_ARRAY, "Array", idTo_a);
if (NIL_P(tmp)) {
return rb_ary_new3(1, ary);
}