frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Show HN: HNSW index for vector embeddings in approx 500 LOC

https://github.com/dicroce/hnsw
73•dicroce•5mo ago

Comments

oersted•5mo ago
I particularly appreciated the concise and plain explanation of the data-structure, it really demystifies it.

> HNSW is a graph structure that consists of levels that are more sparsely populated at the top and more densely populated at the bottom. Nodes within a layer have connections to other nodes that are near them on the same level. When a node is inserted a random level is picked and the node is inserted there. It is also inserted into all levels beneath that level down to 0.

> When searches arrive they start at the top and search that level (following connections) until they find the closest node in the top level. The search then descends and keeps searching nearby nodes. As the search progresses the code keeps track of the K nearest nodes it has seen. Eventually it either finds the value OR it finds the closest value on level 0 and the K nearest nodes seen are returned.

imurray•5mo ago
Looks neat. It would be useful to compare to other implementations: https://ann-benchmarks.com/ -- potentially not just speed, but implementation details that might change recall.
swyx•5mo ago
i think with small codebases like this is less about speed and more about education of essentials - i actually often encourage juniors to do small clones like this, feel proud, and then study the diffs with the at-scale repros and either feel humbled or feel like they have a contribution to make.
oersted•5mo ago
I see they are still using GloVe word embeddings for the first benchmark. Ah good ol' days! Nothing wrong with it, should still yield a realistic distribution of vectors. Just brings a lot of memories :)
antirez•5mo ago
Yes, HSNWs are not so complex, and they work great. I wrote an implementation myself, it's 2500 lines of code (5x the one of dicroce!), but inside there is binary and int8 quantization and many advanced features (include true deletions), and I commented it as much as possible. I hope you may find it useful to read alongside the one proposed by OP:

https://github.com/redis/redis/blob/unstable/modules/vector-...

Still, to be honest, I'm reading the 500 lines of code with great interest because I didn't thought it was possible to go so small, maybe part of the trick is that's in C++ and not in C, as for instance you don't have the queue code.

Also the strategy used by my implementation in order to re-link the orphaned nodes upon deletion adds complexity, too. (btw any feedback on that part is especially appreciated)

EDIT: Ok after carefully inspection this makes more sense :D

1. Yes, C++ helps, the vector class, the priority queue, ...

2. I forgot to say that I implemented serialization other than quantization, and this also includes quite some code.

3. Support for threads is another complexity / code-size price to pay.

And so forth. Ok, now it makes a lot of sense. Probably the 500 LOC implementation is a better first-exposure experience for newcomers. After accumulating all the "but how to..." questions, maybe my C implementation is a useful read.

mertleee•5mo ago
I guess I'm too simple to understand why this is useful? Just because it's been implemented in so few lines or?
cluckindan•5mo ago
It makes it possible to approximate k-nearest neighbor vector search without having to calculate cosine similarity, dot product or euclidean distance for all stored vectors on the fly.
jasonjmcghee•5mo ago
Implementations like these are incredibly useful for understanding how something like HNSW works, and being able to make derivative work.

I put together a tiny little implementation a while ago, the key thing being, it writes the index as a few parquet files, so you can host the index on a CDN and read from it via http range requests (e.g. via duckdb wasm).

Definitely isn't beating any benchmarks, but free (or wildly cheap) to host, as you serve it directly from a CDN and processing is done locally.

https://github.com/jasonjmcghee/portable-hnsw

kamranjon•5mo ago
This is incredibly cool, I’m surprised more people haven’t contributed to this - it seems like it’s only a few optimizations away from being performant enough for a pretty broad set of use cases.
jasonjmcghee•5mo ago
Appreciate the kind words! Obviously feel free to hack on it.

When I first built it, I spent some time trying to tackle the issue of needing to update the entire file (and create an invalidation) if you want to update the database, which might be fine, but closes a lot of doors. I kind of hit a wall on finding a convincing approach to solving it, given the constraints of the setup.

aboardRat4•5mo ago
I almost read it as NSFW.
vismit2000•5mo ago
A very nice intro to HNSW that previously appeared on HN[1]: https://github.com/brtholomy/hnsw/blob/master/README.md

[1] https://news.ycombinator.com/item?id=41694631

Show HN: A PSX/DOS style 3D game written in Rust with a custom software renderer

https://totenarctanz.itch.io/a-scavenging-trip
45•mvx64•7h ago•10 comments

Show HN: I built a platform for long-form media recs (books, articles, etc.)

