View Source HNSW
A dead-simple pure Elixir Hierarchical Navigable Small World (HNSW) module.
You can read a deep dive explanation of HNSW here.
Why?
The Elixir NX project has a project called hnswlib, which is a NIF wrapper around the C++ hnswlib library. While I love the fact that you can write NIFs in order to drop down to higher performing languages, and to bind to libraries that exist in these languages, I always tend to find myself having issues with NIFs. Sometimes it's because memory usage may not be properly reflected by the BEAM (since a NIF may not control the memory allocated by the library), sometimes the interface works in a non-intuitive way because the original library isn't necessarily compatible, tracing and the like is usually more problematic, and on top of all of that, there's a pretty big cognitive overhead to NIFs - you need to know the other language, and the other language is either C, C++, or Rust, all three of which are notoriously complex.
So, whenever I find myself seeing things that are only NIFs, I tend to want to find a pure Elixir version of the same thing. Sure, the performance might not be 100% on par (although you can get pretty much on par, see Jason versus jiffy for example), but at least the cognitive overhead is lower, the function can be traced with the BEAM, and other developers don't need to learn a brand new language just to use it.
Installation
If available in Hex, the package can be installed
by adding hnsw
to your list of dependencies in mix.exs
:
def deps do
[
{:hnsw, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/hnsw.