arXiv:2610.16940cs.LGOct 2026

Manuscript Offprint

arXiv:2610.16940

KNN Implementation Details Can Dramatically Change Performance: An Example from Cover Trees

Amol Khanna, Edward Raff

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Nearest neighbor search algorithms are widely used and have a long history, but we find that key implementation details are seldom reviewed in articles about them. These details can materially impact performance, and thus change the conclusions one makes about a given algorithm's superiority. In this article, we highlight these impacts by reviewing the Cover Tree as an example, producing RUSTKNN, a unified Rust implementation of three construction variants of the Cover Tree with three query modes. Our analysis reveals: (1) the optimal query strategy depends on k and on the dataset—single-tree search is fastest at k=1 on three of our four largest datasets, while dual-tree search is fastest at k≥5k \geq 5 on the three largest—and choosing the wrong query mode can cost up to a 7.5×7.5 \times factor in runtime; (2) the cover tree base parameter, set to 2 in theory and 1.3 in practice, is not a safe default for dual-tree search: 1.3 is optimal on only one of seven datasets, and on covtype it is 1.6×1.6 \times slower than the optimum; (3) among five micro-optimizations, only early-exit distance computation has a consistent effect, and that effect grows with dimension (none at d=32d=32, 1.31×1.31 \times at d=54d=54, and 1.49×1.49 \times at d=784d=784); and (4) all-nearest-neighbor and held-out benchmarks yield different implementation rankings. All main timing tables report medians over 20 independent allocations with bootstrap 95% confidence intervals. RUSTKNN is open source at https://github.com/CrowdStrike/RustKNN.

1 Introduction

Nearest neighbor algorithms, and data structures to accelerate/improve them, exist in many forms. There are algorithms for pruning the search space like the KD-tree [1], sketches for calculating approximations of distance functions faster [2], and more. This line of research has been highly impactful in real-world systems, from information retrieval to clustering/visualization and even in Large Language Models via retrieval-augmented generation and vector databases. While approximate methods have dramatically gained popularity in recent years, exact metric-space k-NN remains necessary when the distance itself is the object of interest or approximate recall is unacceptable—for example, in cases like anomaly detection over raw features, protein-structure comparison under graph kernels, or retrieval under Wasserstein distance. However, when we survey papers (see Appendix K) in this broad area, we find that few consider basic details, such as distinguishing between a hold-out set's query performance and the original training data (26%). Fewer still report both distance-comparison counts and runtime (15%), even though the two are not always correlated, document micro-optimization details (11%)—the low-level implementation choices we isolate in Section 4.2—or batch vs. single-instance queries (10%).

40th Conference on Neural Information Processing Systems (NeurIPS 2026). Track on Evaluations and Datasets.

We argue that papers evaluating the performance of nearest neighbor dependent systems need to document and evaluate these factors carefully, or give a justified reason why only a subset matters for their use case. We support that argument by developing RUSTKNN as an example, re-evaluating the methods behind the seminal cover tree data structure—a structure with multiple proposed variants and one of the few with both empirical success and theoretical grounding for reducing nearest neighbor queries. We re-implement a cache-efficient cover tree with three query settings (single, batch, and dual-tree). Working in a compiled language allows both effective runtime evaluation and easy isolation of six micro-optimizations used in the literature (five of which we ablate), along with poorly documented nuances of the cover tree “base” parameter, which to our knowledge has never been independently tested.

Our contributions are:

  1. Optimization ablation. We quantify the individual and cumulative impact of five micro-optimizations via feature-flag ablation, revealing that early-exit distance computation is the only one with a consistent effect, and that its benefit grows with dimension (none on corel, d=32d=32; 1.31×1.31\times on covtype, d=54d=54; and 1.49×1.49\times on mnist, d=784d=784), while the traversal-level optimizations sit within about 10% of 1.0×1.0\times (Section 4.2).

  2. Query mode × k interaction. We show that the optimal query strategy depends on both k and the dataset: single-tree search is fastest at k=1 on mnist yet 3.3×3.3 \times slower than dual-tree search at k=10 on that same dataset, and choosing the wrong query mode costs up to 7.5×7.5 \times in runtime (Section 5.2).

  3. Base parameter analysis. We show that the conventional base 1.3 is not a safe default for dual-tree search: the optimum is 2.0 on four of seven datasets at k=1k=1 (five at k=10k=10) and 3.0 on the two smallest, and the default is 1.6×1.6 \times slower than the optimum on covtype, the largest dataset (Section 5.3).

  4. All-NN vs. held-out divergence. We demonstrate that the standard all-nearest-neighbor benchmark produces different implementation rankings than held-out evaluation, with implications for how the community evaluates nearest neighbor methods (Section 5.4).

In doing so, we find that no single method dominates; the results are dependent on dataset and query settings, and thus far more complicated and involved than the literature has previously implied. This complexity exists within just the cover tree literature alone, and we find it instructive to the questions we may have of the broader literature.

Sections 2–5 cover related work, background, our implementation, and the evaluation.

References (67)