From 19b8af7ebc880a67d0d88acf8cf936f02f55aa29 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 30 Nov 2022 20:38:03 +0800 Subject: [PATCH] fix: use same strClone function --- component/trie/node.go | 61 ++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/component/trie/node.go b/component/trie/node.go index e7baabb6..e19b40ac 100644 --- a/component/trie/node.go +++ b/component/trie/node.go @@ -4,21 +4,21 @@ import "strings" // Node is the trie's node type Node[T any] struct { + childMap map[string]*Node[T] childNode *Node[T] // optimize for only one child childStr string - children map[string]*Node[T] inited bool data T } func (n *Node[T]) getChild(s string) *Node[T] { - if n.children == nil { + if n.childMap == nil { if n.childNode != nil && n.childStr == s { return n.childNode } return nil } - return n.children[s] + return n.childMap[s] } func (n *Node[T]) hasChild(s string) bool { @@ -26,21 +26,21 @@ func (n *Node[T]) hasChild(s string) bool { } func (n *Node[T]) addChild(s string, child *Node[T]) { - if n.children == nil { + if n.childMap == nil { if n.childNode == nil { n.childStr = s n.childNode = child return } - n.children = map[string]*Node[T]{} + n.childMap = map[string]*Node[T]{} if n.childNode != nil { - n.children[n.childStr] = n.childNode + n.childMap[n.childStr] = n.childNode } n.childStr = "" n.childNode = nil } - n.children[s] = child + n.childMap[s] = child } func (n *Node[T]) getOrNewChild(s string) *Node[T] { @@ -54,45 +54,54 @@ func (n *Node[T]) getOrNewChild(s string) *Node[T] { func (n *Node[T]) optimize() { if len(n.childStr) > 0 { - n.childStr = strings.Clone(n.childStr) + n.childStr = strClone(n.childStr) } if n.childNode != nil { n.childNode.optimize() } - if n.children == nil { + if n.childMap == nil { return } - switch len(n.children) { + switch len(n.childMap) { case 0: - n.children = nil + n.childMap = nil return case 1: - for key := range n.children { + for key := range n.childMap { n.childStr = key - n.childNode = n.children[key] + n.childNode = n.childMap[key] } - n.children = nil + n.childMap = nil n.optimize() return } - children := make(map[string]*Node[T], len(n.children)) // avoid map reallocate memory - for key := range n.children { - child := n.children[key] + children := make(map[string]*Node[T], len(n.childMap)) // avoid map reallocate memory + for key := range n.childMap { + child := n.childMap[key] if child == nil { continue } - switch key { // try to save string's memory - case wildcard: - key = wildcard - case dotWildcard: - key = dotWildcard - default: - key = strings.Clone(key) - } + key = strClone(key) children[key] = child child.optimize() } - n.children = children + n.childMap = children +} + +func strClone(key string) string { + switch key { // try to save string's memory + case wildcard: + key = wildcard + case dotWildcard: + key = dotWildcard + case complexWildcard: + key = complexWildcard + case domainStep: + key = domainStep + default: + key = strings.Clone(key) + } + return key } func (n *Node[T]) isEmpty() bool {