Skip to content

Commit 6f6a9ea

Browse files
eileencodeshsbt
authored andcommitted
[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@49dec52cb2
1 parent d5d12ef commit 6f6a9ea

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/bundler/rubygems_gem_installer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def ensure_writable_dir(dir)
6969
end
7070

7171
def generate_plugins
72-
return unless Gem::Installer.instance_methods(false).include?(:generate_plugins)
72+
return unless Gem::Installer.method_defined?(:generate_plugins)
7373

7474
latest = Gem::Specification.stubs_for(spec.name).first
7575
return if latest && latest.version > spec.version

0 commit comments

Comments
 (0)