derekparker/trie

Profiling results

Opened this issue · 1 comments

Here are some of the slowest lines in my usage
https://github.com/derekparker/trie/blob/master/trie.go#L72
I am attempting to optimize these by short circuit logic and removing unneeded function calls.

ROUTINE ======================== github.com/derekparker/trie.(*Trie).Find in /home/mdupont/gocode/src/github.com/derekparker/trie/trie.go
     240ms      1.93s (flat, cum) 36.90% of Total
         .          .     67:}
         .          .     68:
         .          .     69:// Finds and returns meta data associated
         .          .     70:// with `key`.
         .          .     71:func (t *Trie) Find(key string) (*Node, bool) {
      90ms      450ms     72:   node := findNode(t.Root(), []rune(key))
         .          .     73:   if node == nil {
         .          .     74:           return nil, false
         .          .     75:   }
         .          .     76:
     150ms      1.48s     77:   node, ok := node.Children()[nul]
         .          .     78:   if !ok || !node.term {
         .          .     79:           return nil, false
         .          .     80:   }
         .          .     81:
         .          .     82:   return node, true

And the second one

ROUTINE ======================== github.com/derekparker/trie.findNode in /home/mdupont/gocode/src/github.com/derekparker/trie/trie.go
      70ms       70ms (flat, cum)  1.34% of Total
         .          .    179:// mask of this node.
         .          .    180:func (n Node) Mask() uint64 {
         .          .    181:   return n.mask
         .          .    182:}
         .          .    183:

Maby we could check if the node was nil before calling it. 
https://github.com/derekparker/trie/blob/master/trie.go#L185
  60ms       60ms    184:func findNode(node *Node, runes []rune) *Node {
     .          .    185:   if node == nil {
     .          .    186:           return nil
     .          .    187:   }
     .          .    188:

  10ms       10ms    189:   if len(runes) == 0 {
     .          .    190:           return node
     .          .    191:   }

need to test this more... here are some small changes

This from a larger test

rker/trie/trie.go
     330ms      1.91s (flat, cum) 37.23% of Total
         .          .     66:   return node
         .          .     67:}
         .          .     68:
         .          .     69:// Finds and returns meta data associated
         .          .     70:// with `key`.
      20ms       20ms     71:func (t *Trie) Find(key string) (*Node, bool) {
      90ms      380ms     72:   node := findNode(t.Root(), []rune(key))
      30ms       30ms     73:   if node == nil {
         .          .     74:           return nil, false
         .          .     75:   }
         .          .     76:
     110ms      110ms     77:   node, ok :=node.children[0]
         .          .     78:   //node, ok := node.Children()[nul]
         .          .     79:   if !ok {
         .          .     80:           return nil, false
      40ms      1.33s     81:   }
         .          .     82:   if !node.term {
         .          .     83:           return nil, false
      40ms       40ms     84:   }
         .          .     85:
         .          .     86:   return node, true
         .          .     87:}

      80ms       80ms (flat, cum)  1.56% of Total
         .          .    187:
         .          .    188:func findNode(node *Node, runes []rune) *Node {
         .          .    189:   if node == nil {
         .          .    190:           return nil
         .          .    191:   }
      50ms       50ms    192:
         .          .    193:   if len(runes) == 0 {
         .          .    194:           return node
         .          .    195:   }
         .          .    196:
      20ms       20ms    197:   n, ok := node.Children()[runes[0]]
      10ms       10ms    198:   if !ok {
         .          .    199:           return nil
         .          .    200:   }
         .          .    201:
         .          .    202:   var nrunes []rune
         .          .    203:   if len(runes) > 1 {