From b913cfc87b060340081f2d7ddb8612bcf80e64a8 Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Mon, 28 Aug 2023 20:02:08 -0400 Subject: [PATCH 1/2] (Further) Improve Bundler::Settings#[] performance and memory usage I previously identified and improved this method over in https://github.com/rubygems/rubygems/pull/6884 but while reviewing another memory_profiler profile, I realized another gain we can eek out. This method keeps comes up in part because `configs` is allocating a new Hash every time. My last change took advantage of that by using `map!` on it. `configs` is called quite often, including in this `[]` method, so there's a benefit to memoizing it. Back in `[]`, logically we are trying to find the first Hash in `configs` that has a value for the given key. Currently, we end up `map` and `compact` to just get that value. Instead, we can use a loop over `configs`, and break when we find the value for the key. --- bundler/lib/bundler/settings.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bundler/lib/bundler/settings.rb b/bundler/lib/bundler/settings.rb index f5999481a8f7..e31f207d90ff 100644 --- a/bundler/lib/bundler/settings.rb +++ b/bundler/lib/bundler/settings.rb @@ -102,10 +102,13 @@ def initialize(root = nil) def [](name) key = key_for(name) - values = configs.values - values.map! {|config| config[key] } - values.compact! - value = values.first + value = nil + configs.each do |_, config| + if config[key] + value = config[key] + break + end + end converted_value(value, name) end @@ -316,7 +319,7 @@ def key_for(key) private def configs - { + @configs ||= { :temporary => @temporary, :local => @local_config, :env => @env_config, From 75ffa8ef760a42c1f7f09fd9866ac2506257fea1 Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Tue, 29 Aug 2023 19:01:32 -0400 Subject: [PATCH 2/2] Update bundler/lib/bundler/settings.rb Co-authored-by: Martin Emde --- bundler/lib/bundler/settings.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bundler/lib/bundler/settings.rb b/bundler/lib/bundler/settings.rb index e31f207d90ff..92b4a6a4e377 100644 --- a/bundler/lib/bundler/settings.rb +++ b/bundler/lib/bundler/settings.rb @@ -104,10 +104,9 @@ def [](name) value = nil configs.each do |_, config| - if config[key] - value = config[key] - break - end + value = config[key] + next if value.nil? + break end converted_value(value, name)