From 7e05a9b57442754a1fc5ce1c64cb35601c83aba7 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Thu, 14 Sep 2023 21:25:52 -0700 Subject: [PATCH] cmd/audit: improve performance of versioned formula names 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. --- Library/Homebrew/formula.rb | 11 +++++------ Library/Homebrew/tap.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 329623be703be..424d3d8e3c740 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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 diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index d850d6052f47c..48ac7633f2e4f 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -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.]+)?$/, "") } + .transform_values(&:sort) + .freeze + end + # An array of all {Cask} tokens of this {Tap}. sig { returns(T::Array[String]) } def cask_tokens