1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[AdBlockRule] Improve performance of rule matching with QStringMatcher

Use QStringMatcher instead of QString::contains in RegExpFilters.
QStringMatcher internally uses fast Boyer-Moore algorithm.
This commit is contained in:
David Rosca 2014-04-13 11:49:39 +02:00
parent f086c1563a
commit eb8ae20cc1
2 changed files with 19 additions and 7 deletions

View File

@ -468,7 +468,7 @@ void AdBlockRule::parseFilter()
m_type = RegExpMatchRule; m_type = RegExpMatchRule;
m_regExp = new RegExp; m_regExp = new RegExp;
m_regExp->regExp = QzRegExp(parsedLine, m_caseSensitivity); m_regExp->regExp = QzRegExp(parsedLine, m_caseSensitivity);
m_regExp->regExpStrings = parseRegExpFilter(parsedLine); m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return; return;
} }
@ -509,7 +509,7 @@ void AdBlockRule::parseFilter()
m_type = RegExpMatchRule; m_type = RegExpMatchRule;
m_regExp = new RegExp; m_regExp = new RegExp;
m_regExp->regExp = QzRegExp(createRegExpFromFilter(parsedLine), m_caseSensitivity); m_regExp->regExp = QzRegExp(createRegExpFromFilter(parsedLine), m_caseSensitivity);
m_regExp->regExpStrings = parseRegExpFilter(parsedLine); m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return; return;
} }
@ -632,6 +632,18 @@ QString AdBlockRule::createRegExpFromFilter(const QString &filter) const
return parsed; return parsed;
} }
QList<QStringMatcher> AdBlockRule::createStringMatchers(const QStringList &filters) const
{
QList<QStringMatcher> matchers;
matchers.reserve(filters.size());
foreach (const QString &filter, filters) {
matchers.append(QStringMatcher(filter, m_caseSensitivity));
}
return matchers;
}
bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const
{ {
return QzTools::matchDomain(filter, domain); return QzTools::matchDomain(filter, domain);
@ -641,10 +653,9 @@ bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const
{ {
Q_ASSERT(m_regExp); Q_ASSERT(m_regExp);
foreach (const QString &string, m_regExp->regExpStrings) { foreach (const QStringMatcher &matcher, m_regExp->matchers) {
if (!url.contains(string)) { if (matcher.indexIn(url) == -1)
return false; return false;
}
} }
return true; return true;

View File

@ -47,7 +47,7 @@
#define ADBLOCKRULE_H #define ADBLOCKRULE_H
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringMatcher>
#include "qzcommon.h" #include "qzcommon.h"
#include "qzregexp.h" #include "qzregexp.h"
@ -136,6 +136,7 @@ private:
bool filterIsOnlyDomain(const QString &filter) const; bool filterIsOnlyDomain(const QString &filter) const;
bool filterIsOnlyEndsMatch(const QString &filter) const; bool filterIsOnlyEndsMatch(const QString &filter) const;
QString createRegExpFromFilter(const QString &filter) const; QString createRegExpFromFilter(const QString &filter) const;
QList<QStringMatcher> createStringMatchers(const QStringList &filters) const;
AdBlockSubscription* m_subscription; AdBlockSubscription* m_subscription;
@ -159,7 +160,7 @@ private:
struct RegExp { struct RegExp {
QzRegExp regExp; QzRegExp regExp;
QStringList regExpStrings; QList<QStringMatcher> matchers;
}; };
// Use dynamic allocation to save memory // Use dynamic allocation to save memory