Skip to content

Commit

Permalink
cmd/audit: improve performance of versioned formula names
Browse files Browse the repository at this point in the history
There is a check for other versioned formula with the same name
in the file audit. This just memoizes all of the versioned
formulae in a tap during the first call and then uses that much
shorter list everytime it checks for things.
  • Loading branch information
apainintheneck committed Sep 16, 2023
1 parent 184efd9 commit 7e05a9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,19 @@ def versioned_formula?
name.include?("@")
end

# Returns any `@`-versioned formulae names for any formula (including versioned formulae).
# Returns any other `@`-versioned formulae names for any formula (including versioned formulae).
sig { returns(T::Array[String]) }
def versioned_formulae_names
versioned_names = if tap
name_prefix = "#{name.gsub(/(@[\d.]+)?$/, "")}@"
T.must(tap).formula_names.select do |name|
name.start_with?(name_prefix)
end
name_prefix = name.gsub(/(@[\d.]+)?$/, "")
T.must(tap).prefix_to_versioned_formulae_names.fetch(name_prefix, [])
elsif path.exist?
Pathname.glob(path.to_s.gsub(/(@[\d.]+)?\.rb$/, "@*.rb"))
.map { |path| path.basename(".rb").to_s }
.sort
else
raise "Either tap or path is required to list versioned formulae"
end.sort
end

versioned_names.reject do |versioned_name|
versioned_name == name
Expand Down
11 changes: 11 additions & 0 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@ def formula_names
@formula_names ||= formula_files.map(&method(:formula_file_to_name))
end

# A hash of all {Formula} name prefixes to versioned {Formula} in this {Tap}.
# @private
sig { returns(T::Hash[String, T::Array[String]]) }
def prefix_to_versioned_formulae_names
@prefix_to_versioned_formulae_names ||= formula_names
.select { |name| name.include?("@") }
.group_by { |name| name.gsub(/(@[\d.]+)?$/, "") }

Check warning on line 653 in Library/Homebrew/tap.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/tap.rb#L653

Added line #L653 was not covered by tests
.transform_values(&:sort)
.freeze
end

# An array of all {Cask} tokens of this {Tap}.
sig { returns(T::Array[String]) }
def cask_tokens
Expand Down

0 comments on commit 7e05a9b

Please sign in to comment.