From f86064e3226109a747da34c685f94e3042923bc2 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Tue, 4 Apr 2017 03:25:24 +0300 Subject: [PATCH] Revert "Cache rule regular expressions for performance" This reverts commit bacef1ca240147d22e9b985a88ff6d8a9ce6686a. --- src/base/rss/rssdownloadrule.cpp | 41 ++++++-------------------------- src/base/rss/rssdownloadrule.h | 4 ---- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/base/rss/rssdownloadrule.cpp b/src/base/rss/rssdownloadrule.cpp index afa054ea2..5f897a3d7 100644 --- a/src/base/rss/rssdownloadrule.cpp +++ b/src/base/rss/rssdownloadrule.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -50,29 +49,9 @@ DownloadRule::DownloadRule() , m_useRegex(false) , m_apstate(USE_GLOBAL) , m_ignoreDays(0) - , m_cachedRegexes(new QHash) { } -DownloadRule::~DownloadRule() -{ - delete m_cachedRegexes; -} - -QRegularExpression DownloadRule::getRegex(const QString &expression, bool isRegex) const -{ - // Use a cache of regexes so we don't have to continually recompile - big performance increase. - // The cache is cleared whenever the regex/wildcard, must or must not contain fields or - // episode filter are modified. - Q_ASSERT(!expression.isEmpty()); - QRegularExpression regex((*m_cachedRegexes)[expression]); - - if (!regex.pattern().isEmpty()) - return regex; - - return (*m_cachedRegexes)[expression] = QRegularExpression(isRegex ? expression : Utils::String::wildcardToRegex(expression), QRegularExpression::CaseInsensitiveOption); -} - bool DownloadRule::matches(const QString &articleTitle, const QString &expression) const { static QRegularExpression whitespace("\\s+"); @@ -82,14 +61,14 @@ bool DownloadRule::matches(const QString &articleTitle, const QString &expressio return true; } else if (m_useRegex) { - QRegularExpression reg(getRegex(expression)); + QRegularExpression reg(expression, QRegularExpression::CaseInsensitiveOption); return reg.match(articleTitle).hasMatch(); } else { // Only match if every wildcard token (separated by spaces) is present in the article name. // Order of wildcard tokens is unimportant (if order is important, they should have used *). foreach (const QString &wildcard, expression.split(whitespace, QString::SplitBehavior::SkipEmptyParts)) { - QRegularExpression reg(getRegex(wildcard, false)); + QRegularExpression reg(Utils::String::wildcardToRegex(wildcard), QRegularExpression::CaseInsensitiveOption); if (!reg.match(articleTitle).hasMatch()) return false; @@ -147,7 +126,7 @@ bool DownloadRule::matches(const QString &articleTitle) const if (!m_episodeFilter.isEmpty()) { qDebug() << "Checking episode filter:" << m_episodeFilter; - QRegularExpression f(getRegex("(^\\d{1,4})x(.*;$)")); + QRegularExpression f("(^\\d{1,4})x(.*;$)"); QRegularExpressionMatch matcher = f.match(m_episodeFilter); bool matched = matcher.hasMatch(); @@ -169,7 +148,7 @@ bool DownloadRule::matches(const QString &articleTitle) const if (ep.indexOf('-') != -1) { // Range detected QString partialPattern1 = "\\bs0?(\\d{1,4})[ -_\\.]?e(0?\\d{1,4})(?:\\D|\\b)"; QString partialPattern2 = "\\b(\\d{1,4})x(0?\\d{1,4})(?:\\D|\\b)"; - QRegularExpression reg(getRegex(partialPattern1)); + QRegularExpression reg(partialPattern1, QRegularExpression::CaseInsensitiveOption); if (ep.endsWith('-')) { // Infinite range int epOurs = ep.left(ep.size() - 1).toInt(); @@ -179,7 +158,7 @@ bool DownloadRule::matches(const QString &articleTitle) const matched = matcher.hasMatch(); if (!matched) { - reg = QRegularExpression(getRegex(partialPattern2)); + reg = QRegularExpression(partialPattern2, QRegularExpression::CaseInsensitiveOption); matcher = reg.match(articleTitle); matched = matcher.hasMatch(); } @@ -208,7 +187,7 @@ bool DownloadRule::matches(const QString &articleTitle) const matched = matcher.hasMatch(); if (!matched) { - reg = QRegularExpression(getRegex(partialPattern2)); + reg = QRegularExpression(partialPattern2, QRegularExpression::CaseInsensitiveOption); matcher = reg.match(articleTitle); matched = matcher.hasMatch(); } @@ -226,7 +205,7 @@ bool DownloadRule::matches(const QString &articleTitle) const } else { // Single number QString expStr("\\b(?:s0?" + s + "[ -_\\.]?" + "e0?" + ep + "|" + s + "x" + "0?" + ep + ")(?:\\D|\\b)"); - QRegularExpression reg(getRegex(expStr)); + QRegularExpression reg(expStr, QRegularExpression::CaseInsensitiveOption); if (reg.match(articleTitle).hasMatch()) { qDebug() << "Matched episode:" << ep; qDebug() << "Matched article:" << articleTitle; @@ -244,8 +223,6 @@ bool DownloadRule::matches(const QString &articleTitle) const void DownloadRule::setMustContain(const QString &tokens) { - m_cachedRegexes->clear(); - if (m_useRegex) m_mustContain = QStringList() << tokens; else @@ -258,8 +235,6 @@ void DownloadRule::setMustContain(const QString &tokens) void DownloadRule::setMustNotContain(const QString &tokens) { - m_cachedRegexes->clear(); - if (m_useRegex) m_mustNotContain = QStringList() << tokens; else @@ -409,7 +384,6 @@ bool DownloadRule::useRegex() const void DownloadRule::setUseRegex(bool enabled) { m_useRegex = enabled; - m_cachedRegexes->clear(); } QString DownloadRule::episodeFilter() const @@ -420,7 +394,6 @@ QString DownloadRule::episodeFilter() const void DownloadRule::setEpisodeFilter(const QString &e) { m_episodeFilter = e; - m_cachedRegexes->clear(); } QStringList DownloadRule::findMatchingArticles(const FeedPtr &feed) const diff --git a/src/base/rss/rssdownloadrule.h b/src/base/rss/rssdownloadrule.h index 7c7b1a053..6b64943be 100644 --- a/src/base/rss/rssdownloadrule.h +++ b/src/base/rss/rssdownloadrule.h @@ -36,7 +36,6 @@ #include #include -template class QHash; class QRegularExpression; namespace Rss @@ -58,7 +57,6 @@ namespace Rss }; DownloadRule(); - ~DownloadRule(); static DownloadRulePtr fromVariantHash(const QVariantHash &ruleHash); QVariantHash toVariantHash() const; @@ -93,7 +91,6 @@ namespace Rss private: bool matches(const QString &articleTitle, const QString &expression) const; - QRegularExpression getRegex(const QString &expression, bool isRegex = true) const; QString m_name; QStringList m_mustContain; @@ -107,7 +104,6 @@ namespace Rss AddPausedState m_apstate; QDateTime m_lastMatch; int m_ignoreDays; - mutable QHash *m_cachedRegexes; }; }