https://rhomeapp.com/Guest
13•rohannih•9h ago•1 comments

Show HN: Coding AI Agent API for Developers

https://workser.ai
2•Khemmapich•3h ago•0 comments

Show HN: STT –> LLM –> TTS pipeline in C

https://github.com/RhinoDevel/mt_llm/tree/main/stt_llm_tts-pipeline-example
2•RhinoDevel•3h ago•0 comments

Show HN: A store that generates products from anything you type in search

https://anycrap.shop/
1146•kafked•3d ago•326 comments

Show HN: HuMo AI – Create Realistic Videos with Text, Image, and Audio Inputs

https://www.humoai.co
3•Viaya•6h ago•0 comments

Show HN: Pyproc – Call Python from Go Without CGO or Microservices

https://github.com/YuminosukeSato/pyproc
39•acc_10000•1d ago•9 comments

Show HN: I wrote a from-scratch OS to serve my blog

https://github.com/thass0/tatix
8•thasso•19h ago•0 comments

Show HN: I reverse engineered macOS to allow custom Lock Screen wallpapers

https://cindori.com/backdrop
78•cindori•2d ago•56 comments

Show HN: npm-daycare, an NPM proxy that filters out recent & small packages

https://github.com/stack-auth/npm-daycare
6•n2d4•8h ago•2 comments

Show HN: AI Code Detector – detect AI-generated code with 95% accuracy

https://code-detector.ai/
71•henryl•15h ago•63 comments

Show HN: Daffodil – Open-Source Ecommerce Framework to connect to any platform

https://github.com/graycoreio/daffodil
64•damienwebdev•1d ago•8 comments

Show HN: AI-powered web service combining FastAPI, Pydantic-AI, and MCP servers

https://github.com/Aherontas/Pycon_Greece_2025_Presentation_Agents
42•Aherontas•2d ago•22 comments

Show HN: Omarchy on CachyOS

https://github.com/mroboff/omarchy-on-cachyos
63•theYipster•2d ago•62 comments

Show HN: Scientific Calculator for Android

https://play.google.com/store/apps/details?id=scientific.codegres.calculator&hl=en_US
2•Codegres•10h ago•0 comments

Show HN: Semlib – Semantic Data Processing

https://github.com/anishathalye/semlib
59•anishathalye•1d ago•12 comments

Show HN: Dagger.js – A buildless, runtime-only JavaScript micro-framework

https://daggerjs.org
76•TonyPeakman•2d ago•73 comments

Show HN: Quizquestions.org – A free library for quiz questions

https://www.quizquestions.org/
3•Salim99•15h ago•0 comments

Show HN: Small Transfers – charge from 0.000001 USD per request for your SaaS

https://smalltransfers.com/
193•strnisa•6d ago•74 comments

Show HN: I built a decentralized protocol for predicting interest rate movement

https://kairosswap.com/
3•vinniejames•12h ago•0 comments

Show HN: Ghostpipe – Connect files in your codebase to user interfaces

https://github.com/inputlogic/ghostpipe
4•adriaanmulder•12h ago•1 comments

Show HN: I built a tool to visually manage my LLM prompt templates and save them

https://promptcanvas.ml4den.com/
3•ml4den•12h ago•2 comments

Show HN: MCP Server Installation Instructions Generator

https://hyprmcp.com/mcp-install-instructions-generator/
22•pmig•1d ago•6 comments

Show HN: Drop-in Redis replacement in Rust with 5M+ GET/s

https://github.com/mehrantsi/feox-server
25•mehrant•1d ago•3 comments

Show HN: I Collected Every Emoticon I Could Find – All Mood and Generator

https://emoticonhub.com/
2•AdityaGavit•14h ago•1 comments

Show HN: Datadef.io – Canvas for data lineage and metadata management

https://datadef.io/
12•theolouvart•2d ago•6 comments

Show HN: Vicinae – A native, Raycast-compatible launcher for Linux

https://github.com/vicinaehq/vicinae
179•aurellius•1w ago•34 comments

Show HN: I built an app store for open-source financial plans (on spreadsheets)

https://finfam.app/explore/views
46•mhashemi•1d ago•14 comments

Show HN: Clean Clode – Clean Messy Terminal Pastes from Claude Code and Codex

https://cleanclode.com
5•thewojo•17h ago•2 comments

Show HN: Ruminate – AI reading tool for understanding hard things

https://tryruminate.com/
17•rshanreddy•1d ago•3 comments