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

Simplify Tap#formula_files_by_name. #16777

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,6 @@ def self.find_formula_in_tap(name, tap)
"#{name}.rb"
end

Tap.formula_files_by_name(tap)
.fetch(name, tap.formula_dir/filename)
tap.formula_files_by_name.fetch(name, tap.formula_dir/filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tap.formula_files_by_name.fetch(name, tap.formula_dir/filename)
tap.formula_files_by_name.fetch(name) { tap.formula_dir/filename }

To avoid building unnecessary Pathname objects when the name is in the hash.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apainintheneck same deal as last PR: would like to see something like brew prof show a measurable impact before doing these sorts of micro-optimisations.

end
end
14 changes: 4 additions & 10 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def clear_cache
@command_dir = nil
@formula_names = nil
@formula_files = nil
@formula_files_by_name = nil
@cask_files = nil
@alias_dir = nil
@alias_files = nil
Expand Down Expand Up @@ -560,19 +561,12 @@ def formula_files
end
end

# A cached hash of {Formula} basenames to {Formula} file pathnames for a {Tap}
sig { params(tap: Tap).returns(T::Hash[String, Pathname]) }
def self.formula_files_by_name(tap)
cache_key = "formula_files_by_name_#{tap}"
cache.fetch(cache_key) do |key|
cache[key] = tap.formula_files_by_name
end
end

# A mapping of {Formula} names to {Formula} file paths.
#
# @private
sig { returns(T::Hash[String, Pathname]) }
def formula_files_by_name
formula_files.each_with_object({}) do |file, hash|
@formula_files_by_name ||= formula_files.each_with_object({}) do |file, hash|
# If there's more than one file with the same basename: use the longer one to prioritise more specific results.
basename = file.basename(".rb").to_s
existing_file = hash[basename]
Expand Down