NearestNeighbors perform too many allocations, GC stalls
db7 opened this issue · comments
When continuously running KNN-searches, we have noticed that sometimes the garbage collector (Go >= 1.9) stalls the process for more than 2s. That caused often deadline exceeded errors in our search services.
The issue occurs when sorting the nodes of each level: KNN goes depth first and at each node sorts its children in a temporary buffer of size <= MaxChildren
. KNN then prunes some of the children and continues descending the tree in the recursion up to Rtree.Depth()
levels. When the recursion returns and advances to siblings, more buffers are allocated down to the leaf nodes. That causes too many allocations for large trees and generate excessive trash for the garbage collector.
Instead of allocating a buffer for each node to sort its children, one can allocate one single buffer at the root and slide over the buffer the deeper one goes. The buffer should have the size MaxChildren*Rtree.Depth()
.
Assuming your pull requests fixed this. Thanks for the contribution!