Feature #17127
openSome TrueClass methods are faster if implemented in Ruby
Description
Some TrueClass methods are faster if implemented in Ruby code.
like this.
class TrueClass
def to_s
"true".freeze
end
alias_method :inspect, :to_s
def |(bool)
true
end
end
benchmark file:
benchmark:
to_s: |
true.to_s
inspect: |
true.inspect
or: |
true | false
loop_count: 1000000
benchmark result:
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 these methods.
pull request:
https://github.com/ruby/ruby/pull/3435
Updated by sawa (Tsuyoshi Sawada) over 4 years ago
- Subject changed from Some TrueClass methods is faster if implemented in Ruby to Some TrueClass methods are faster if implemented in Ruby
- Description updated (diff)
Updated by k0kubun (Takashi Kokubun) over 4 years ago
off topic: If you write an entire ticket in English, I think you should post your ticket to ruby-core to attract more audiences instead of ruby-dev, which has been for tickets written in Japanese.
Updated by S_H_ (Shun Hiraoka) about 4 years ago
k0kubun (Takashi Kokubun) wrote in #note-3:
off topic: If you write an entire ticket in English, I think you should post your ticket to ruby-core to attract more audiences instead of ruby-dev, which has been for tickets written in Japanese.
Thanks!
Updated by S_H_ (Shun Hiraoka) about 4 years ago
these method able to speeding up implemented by Ruby code.
TrueClass#^
TrueClass#&
benchmark:
benchmark:
to_s: |
true.to_s
inspect: |
true.inspect
or: |
true | false
xor: |
true ^ false
and: |
true & false
loop_count: 1000000
result:
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 | 63.991M| 93.655M|
| | -| 1.46x|
|inspect | 83.655M| 96.044M|
| | -| 1.15x|
|or | 38.138M| 77.149M|
| | -| 2.02x|
|xor | 61.388M| 76.603M|
| | -| 1.25x|
|and | 59.928M| 71.783M|
| | -| 1.20x|
COMPARE_RUBY
is ruby 2.8.0dev (2020-08-26T05:28:05Z master 72cb9bc55f) [x86_64-linux]
. BENCH_RUBY
is patched ruby.
Updated by S_H_ (Shun Hiraoka) about 4 years ago
recreate PR for merge .rb
files