aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bundler/index.rb
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2023-09-14 19:14:08 -0700
committergit <svn-admin@ruby-lang.org>2023-09-15 22:01:10 +0000
commit2cf5fe58fbe8053f661552786a93d8cad7c307e0 (patch)
treea7dde1755c88ec7ab29bf715dc48289c6e6e93b0 /lib/bundler/index.rb
parent4e8869c663f82f5745070e3bce8902b800b1b6a7 (diff)
downloadruby-2cf5fe58fbe8053f661552786a93d8cad7c307e0.tar.gz
[rubygems/rubygems] Avoid allocating empty hashes in Index
Since the hashes have a default proc that returns a (new) empty hash, we can avoid allocating those empty hashes when we are only doing lookups. Test from running `bundle update --bundler` against a rails app I have lying around: ``` ==> memprof.after.txt <== Total allocated: 9.71 MB (68282 objects) Total retained: 4.87 MB (33791 objects) ==> memprof.before.txt <== Total allocated: 10.83 MB (100596 objects) Total retained: 5.02 MB (34721 objects) ``` https://github.com/rubygems/rubygems/commit/8f7c9cb23e
Diffstat (limited to 'lib/bundler/index.rb')
-rw-r--r--lib/bundler/index.rb9
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/bundler/index.rb b/lib/bundler/index.rb
index 6a20302322..1b2d3b5333 100644
--- a/lib/bundler/index.rb
+++ b/lib/bundler/index.rb
@@ -179,7 +179,8 @@ module Bundler
end
def specs_by_name(name)
- @specs[name].values
+ return EMPTY_SEARCH unless specs = @specs.fetch(name, nil)
+ specs.values
end
EMPTY_SEARCH = [].freeze
@@ -190,11 +191,13 @@ module Bundler
end
def find_by_spec(spec)
- @specs[spec.name][spec.full_name]
+ return unless specs = @specs.fetch(spec.name, nil)
+ specs[spec.full_name]
end
def exist?(spec)
- @specs[spec.name].key?(spec.full_name)
+ return unless specs = @specs.fetch(spec.name, nil)
+ specs.key?(spec.full_name)
end
end
end