Skip to content

Benchmark and optimise the HNSW index #100

Open

Description

@pranav7002

goal

reduce the build and search time of the HNSW index so it is close to industry standard HNSW libraries on ANN-Benchmarks, and if we can't get there, mention what is causing the gap and what would need to change later.

scope

  • only index build time and search time
  • delete is out of scope

current plan

  • run ANN-Benchmarks on the VortexDB HNSW index
  • run hnswlib on the same machine with the same settings and compare the recall vs queries per second curves and build times
  • study where VortexDB is behind at the same recall
  • profile build and search with flamegraphs to see where the time is actually going
  • optimise the biggest time losses one at a time, re running the benchmark after each change and comparing
  • write up the before/after results, and for anything still behind, what is causing it and what would need to change

Activity

  1. pranav7002 commented on Oct 14, 2026

    @pranav7002
    Author

    update, 14 Oct

    codebase: went through the codebase partly, the architecture, understood the HNSW insert and search path and how its different from hnswlib, three things that i plan to optimise first:

    1. only rerank when the neighbour list is full. right now every backlink adds the new point and then recalculates the distance from the neighbour to all its existing neighbours and sorts them, even if there is space for one more.
    2. use the diversity criterion when trimming a full list. right now a full list keeps the closest M neighbours by distance, changing this to going from closest to farthest and only keeping a candidate if it's closer to the node than to any neighbour already kept. this should improve recall at high ef.
    3. make the writes concurrent (dont know enough rust for this yet, will approach in some days)

    rust: learning rust, not done yet, so I will start writing code in about 3 to 4 days

    benchmark: An ANN-Benchmarks wrapper that runs VortexDB through the gRPC server written, i ran it against hnswlib on the same machine.

    settings: Fashion-MNIST (60k vectors, 784 dims, euclidean), M=16, efConstruction=200, ef from 10 to 800, k=10, single core, on Apple Silicon Mac (arm)

    recall@10 hnswlib QPS VortexDB QPS VortexDB without gRPC (estimate)
    0.95 3993 1698 (2.4x slower) 2370 (1.7x slower)
    0.99 2455 1145 (2.1x slower) 1416 (1.7x slower)
    0.999 1110 372 (3.0x slower) 396 (2.8x slower)
    index build 82s 124s (1.5x slower)
    Image Image

    will improve:

    • my mac is arm and hnswlib only has its SIMD distance code for x86, so the gap is probably wider on an Intel or amd machine.
    • the without gRPC column is an approximation, I measured the gRPC time on a server with only 10 vectors and subtracted the mean from each query.

    next: start profiling with flamegraphs(will take sometime, still learning how to profile properly).

    • should i make a PR for the benchmark adaptor or keep running it locally whenever needed?
    • gRPC is the design so I have not built native Python bindings, is that needed in this major?
  2. self-assigned this
    on Oct 16, 2026
  3. Shashank-k15 commented on Oct 29, 2026

    @Shashank-k15
    Contributor
    • my mac is arm and hnswlib only has its SIMD distance code for x86, so the gap is probably wider on an Intel or amd machine.

    You can use Kratos for benchmarking from now on, also update the numbers once you do it.

    • should i make a PR for the benchmark adaptor or keep running it locally whenever needed?

    I don't think you need to do that.

    • gRPC is the design so I have not built native Python bindings, is that needed in this major?

    No.

  4. Shashank-k15 commented on Oct 29, 2026

    @Shashank-k15
    Contributor
    1. Fashion-MNIST

    Use the SIFT-1M dataset for the benchmarks (you can use the Fashion-MNIST or siftsmal for quick tests)

  5. pranav7002 commented on Nov 28, 2026

    @pranav7002
    Author

    @Shashank-k15 update: benchmarks on SIFT-1M dataset. ran these on my personal linux machine, not in campus so couldn't use kratos for this. the simd working on x86 for hnswlib now.

    settings: 1M × 128, M=16, M0=32, efC=200, k=10, one thread, one core. seeds 100/101/102, median over the 3 seeds:

    main #104 hnswlib
    build (s) 2,116 2,058 375
    build distances 4,186M 3,986M –
    peak memory (MB) 1,256 1,356 –
    ef=10 qps / recall@10 4,012 / 0.746 3,815 / 0.746 20,221 / 0.711
    ef=40 qps / recall@10 1,625 / 0.939 1,590 / 0.939 9,696 / 0.928
    ef=200 qps / recall@10 441 / 0.995 432 / 0.995 2,507 / 0.996
    image

    #104 vs main:

    also, for these i ran the bench in the same process as the index, no grpc. i can run it with grpc later if needed. this way is more useful to me for now, with grpc the gains from my changes were very hard to get consistent since the network request cost made them fluctuate a lot, now i can see what each change made better or worse. it's also fairer to compare with hnswlib, since that runs in process too.

    i'll run the final benchmarks with the server after the major changes are in. can i keep doing it without the server till then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions