Project

General

Profile

Actions

Feature #17127

open

Some TrueClass methods are faster if implemented in Ruby

Added by S_H_ (Shun Hiraoka) over 4 years ago. Updated about 4 years ago.

Status:
Open
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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0