Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
K

kiddo

> 编程语言
Open source

Kiddo

172 stars0 likes1 views
WebsiteGitHub

About

Kiddo

Kiddo

A high-performance k-d tree library for exact and approximate nearest-neighbour queries in low-dimensional spaces.

Built with an aggressive focus on query performance, including cache-aware layouts and optional SIMD-accelerated code paths. See the companion benchmarking site to compare Kiddo against other k-d tree implementations across a range of workloads.

  • Crate
  • Documentation
  • Usage
  • Examples
  • What's New In v6
  • MSRV
  • Benchmarks
  • Change Log
  • License

Kiddo v6 provides a single generic KdTree that supports floating-point (f64, f32, f16), selected fixed-point (via the fixed crate), and unsigned-integer (u8, u16, u32) types as coordinates, along with both mutable and immutable usage patterns.

Kiddo is designed for low-dimensional search problems, especially 2D, 3D, and 4D workloads. Typical use cases include point-cloud analysis, astronomical catalogue crossmatching, colour quantization and palette lookup, local neighbourhood queries in simulations, and other nearest-neighbour and radius-search tasks. Kiddo has been used for diverse geographical and scientific workloads including geocoding, astronomy, cosmology, computer-aided drug discovery, crystallography, and computational neuroscience.

If your points are known up front and the tree will be built once and then queried, start with ImmutableKdTree. It offers the best query performance and pairs well with rkyv for zero-copy loading of prebuilt trees from disk; when used with memory-mapped files, loading can be effectively instant.

If you need to add or remove points after construction, start with MutableKdTree. Mutable trees remain a good fit for many dynamic workloads, but they do not currently perform dynamic rebalancing, so workloads with substantial growth or heavy churn may benefit from periodic rebuilds.

ImmutableKdTree and MutableKdTree are convenience aliases for KdTree with sensible defaults for these common read-heavy and mutable workloads.

Kiddo is not intended as a library for high-dimensional vector search or feature matching over hundreds or thousands of dimensions, where plain k-d trees are usually the wrong data structure and other approaches are more appropriate. The API does not impose a hard dimensional limit, but Kiddo is primarily intended for low-dimensional workloads.

Kiddo supports the following query types through its fluent builder API, starting from KdTree::query:

  • .nearest_one() finds the single nearest item to a query point. Useful for tasks like finding the nearest airport to a given location, or finding the nearest catalogued star to a sky position.

  • .best_n_within(radius, n) finds the "best" n items within a specified distance of a query point, for some definition of "best". For example, "give me the 5 largest settlements within 50km of a given point, ordered by descending population", or "the 5 brightest stars within a degree of a point on the sky, ordered brightest first".

  • .nearest_one().approx() performs approximate nearest-neighbour (ANN) search, returning a good approximate nearest item, often much faster than exact nearest-neighbour search. Useful for latency-sensitive workloads like interactive point-cloud picking, or mapping image pixels to a palette colour during colour quantization.

  • .nearest_n(n) performs k-nearest-neighbour (k-NN) search, finding the n nearest items to a query point ordered by distance. Useful for finding the nearest weather stations or sensors to a location, or generating candidate correspondences for point-cloud registration.

  • .nearest_n(n).within(radius) finds up to n nearest items within a specified radius of a query point, ordered by distance. Useful when you want the closest local neighbours inside a meaningful cutoff, such as the nearest shops within 5 miles, or nearby atoms within an interaction radius.

  • .within(radius) finds all items within a specified radius of a query point, ordered by distance. Useful for radial catalogue searches in astronomy, or collision and proximity queries where the full neighbourhood is needed in sorted order.

  • .within(radius).unsorted() finds all items within a specified radius of a query point without sorting the results. This is often faster than .within(radius) when result order does not matter, such as finding all customers within 5 miles of a store, or collecting point-cloud neighbourhoods for clustering or normal estimation.

The builder API also supports boundary exclusivity, result projection, and periodic boundary conditions where applicable. If you can estimate how many matches an eagerly materialized .within(radius) query will return, add .with_result_capacity(estimated_count) before .execute() to avoid result-vector growth reallocations. This is only an allocation hint and does not limit the number of results returned; omit it when no reasonable estimate is available.

What's New In v6

Version 6 is a substantial API and architecture refresh. The biggest user-facing changes are:

  • A single generic KdTree now underpins both mutable and immutable usage patterns.
  • Queries now go through a fluent builder API starting from KdTree::query.
  • Query results now use explicit structs such as QueryResultItem and BestQueryResultItem.
  • Radius-bounded queries support exclusive boundaries.
  • Periodic boundary conditions are supported through the query builder.
  • Owned trees now support in-place item replacement via replace_item.
  • Support for rkyv 0.7 has been removed; v6 uses rkyv_08.

