Merge pull request #14056 from ThatsJustCheesy/cache-regexes

Improve performance of `brew info` by caching compiled regexes
This commit is contained in:
Mike McQuaid 2022-10-27 08:53:59 +01:00 committed by GitHub
commit 3da846d1b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,12 +19,15 @@ module Cask
content = ref.to_str content = ref.to_str
token = /(?:"[^"]*"|'[^']*')/ # Cache compiled regex
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ @regex ||= begin
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ token = /(?:"[^"]*"|'[^']*')/
regex = /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m 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 end
def initialize(content) def initialize(content)
@ -93,8 +96,13 @@ module Cask
extend T::Sig extend T::Sig
def self.can_load?(ref) def self.can_load?(ref)
uri_regex = ::URI::DEFAULT_PARSER.make_regexp # Cache compiled regex
return false unless ref.to_s.match?(Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options)) @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) uri = URI(ref)
return false unless uri return false unless uri