From 4c03a944f65d5406c2daca06513d936460e8311d Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 4 Aug 2020 14:37:32 +0800 Subject: [PATCH] PERF: Move URI regexp in `GroupSearchResults.blurb_for` into constant No need to generate the huge regexp over and over again. --- lib/search/grouped_search_results.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/search/grouped_search_results.rb b/lib/search/grouped_search_results.rb index b6059f0ec87..7f22c16f6f6 100644 --- a/lib/search/grouped_search_results.rb +++ b/lib/search/grouped_search_results.rb @@ -85,12 +85,14 @@ class Search end end + URI_REGEXP = URI.regexp(%w{http https}) + def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true) blurb = nil cooked = SearchIndexer.scrub_html_for_search(cooked) if scrub urls = Set.new - cooked.scan(URI.regexp(%w{http https})) { urls << $& } + cooked.scan(URI_REGEXP) { urls << $& } urls.each do |url| begin case File.extname(URI(url).path || "")