Merge pull request #16233 from Bo98/formula_files_by_name-optimise

tap: optimise `CoreTap#formula_files_by_name`
This commit is contained in:
Kevin 2023-11-18 12:32:16 -08:00 committed by GitHub
commit bd2c9791a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1138,12 +1138,13 @@ class CoreTap < AbstractCoreTap
def formula_files_by_name
return super if Homebrew::EnvConfig.no_install_from_api?
tap_path = path.to_s
Homebrew::API::Formula.all_formulae.each_with_object({}) do |item, hash|
name, formula_hash = item
# If there's more than one item with the same path: use the longer one to prioritise more specific results.
existing_path = hash[name]
new_path = path/formula_hash["ruby_source_path"]
hash[name] = new_path if existing_path.nil? || existing_path.to_s.length < new_path.to_s.length
new_path = File.join(tap_path, formula_hash["ruby_source_path"]) # Pathname equivalent is slow in a tight loop
hash[name] = Pathname(new_path) if existing_path.nil? || existing_path.to_s.length < new_path.length
end
end
end