Skip to content

Commit

Permalink
refactor: reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Nov 15, 2021
1 parent 465ff31 commit 8c9a0bc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions util/suffix_tree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"runtime"
"strings"
)

Expand All @@ -18,9 +19,24 @@ func NewNodeFromRules(rules ...string) *Node {
for i := range rules {
n.Add(rules[i])
}

n.node = n.node.lite()
runtime.GC()
return n
}

func (n *node) lite() *node {
lite := &node{
secs: make([]string, 0, len(n.secs)),
subNodes: make([]*node, 0, len(n.subNodes)),
}
lite.secs = append(lite.secs, n.secs...)
for i := range n.subNodes {
lite.subNodes = append(lite.subNodes, n.subNodes[i].lite())
}
return lite
}

func (n *Node) String() string {
return n.string("", " ")
}
Expand Down

0 comments on commit 8c9a0bc

Please sign in to comment.