Make regular expressions static variables.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
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.
This commit is contained in:
parent
4df7b36dfc
commit
4ff796a590
@ -31,8 +31,8 @@ namespace epubgrep::search
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
std::vector<match> search(const fs::path &filepath, std::string_view regex,
|
std::vector<match> search(const fs::path &filepath,
|
||||||
const settings &opts)
|
const std::string_view regex, const settings &opts)
|
||||||
{
|
{
|
||||||
boost::regex::flag_type flags{};
|
boost::regex::flag_type flags{};
|
||||||
|
|
||||||
@ -107,17 +107,21 @@ std::vector<match> search(const fs::path &filepath, std::string_view regex,
|
|||||||
|
|
||||||
void cleanup_text(string &text)
|
void cleanup_text(string &text)
|
||||||
{
|
{
|
||||||
|
static const boost::regex re_header_start{"<[hH][1-6]"};
|
||||||
|
static const boost::regex re_header_end{"</[hH]"};
|
||||||
|
static const boost::regex re_pagebreak{".+pagebreak.+(title|aria-label)"
|
||||||
|
"=\"([[:alnum:]]+)\".*"};
|
||||||
|
|
||||||
size_t pos{};
|
size_t pos{};
|
||||||
while ((pos = text.find('<', pos)) != string::npos)
|
while ((pos = text.find('<', pos)) != string::npos)
|
||||||
{
|
{
|
||||||
// Mark headlines. We need them later on.
|
// Mark headlines. We need them later on.
|
||||||
string replacement;
|
string replacement;
|
||||||
if (boost::regex_match(text.substr(pos, 3), boost::regex{"<[hH][1-6]"}))
|
if (boost::regex_match(text.substr(pos, 3), re_header_start))
|
||||||
{
|
{
|
||||||
replacement = "<H>";
|
replacement = "<H>";
|
||||||
}
|
}
|
||||||
else if (boost::regex_match(text.substr(pos, 3),
|
else if (boost::regex_match(text.substr(pos, 3), re_header_end))
|
||||||
boost::regex{"</[hH]"}))
|
|
||||||
{
|
{
|
||||||
replacement = "</H>";
|
replacement = "</H>";
|
||||||
}
|
}
|
||||||
@ -125,8 +129,6 @@ void cleanup_text(string &text)
|
|||||||
{
|
{
|
||||||
auto endpos{text.find('>')};
|
auto endpos{text.find('>')};
|
||||||
boost::match_results<const char *> match;
|
boost::match_results<const char *> match;
|
||||||
const boost::regex re_pagebreak{".+pagebreak.+(title|aria-label)"
|
|
||||||
"=\"([[:alnum:]]+)\".*"};
|
|
||||||
if (boost::regex_search(text.substr(pos, endpos).data(), match,
|
if (boost::regex_search(text.substr(pos, endpos).data(), match,
|
||||||
re_pagebreak))
|
re_pagebreak))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user