Project

General

Profile

Feature #17127

Updated by S_H_ (Shun Hiraoka) over 3 years ago

Some TrueClass methods is faster if implemented in Ruby code. 

 like this. 


 ```ruby 
 class TrueClass 
   def to_s 
     "true".freeze 
   end 
   alias_method :inspect, :to_s 

   def |(bool) 
     true 
   end 
 end 

 ``` 

 benchmark file: 
 ```yaml 
 benchmark: 
   to_s: | 
     true.to_s 
   inspect: | 
     true.inspect 
   or: | 
     true | false 
 loop_count: 1000000 
 ``` 


 benchmark result: 

 ```bash 
 sh@MyComputer:~/rubydev/build$ make benchmark/trueclass.yml -e COMPARE_RUBY=~/.rbenv/shims/ruby -e BENCH_RUBY=../install/bin/ruby 
 # Iteration per second (i/s) 

 |           |compare-ruby|built-ruby| 
 |:--------|-----------:|---------:| 
 |to_s       |       66.001M|     91.927M| 
 |           |             -|       1.39x| 
 |inspect    |       70.464M|     97.220M| 
 |           |             -|       1.38x| 
 |or         |       61.434M|     86.484M| 
 |           |             -|       1.41x| 
 ``` 

 `COMPARE_RUBY` is `ruby 2.8.0dev (2020-08-20T04:24:55Z master 6509652c13) [x86_64-linux]`. `BENCH_RUBY` is ahead of `ruby 2.8.0dev (2020-08-20T04:24:55Z master 6509652c13) [x86_64-linux]`. 

 Probably,    inline method cache was able to speed up theese methods. 

 pull request: 
 https://github.com/ruby/ruby/pull/3435

Back