Migration from v5

The largest source-level breaking changes when moving from v5 are:

  • Direct query methods such as nearest_one, nearest_n, nearest_n_within, within, and within_unsorted have been replaced by builder chains beginning with .query(&point).
  • Query results are no longer returned as tuples or NearestNeighbour / BestNeighbour; use .distance, .item, and .point on QueryResultItem / BestQueryResultItem.
  • rkyv support is now exposed through the rkyv_08 crate feature.
  • The old kiddo::mutable::* and kiddo::immutable::* module-based public API is gone; use KdTree, ImmutableKdTree, and MutableKdTree from the crate root.

In practice, code like this in v5:

rust
let result = kdtree.nearest_one::>(&query);

becomes this in v6:

rust
let result = kdtree
    .query(&query)
    .nearest_one::>()
    .execute();

Usage

Add kiddo to Cargo.toml

toml
[dependencies]
kiddo = "6.3.0"

Add points to a k-d tree and query the nearest points with a distance metric:

…

See the examples documentation for some more detailed examples.

The same builder can be used to select unsorted range queries, approximate nearest-neighbour search, exclusive radius boundaries, result projection, and periodic boundary conditions. For example:

…

Batch Queries

When many query points are known up front, run them as one batch. A batch query is configured exactly like a single-point query and returns one result per query point, indexed by position in the input slice:

rust
use kiddo::{ImmutableKdTree, SquaredEuclidean};

let entries = vec![[0f64, 0f64], [1f64, 1f64], [2f64, 2f64]];
let kdtree = ImmutableKdTree::new_from_slice(&entries).unwrap();

let queries = [[0.1f64, 0.1], [1.9, 1.9]];

let results = kdtree
    .query_batch(&queries)
    .nearest_one::>()
    .execute();

assert_eq!(results[0].item, 0);
assert_eq!(results[1].item, 2);

Batches spread across multiple threads by default, via the multi-threaded feature. Results stay indexed by query position no matter how the work was scheduled, and for_each is available for allocation-free streaming.

How a batch is scheduled — the order query points are executed in, which thread runs each one, and how work is grouped — is deliberately unspecified, so that batch execution can keep getting faster without breaking callers. The kiddo::batch module documents exactly what is and is not guaranteed.

Optional Features

Kiddo exposes a number of optional crate features:

  • fixed enables support for fixed-point coordinate types from the fixed crate.

  • f16 enables support for half-precision floating-point coordinates via the half crate.

  • serde enables serialization and deserialization via Serde.

  • rkyv_08 enables zero-copy serialization and deserialization via rkyv 0.8.x. This is particularly useful for prebuilt immutable trees that you want to load very quickly, especially in conjunction with memory-mapped files.

  • simd (NIGHTLY) enables handwritten SIMD and prefetch intrinsics for additional performance where available. This requires a nightly Rust toolchain.

  • multi-threaded (default) lets tree construction and batch queries use multiple threads, via the current rayon thread pool, and is what pulls rayon in as a dependency.

    Disabling it drops that dependency and configures the threaded APIs out rather than quietly downgrading them: ParallelConstruction, KdTreeBuilder::with_parallel_construction, the *_parallel constructors, Executor::parallel() and Executor::with_min_queries_per_task() no longer exist, so code that used them fails to compile instead of silently running on one thread. Whatever still compiles keeps working and produces identical trees and results. Use it for WASM, embedded, or single-core targets, or wherever an extra dependency is not worth it.

  • huge_pages enables Linux-specific huge-page advice helpers for owned and archived tree storage.

  • leaf_nta_prefetch enables additional non-temporal leaf prefetch hints in some query paths. This is an advanced tuning feature and is only useful in specific workloads.

Kiddo also contains a number of additional feature flags used for internal experimentation, benchmarking, simulation, and specialized tuning. Most users will not need them.

NOTE: Support for rkyv 0.7 was removed in Kiddo v6.

v5.x

Version 5 bundled a complete re-write of ImmutableKdTree alongside some rationalization of feature names and a change of type of the max_qty parameter present in some query methods from usize to NonZero.

ImmutableKdTree rewrite

Many people had previously unsuccessfully tried to use ImmutableKdTree with data containing many points that had the same value on one or more of their axes, for example point clo

Issues· 10 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言