From 4ff796a59063f2de4fd8e28405244b3abb15a837 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 28 May 2021 19:07:27 +0200 Subject: [PATCH] Make regular expressions static variables. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fewer allocations → faster program. About 17% speed increase with 89 books on up to 3 cores. Measured using the average of 4 runs. Before: ~15,5 seconds After: ~12,8 seconds Calls to allocation functions went down from 16.652.583 to 5.059.301. --- src/search.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 7cd5891..050bfbc 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -31,8 +31,8 @@ namespace epubgrep::search using std::string; -std::vector search(const fs::path &filepath, std::string_view regex, - const settings &opts) +std::vector search(const fs::path &filepath, + const std::string_view regex, const settings &opts) { boost::regex::flag_type flags{}; @@ -107,17 +107,21 @@ std::vector search(const fs::path &filepath, std::string_view regex, void cleanup_text(string &text) { + static const boost::regex re_header_start{"<[hH][1-6]"}; + static const boost::regex re_header_end{""; } - else if (boost::regex_match(text.substr(pos, 3), - boost::regex{""; } @@ -125,8 +129,6 @@ void cleanup_text(string &text) { auto endpos{text.find('>')}; boost::match_results match; - const boost::regex re_pagebreak{".+pagebreak.+(title|aria-label)" - "=\"([[:alnum:]]+)\".*"}; if (boost::regex_search(text.substr(pos, endpos).data(), match, re_pagebreak)) {