Skip to content

Comments

Optimize collect#17

Closed
curlup wants to merge 3 commits intoderekparker:masterfrom
okmeter:optimize_collect
Closed

Optimize collect#17
curlup wants to merge 3 commits intoderekparker:masterfrom
okmeter:optimize_collect

Conversation

@curlup
Copy link
Contributor

@curlup curlup commented May 10, 2018

Was

$ go test -bench . -benchtime 4s -benchmem
TieKeys-4        	 1000000	      8335 ns/op	     847 B/op	      67 allocs/op
PrefixSearch-4   	   10000	    617542 ns/op	   71653 B/op	    6287 allocs/op
FuzzySearch-4    	    1000	   8789504 ns/op	  793950 B/op	   52051 allocs/op
BuildTree-4      	      30	 172820241 ns/op	64263468 B/op	  908713 allocs/op
PASS
ok  	_trie	30.907s

And now its

$ go test -bench . -benchtime 4s -benchmem
TieKeys-4        	 1000000	      4583 ns/op	     566 B/op	       7 allocs/op
PrefixSearch-4   	   20000	    219790 ns/op	   33746 B/op	      16 allocs/op
FuzzySearch-4    	    1000	   5759077 ns/op	  479819 B/op	    5065 allocs/op
BuildTree-4      	      20	 240212152 ns/op	78269795 B/op	 1347616 allocs/op
PASS
ok  	_trie	24.888s

It's better than #16, but there's additional memory footprint and time to build trie.

@curlup
Copy link
Contributor Author

curlup commented May 10, 2018

I've improved my result but still building trie takes more time/ops/mem than current impl

$ go test -bench . -benchtime 4s -benchmem
TieKeys-4        	 1000000	      4616 ns/op	     565 B/op	       7 allocs/op
PrefixSearch-4   	   30000	    217131 ns/op	   33710 B/op	      16 allocs/op
FuzzySearch-4    	    1000	   5641379 ns/op	  480559 B/op	    5065 allocs/op
BuildTree-4      	      30	 188456008 ns/op	70782355 B/op	 1011021 allocs/op
PASS

}


func findPath(node *Node, runes []rune) []*Node {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not seem to be used anywhere.

if n.term {
word := ""
for p := n.parent; p.depth != 0; p = p.parent {
word = string(p.val) + word
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to get the same speedup and greatly reduce allocations by using the string builder.

Copy link
Contributor

@jsign jsign Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to get the same speedup and greatly reduce allocations by using the string builder.

I did another PR to optimize collect and now see this one.
In particular, there I do use string builder as you mentioned... might be useful to mix both ideas maybe.

@derekparker
Copy link
Owner

Merged via another PR.

@derekparker derekparker closed this Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants