dnsfilter -- avoid using regexps when simple suffix match is enough.

This covers 96.98% of all adguard dns rules.
This commit is contained in:
Eugene Bujak
2018-10-04 13:19:43 +03:00
parent 9e939e5754
commit cb97a254a5
3 changed files with 94 additions and 5 deletions

View File

@@ -67,6 +67,10 @@ type rule struct {
// user-supplied data
listID uint32
// suffix matching
isSuffix bool
suffix string
// compiled regexp
compiled *regexp.Regexp
@@ -387,12 +391,21 @@ func (rule *rule) extractShortcut() {
func (rule *rule) compile() error {
rule.RLock()
isCompiled := rule.compiled != nil
isCompiled := rule.isSuffix || rule.compiled != nil
rule.RUnlock()
if isCompiled {
return nil
}
isSuffix, suffix := getSuffix(rule.text)
if isSuffix {
rule.Lock()
rule.isSuffix = isSuffix
rule.suffix = suffix
rule.Unlock()
return nil
}
expr, err := ruleToRegexp(rule.text)
if err != nil {
return err
@@ -417,7 +430,16 @@ func (rule *rule) match(host string) (Result, error) {
return res, err
}
rule.RLock()
matched := rule.compiled.MatchString(host)
matched := false
if rule.isSuffix {
if host == rule.suffix {
matched = true
} else if strings.HasSuffix(host, "."+rule.suffix) {
matched = true
}
} else {
matched = rule.compiled.MatchString(host)
}
rule.RUnlock()
if matched {
res.Reason = FilteredBlackList