HNSW-Sharp

Available on NuGet Read more

HNSW-Sharp

A .NET library for fast approximate nearest-neighbor search using Hierarchical Navigable Small World graphs — index millions of vectors and run sub-millisecond k-NN queries from C#.

What is HNSW-Sharp?

HNSW-Sharp (also published as HNSW.Net) is a managed C# implementation of the Hierarchical Navigable Small World graph algorithm by Malkov and Yashunin — the de facto standard for fast approximate nearest-neighbor (ANN) search in high-dimensional spaces.

Exact k-NN search degrades sharply as the number of dimensions grows; HNSW trades a small amount of accuracy for orders-of-magnitude faster queries by walking a multi-layer proximity graph. This library wraps that algorithm in an idiomatic generic API — SmallWorld<TItem, TDistance> — so you can index float[] embeddings, custom structs, or anything else you can attach a distance function to.

Project on GitHub HNSW paper (arXiv) ACORN filtering paper (arXiv)


A first taste

using HNSW.Net;

var parameters = new SmallWorldParameters
{
    M = 15,
    LevelLambda = 1 / Math.Log(15),
    EfSearch = 50,
};

float[][] vectors = LoadEmbeddings();

var graph = new SmallWorld<float[], float>(
    CosineDistance.SIMDForUnits,
    DefaultRandomGenerator.Instance,
    parameters);

graph.AddItems(vectors);

float[] query = LoadQuery();
var top10 = graph.KNNSearch(query, k: 10);

KNNSearch returns the k closest items together with their numeric distance — typically in microseconds even on graphs with millions of vectors.


Why HNSW-Sharp?

Sub-millisecond queries at scale

Walking a hierarchical proximity graph is logarithmic in the number of items. Indexes with tens of millions of vectors return top-k results in well under a millisecond per query.

Generic over item and distance

SmallWorld<TItem, TDistance> works with any item type and any numeric distance. Plug in your own metric — Euclidean, Hamming, mixed-feature — without forking the library.

SIMD cosine out of the box

Four CosineDistance variants cover the universality/performance trade-off, including SIMD-accelerated paths for unit vectors. See Distance functions.

Serialize and reload

Write the entire graph to a stream with SerializeGraph and bring it back with DeserializeGraph. Skip rebuilds on application start and ship pre-built indexes with your app.

ACORN-γ filtering (experimental)

Opt in to ACORN-style filtered search with OptimizeForFiltering to run k-NN under predicates without falling off the speed cliff.

Thread-safe by default

A reader-writer lock around the graph makes concurrent KNNSearch calls safe alongside AddItems. Disable with threadSafe: false if you manage your own synchronisation.


Pick your path

Get Started

Install the NuGet package and build your first HNSW index in under a minute.

Core Concepts

What HNSW is, the role of M, EfSearch, and friends, and how distance functions plug in.

Guides

Build a graph, run k-NN search, serialize indexes, and use ACORN-style filtering.

Advanced Topics

Performance tuning, custom distance functions, thread-safety semantics, and progress reporting.

Source & Issues

Browse the source, file issues, and check the latest releases.


Requirements

Requirement Minimum Notes
.NET .NET 6.0+ The package multi-targets modern .NET; SIMD distances require a runtime with System.Numerics.Vector<T>.
Memory Roughly N × (M × 4 + vector size) Layer-zero degree is 2 × M. Plan for the index plus the vectors themselves.
Threading Optional read-write lock Pass threadSafe: false to the constructor if you serialise access elsewhere.

HNSW-Sharp has no native dependencies. Models, embeddings, and the index file are all plain managed bytes.


Learn more about HNSW

© 2026 HNSW-Sharp. All rights reserved.