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#cask_files_by_name. #16775

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/cask/cask_loader.rb
Expand Up @@ -588,8 +588,7 @@ def self.default_path(token)
def self.find_cask_in_tap(token, tap)
filename = "#{token}.rb"

Tap.cask_files_by_name(tap)
.fetch(token, tap.cask_dir/filename)
tap.cask_files_by_name.fetch(token, tap.cask_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.cask_files_by_name.fetch(token, tap.cask_dir/filename)
tap.cask_files_by_name.fetch(token) { tap.cask_dir/filename }

Nit: You could use a block here to avoid building Pathname objects when the result 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 Seems like a good follow-up PR if it has any measurable performance impact.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have any benchmarks to back this up but we have had a lot of performance problems related to creating loads of Pathname objects before. It just seemed like an easy way to avoid that.

Copy link
Member

Choose a reason for hiding this comment

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

@apainintheneck Ok, thanks, will bear that in mind in future.

end
end
end
14 changes: 4 additions & 10 deletions Library/Homebrew/tap.rb
Expand Up @@ -129,6 +129,7 @@ def clear_cache
@formula_names = nil
@formula_files = nil
@cask_files = nil
@cask_files_by_name = nil
@alias_dir = nil
@alias_files = nil
@aliases = nil
Expand Down Expand Up @@ -590,19 +591,12 @@ def cask_files
end
end

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

# A mapping of {Cask} tokens to {Cask} file paths.
#
# @private
sig { returns(T::Hash[String, Pathname]) }
def cask_files_by_name
cask_files.each_with_object({}) do |file, hash|
@cask_files_by_name ||= cask_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