From e6b5356ea99449e80fcb3c2ec698f2b2d06dfbb3 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 22 Aug 2018 22:49:02 +0200 Subject: [PATCH] don't alloc new maps until required --- common/strmatcher/domain_matcher.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/common/strmatcher/domain_matcher.go b/common/strmatcher/domain_matcher.go index 317913240..f853efb05 100644 --- a/common/strmatcher/domain_matcher.go +++ b/common/strmatcher/domain_matcher.go @@ -19,18 +19,19 @@ type DomainMatcherGroup struct { func (g *DomainMatcherGroup) Add(domain string, value uint32) { if g.root == nil { - g.root = &node{ - sub: make(map[string]*node), - } + g.root = new(node) } current := g.root parts := breakDomain(domain) for i := len(parts) - 1; i >= 0; i-- { part := parts[i] + if current.sub == nil { + current.sub = make(map[string]*node) + } next := current.sub[part] if next == nil { - next = &node{sub: make(map[string]*node)} + next = new(node) current.sub[part] = next } current = next @@ -52,6 +53,9 @@ func (g *DomainMatcherGroup) Match(domain string) uint32 { parts := breakDomain(domain) for i := len(parts) - 1; i >= 0; i-- { part := parts[i] + if current.sub == nil { + break + } next := current.sub[part] if next == nil { break