Project

General

Profile

Actions

Feature #17127

open

Some TrueClass methods are faster if implemented in Ruby

Added by S_H_ (Shun Hiraoka) 12 months ago. Updated 9 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-dev:50948]

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

Actions #1

Updated by S_H_ (Shun Hiraoka) 12 months ago

  • Description updated (diff)
Actions #2

Updated by sawa (Tsuyoshi Sawada) 12 months ago

  • Description updated (diff)
  • Subject changed from Some TrueClass methods is faster if implemented in Ruby to Some TrueClass methods are faster if implemented in Ruby

Updated by k0kubun (Takashi Kokubun) 12 months 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) 11 months 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) 11 months 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.

Actions

Also available in: Atom PDF