Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UnboundMethod#bind_call method instead to improve performance #4588

Merged
merged 1 commit into from
Aug 15, 2024

Commits on Aug 14, 2024

  1. Use UnboundMethod#bind_call method instead to improve performance

    UnboundMethod#bind_call method had been introduced at Ruby 2.7 that it aims to improve performance.
    
    Ref. https://bugs.ruby-lang.org/issues/15955
    
    ### environment
    - Linux
      - Manjaro Linux x86_64
      - Kernel: 6.9.12-1-MANJARO
      - CPU: Intel i9-14900 (32) @ 5.500GHz
      - Compiler: gcc 14.1.1
      - Ruby: ruby 3.3.4 (2024-07-09 revision be1089c8ec) [x86_64-linux]
    
    ### micro benchmark code
    ```ruby
    require 'bundler/inline'
    gemfile do
      source 'https://rubygems.org'
      gem 'benchmark-ips'
    end
    
    class Foo
      def foo
      end
    end
    
    meth = Foo.instance_method(:foo)
    obj = Foo.new
    
    Benchmark.ips do |x|
      x.report("bind.call") {
        meth.bind(obj).call
      }
      x.report("bind_call") {
        meth.bind_call(obj)
      }
    
      x.compare!
    end
    ```
    
    ### result
    ```
    $ ruby bind_call.rb
    ruby 3.3.4 (2024-07-09 revision be1089c8ec) [x86_64-linux]
    Warming up --------------------------------------
               bind.call   766.396k i/100ms
               bind_call     1.651M i/100ms
    Calculating -------------------------------------
               bind.call      7.967M (± 3.4%) i/s -     39.853M in   5.008135s
               bind_call     16.881M (± 4.8%) i/s -     85.852M in   5.097269s
    
    Comparison:
               bind_call: 16880585.1 i/s
               bind.call:  7966891.3 i/s - 2.12x  slower
    ```
    
    Signed-off-by: Watson <watson1978@gmail.com>
    Watson1978 committed Aug 14, 2024
    Configuration menu
    Copy the full SHA
    aeb7ece View commit details
    Browse the repository at this point in the history