Skip to content

Commit

Permalink
Merge pull request Homebrew#14056 from ThatsJustCheesy/cache-regexes
Browse files Browse the repository at this point in the history
Improve performance of `brew info` by caching compiled regexes
  • Loading branch information
MikeMcQuaid authored Oct 27, 2022
2 parents 560f571 + cdc707f commit 3da846d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ def self.can_load?(ref)

content = ref.to_str

token = /(?:"[^"]*"|'[^']*')/
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/
regex = /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
# Cache compiled regex
@regex ||= begin
token = /(?:"[^"]*"|'[^']*')/
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/
/\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
end

content.match?(regex)
content.match?(@regex)
end

def initialize(content)
Expand Down Expand Up @@ -93,8 +96,13 @@ class FromURILoader < FromPathLoader
extend T::Sig

def self.can_load?(ref)
uri_regex = ::URI::DEFAULT_PARSER.make_regexp
return false unless ref.to_s.match?(Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options))
# Cache compiled regex
@uri_regex ||= begin
uri_regex = ::URI::DEFAULT_PARSER.make_regexp
Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options)
end

return false unless ref.to_s.match?(@uri_regex)

uri = URI(ref)
return false unless uri
Expand Down

0 comments on commit 3da846d

Please sign in to comment.