Speedup for sparse knn code.