From: watson1978@... Date: 2017-03-21T00:45:12+00:00 Subject: [ruby-dev:50024] [Ruby trunk Bug#13341] Improve performance of implicit type conversion Issue #13341 has been reported by watson1978 (Shizuo Fujita). ---------------------------------------- Bug #13341: Improve performance of implicit type conversion https://bugs.ruby-lang.org/issues/13341 * Author: watson1978 (Shizuo Fujita) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: * Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN ---------------------------------------- At least, Array#flatten will be faster around 20%. Seems that strncmp() & strcmp() in convert_type() are slightly heavy to look up the method's id for type conversion. (https://github.com/ruby/ruby/blob/4f2db15b42d7b8eb5b304a92ba2296632dba3edf/object.c#L2634-L2643) This patch will use known method's id directly. ### Before ~~~ user system total real Array#flatten (rb_check_convert_type2) 1.000000 0.000000 1.000000 ( 1.001917) Array#+ (rb_convert_type2) 1.010000 0.000000 1.010000 ( 1.006383) ~~~ ### After ~~~ user system total real Array#flatten (rb_check_convert_type2) 0.830000 0.000000 0.830000 ( 0.833411) Array#+ (rb_convert_type2) 0.950000 0.000000 0.950000 ( 0.953832) ~~~ ### Test Code ~~~ require 'benchmark' Benchmark.bmbm do |x| ary = [] 100.times { |i| ary << i } array = [ary] x.report "Array#flatten (rb_check_convert_type2)"do 100000.times do array.flatten end end x.report "Array#+ (rb_convert_type2)"do class Foo def to_ary [1,2,3] end end obj = Foo.new 2000000.times do array + obj end end end ~~~ ### Patch The patch is in https://github.com/ruby/ruby/pull/1537 -- https://bugs.ruby-lang.org/