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

Conversation

Watson1978
Copy link
Contributor

Which issue(s) this PR fixes:
Fixes #

What this PR does / why we need it:
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

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

Docs Changes:

Release Note:

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 <[email protected]>
Copy link
Contributor

@daipom daipom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Great!

I am concerned about the point that these codes are in the compat layer.
I think it's okay to include performance improvements, but am I correct?

@Watson1978
Copy link
Contributor Author

It is just a simple replacement.
So, I think it is ok if the CI passes :)

@daipom daipom added this to the v1.17.1 milestone Aug 15, 2024
Copy link
Contributor

@daipom daipom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!!

@daipom daipom merged commit e184fd2 into fluent:master Aug 15, 2024
16 checks passed
@Watson1978 Watson1978 deleted the bind_call branch August 15, 2024 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants