소스 검색

don't alloc new maps until required

Darien Raymond 7 년 전
부모
커밋
e6b5356ea9
1개의 변경된 파일8개의 추가작업 그리고 4개의 파일을 삭제
  1. 8 4
      common/strmatcher/domain_matcher.go

+ 8 - 4
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