Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory usage in Bundler::Settings, and thus improve boot time #6884

Merged
merged 14 commits into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_") }
technicalpickles marked this conversation as resolved.
Show resolved Hide resolved
@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_")
technicalpickles marked this conversation as resolved.
Show resolved Hide resolved
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