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
# Cache compiled regex
@regex ||= begin
token = /(?:"[^"]*"|'[^']*')/ token = /(?:"[^"]*"|'[^']*')/
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/
regex = /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m /\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)
# Cache compiled regex
@uri_regex ||= begin
uri_regex = ::URI::DEFAULT_PARSER.make_regexp uri_regex = ::URI::DEFAULT_PARSER.make_regexp
return false unless ref.to_s.match?(Regexp.new("\\A#{uri_regex.source}\\Z", uri_regex.options)) 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