mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
a03653d386
The most common use case for `bind_call` is to protect from core
methods being redefined, for instance a typical use:
```ruby
UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
def real_mod_name(mod)
UNBOUND_METHOD_MODULE_NAME.bind_call(mod)
end
```
But it's extremely common that the method wasn't actually redefined.
In such case we can avoid creating a new callable method entry,
and simply delegate to the receiver.
This result in a 1.5-2X speed-up for the fast path, and little to
no impact on the slowpath:
```
compare-ruby: ruby 3.1.0dev (2021-02-05T06:33:00Z master b2674c1fd7
) [x86_64-darwin19]
built-ruby: ruby 3.1.0dev (2021-02-15T10:35:17Z bind-call-fastpath d687e06615) [x86_64-darwin19]
| |compare-ruby|built-ruby|
|:---------|-----------:|---------:|
|fastpath | 11.325M| 16.393M|
| | -| 1.45x|
|slowpath | 10.488M| 10.242M|
| | 1.02x| -|
```
16 lines
267 B
YAML
16 lines
267 B
YAML
prelude: |
|
|
named_module = Kernel
|
|
|
|
module FakeName
|
|
def self.name
|
|
"NotMyame".freeze
|
|
end
|
|
end
|
|
|
|
MOD_NAME = Module.instance_method(:name)
|
|
|
|
benchmark:
|
|
fastpath: MOD_NAME.bind_call(Kernel)
|
|
slowpath: MOD_NAME.bind_call(FakeName)
|
|
|
|
loop_count: 100_000
|