1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +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_regExp = new RegExp;
m_regExp->regExp = QzRegExp(parsedLine, m_caseSensitivity);
m_regExp->regExpStrings = parseRegExpFilter(parsedLine);
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return;
}
@ -509,7 +509,7 @@ void AdBlockRule::parseFilter()
m_type = RegExpMatchRule;
m_regExp = new RegExp;
m_regExp->regExp = QzRegExp(createRegExpFromFilter(parsedLine), m_caseSensitivity);
m_regExp->regExpStrings = parseRegExpFilter(parsedLine);
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return;
}
@ -632,6 +632,18 @@ QString AdBlockRule::createRegExpFromFilter(const QString &filter) const
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
{
return QzTools::matchDomain(filter, domain);
@ -641,10 +653,9 @@ bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const
{
Q_ASSERT(m_regExp);
foreach (const QString &string, m_regExp->regExpStrings) {
if (!url.contains(string)) {
foreach (const QStringMatcher &matcher, m_regExp->matchers) {
if (matcher.indexIn(url) == -1)
return false;
}
}
return true;

View File

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