Skip to content

Commit

Permalink
Merge pull request #6884 from technicalpickles/settings-memory
Browse files Browse the repository at this point in the history
Improve memory usage in Bundler::Settings, and thus improve boot time
  • Loading branch information
indirect committed Aug 20, 2023
2 parents ee65a1d + 75c0f27 commit 06231fd
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions bundler/lib/bundler/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,22 @@ class Settings
def initialize(root = nil)
@root = root
@local_config = load_config(local_config_file)
@env_config = ENV.to_h.select {|key, _value| key =~ /\ABUNDLE_.+/ }

@env_config = ENV.to_h
@env_config.select! {|key, _value| key.start_with?("BUNDLE_") }
@env_config.delete("BUNDLE_")

@global_config = load_config(global_config_file)
@temporary = {}
end

def [](name)
key = key_for(name)
value = configs.values.map {|config| config[key] }.compact.first

values = configs.values
values.map! {|config| config[key] }
values.compact!
value = values.first

converted_value(value, name)
end
Expand Down Expand Up @@ -140,17 +148,22 @@ def set_global(key, value)
end

def all
keys = @temporary.keys | @global_config.keys | @local_config.keys | @env_config.keys
keys = @temporary.keys.union(@global_config.keys, @local_config.keys, @env_config.keys)

keys.map do |key|
key.sub(/^BUNDLE_/, "").gsub(/___/, "-").gsub(/__/, ".").downcase
end.sort
keys.map! do |key|
key = key.delete_prefix("BUNDLE_")
key.gsub!("___", "-")
key.gsub!("__", ".")
key.downcase!
key
end.sort!
keys
end

def local_overrides
repos = {}
all.each do |k|
repos[$'] = self[k] if k =~ /^local\./
repos[k.delete_prefix("local.")] = self[k] if k.start_with?("local.")
end
repos
end
Expand Down Expand Up @@ -329,16 +342,20 @@ def split_specific_setting_for(name)
end

def is_bool(name)
BOOL_KEYS.include?(name.to_s) || BOOL_KEYS.include?(parent_setting_for(name.to_s))
name = name.to_s
BOOL_KEYS.include?(name) || BOOL_KEYS.include?(parent_setting_for(name))
end

def is_string(name)
STRING_KEYS.include?(name.to_s) || name.to_s.start_with?("local.") || name.to_s.start_with?("mirror.") || name.to_s.start_with?("build.")
name = name.to_s
STRING_KEYS.include?(name) || name.start_with?("local.") || name.start_with?("mirror.") || name.start_with?("build.")
end

def to_bool(value)
case value
when nil, /\A(false|f|no|n|0|)\z/i, false
when String
value.match?(/\A(false|f|no|n|0|)\z/i) ? false : true
when nil, false
false
else
true
Expand Down Expand Up @@ -393,6 +410,8 @@ def set_key(raw_key, value, hash, file)
end

def converted_value(value, key)
key = key.to_s

if is_array(key)
to_array(value)
elsif value.nil?
Expand Down Expand Up @@ -491,8 +510,11 @@ def serializer_class

def self.key_for(key)
key = normalize_uri(key).to_s if key.is_a?(String) && key.start_with?("http", "mirror.http")
key = key.to_s.gsub(".", "__").gsub("-", "___").upcase
"BUNDLE_#{key}"
key = key.to_s.gsub(".", "__")
key.gsub!("-", "___")
key.upcase!

key.prepend("BUNDLE_")
end

# TODO: duplicates Rubygems#normalize_uri
Expand Down

0 comments on commit 06231fd

Please sign in to comment.