HNSW-Sharp

Parameters

SmallWorldParameters is the single configuration object passed to SmallWorld<TItem, TDistance>. Most defaults are sensible; the table below explains what each knob does and when to change it.

Construction knobs

Property Default Effect
M 10 Graph degree. Higher values mean better recall and faster search, at the cost of memory and build time. Typical range: 864.
LevelLambda 1 / Math.Log(M) Controls the level distribution. Keep in sync with M — set both together.
ConstructionPruning (efConstruction) 200 Number of candidates considered when inserting an item. Larger values produce a higher-quality graph at the cost of build time. Doesn't affect query time.
NeighbourHeuristic SelectSimple Strategy for picking neighbours during insertion. SelectHeuristic keeps more diverse neighbours — useful for clustered data.
ExpandBestSelection false Only meaningful with SelectHeuristic. Expands the candidate pool before pruning.
KeepPrunedConnections false Only meaningful with SelectHeuristic. Keeps pruned candidates as second-best edges.
EnableDistanceCacheForConstruction true Caches distance computations during build. Disable only if distances are nearly free.
InitialDistanceCacheSize 1_048_576 Initial size of the distance cache. Reset to 0 on DeserializeGraph.
InitialItemsSize 1024 Pre-allocates the items list. Set to your expected dataset size to avoid resizing.

Search knobs

Property Default Effect
EfSearch 50 Number of candidates kept during query-time graph walk. Higher values mean better recall at the cost of latency.

EfSearch is the runtime dial — change it between queries without rebuilding the graph.

ACORN filtering knobs

OptimizeForFiltering = true switches the construction strategy to ACORN-γ, an extension of HNSW designed for filtered nearest-neighbor search. When the predicate is selective, the standard HNSW graph "falls off" — most edges point to nodes that fail the filter, so the walk wastes time. ACORN keeps more diverse edges at layer 0 to compensate.

Property Default Effect
OptimizeForFiltering false Enables the ACORN construction strategy.
Gamma (γ) 1 Neighbour expansion factor. Higher values build a denser graph that survives more selective filters.
Mb 10 Compression parameter for layer 0.

For a worked example, see Filtering.

Tuning workflow

  1. Pick M first. M = 16 for float[] embeddings of dimension 100–1000 is a reasonable default. Larger values help dense data.
  2. Build once with the defaults, measure recall against bruteforce, and tune ConstructionPruning if recall is too low.
  3. Tune EfSearch at query time. It's the cheapest dial and gives you a direct recall/latency curve without rebuilding.
  4. Only touch the heuristic and ACORN knobs when you have a specific reason — measured recall problems on clustered data, or filtered search.

A starting configuration

var parameters = new SmallWorldParameters
{
    M = 16,
    LevelLambda = 1 / Math.Log(16),
    ConstructionPruning = 200,
    EfSearch = 64,
    InitialItemsSize = expectedItemCount,
};

Build, measure, adjust. Don't tune by inspection — measure recall on a real query set.

Referenced by

© 2026 HNSW-Sharp. All rights reserved.