Commit 6f6a9ea
[ruby/rubygems] Replace instance method look up in plugin installer
`Gem::Installer.instance_methods(false).include?(:generate_plugins)` is
63x slower than `Gem::Installer.method_defined?(:generate_plugins)` in a
microbenchmark. The latter is a direct lookup, whereas the former will
create an array, which will be slower.
```ruby
require "benchmark/ips"
Benchmark.ips do |x|
x.report "instance_methods" do
Gem::Installer.instance_methods(false).include?(:generate_plugins)
end
x.report "method_defined" do
Gem::Installer.method_defined?(:generate_plugins)
end
x.compare!
end
```
```
$ ruby -I lib/ benchmark_methods.rb
ruby 3.4.4 (2025-05-14 revision ruby/rubygems@a38531fd3f) +PRISM [arm64-darwin23]
Warming up --------------------------------------
instance_methods 58.449k i/100ms
method_defined 3.375M i/100ms
Calculating -------------------------------------
instance_methods 541.874k (± 5.7%) i/s (1.85 μs/i) - 2.747M in 5.087825s
method_defined 34.263M (± 1.1%) i/s (29.19 ns/i) - 172.135M in 5.024524s
Comparison:
method_defined: 34263189.1 i/s
instance_methods: 541874.3 i/s - 63.23x slower
```
This does not make much difference in an overall benchmark or profile,
but this is more idiomatic Ruby than the prior code.
ruby/rubygems@49dec52cb21 parent d5d12ef commit 6f6a9ea
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
0 commit comments