Skip to main content

Crate legit_hamming

Crate legit_hamming 

Source
Expand description

Exact Hamming radius search over 64-bit and 256-bit hashes held in memory: every stored record within distance d of a query, with its distance, and never a match missed.

A plain Rust library with no framework dependency. A set of Records holds fixed-width values, u64 or [u64; 4], each with an application’s 64-bit id or without. Several variants search it behind one trait, Search, and every one returns exactly the matches of the plainest of them:

  • Planned: the one to use. It owns the records and, within a memory budget, multi-index tables over them, and answers each query by a scan or a probe of the tables, whichever its estimate says costs less;
  • Scan: a flat scan on a code path chosen at run time from what the CPU has (Isa): popcnt, AVX2, AVX-512 or NEON;
  • MultiIndex: multi-index hashing with tables of 16-bit chunks, sized from the guarantee m(r + 1) − 1;
  • Reference: the plainest scan, which every other variant is checked against;
  • Mapped: a snapshot file searched in place, mapped into memory.

search and search_batch run one, on as many threads as asked, and return Found lists sorted by distance and then id, or position for a set without ids. A caller’s limit keeps the nearest matches, never the first found, a result says when anything cut it short, and Found::plan says how a planned query ran. neighbour_table measures how many records sit at each distance from a typical one.

use std::sync::Arc;

use legit_hamming::{Options, Planned, Records, search};

let records = Records::from_records([(1, 0b1011u64), (2, 0b1000), (5, u64::MAX)])?;
let index = Planned::new(Arc::new(records), 64 << 20, 1);

let found = search(&index, 0b1001, &Options::new(1));
let ids: Vec<_> = found.matches.iter().map(|m| (m.id, m.distance)).collect();
assert_eq!(ids, [(Some(1), 1), (Some(2), 1)]);
assert!(found.is_complete());

The service module puts named indexes and subsets loaded from snapshot directories behind a search service, for the legit-hamming program’s query command and, with the serve feature, its serve worker on Redis Streams, in the serve module.

unsafe code is confined to the vector kernels, behind a safe wrapper that checks the CPU at run time and falls back to the scalar path, and the memory map, whose constructors are unsafe fns: a map is sound only while no process changes its files, which the caller promises. Every other path, the service and the verify command included, reads files into memory it owns; the rest of the crate denies unsafe.

Modules§

serve
legit-hamming serve: the service as a worker on the Redis Streams work contract of legit_streams, with the task kind hamming.
service
The search service around the library: named indexes and subsets loaded from snapshot directories, searched with the options and answered with the result objects of the service contract, within limits checked before any work starts.
snapshot
Snapshot files: a set of records on disk, in parts, with a manifest of checksums.

Structs§

Cpu
What the running machine offers, for a report or a benchmark record.
Estimate
What a query was expected to cost each way, in records scanned: the numbers a plan was chosen by.
Found
The result of one query.
Hit
One stored value within the radius: its position in the set searched and its distance from the query.
HitList
The hits one share collected for one query, held within a bound.
Mapped
A snapshot searched in place: each part file is mapped into memory and scanned as it lies, an id word and then the value’s words, least significant first.
Match
A stored record within the radius of a query.
Memory
The memory a variant takes, in bytes.
MultiIndex
Multi-index hashing (Norouzi, Punjani and Fleet, “Fast search in Hamming space with multi-index hashing”, CVPR 2012): the value is split into equal chunks, each with a table from the chunk’s key to the records that have it. A search probes each table with every key within that chunk’s radius of the query’s chunk, from chunk_radii, and checks each record found against the whole query.
NeighbourTable
How many other records sit at each distance from a sample of an index’s own records.
Options
How to run a search.
Plan
The plan a query ran with: its strategy, and the estimate that chose it.
Planned
A set of records that answers each query the cheaper way: a scan of every record, or a probe of its multi-index tables, chosen by an estimate of each.
Planning
The plans for one call of a search, and the threads it runs on: what Search::plan returns.
Records
A set of records in memory: each a fixed-width value, with an unsigned 64-bit id or without.
Reference
The reference: the plainest possible scan, which every other variant is checked against.
Scan
A flat scan of every record, on a chosen code path.
Share
One share of a search split across threads: share index of count.
Stop
When to stop collecting candidates: never, or at a deadline.

Enums§

Cut
What cut a result short.
Error
What went wrong. Every failure in this crate, bad input included, is one of these.
Isa
A code path for the distance kernels, named after the instructions it needs.
Strategy
How a query is answered.
Verify
Whether Mapped::open reads every part through to check it.

Constants§

TRIM_RUN
The most records a variant of this crate compares for one query between two calls of HitList::trim, and so the most hits it pushes between them: a scan cuts each tile into runs of this many. Small beside a tile, so a dense tile can’t swell a list, and large enough that the calls cost nothing beside the comparisons.

Traits§

Search
A searchable set of records: one of the crate’s variants, or one of your own.
Value
A fixed-width value to search: u64 for 64 bits, or [u64; 4] for 256 bits.

Functions§

chunk_radii
The radius to probe each chunk with so that a search finds every value within radius, for chunks chunks: None for a chunk that needn’t be probed at all. No chunks give no radii.
guaranteed_radius
The largest distance that chunks chunks, each probed with every key within chunk_radius bits, are sure to find: chunks × (chunk_radius + 1) − 1.
max_hits
The most hits a list that keeps the nearest keep holds at any moment, its capacity included, when the variant filling it pushes at most TRIM_RUN hits between trims, as every variant of this crate does: 2 × keep + 4,096, the length at which it trims, plus one run less one. A list that counts a histogram holds at most max_hits(0).
neighbour_table
Measure the NeighbourTable of index out to max_distance, from sample of its records drawn at random without repeats, from seed, so the same seed draws the same sample. A sample as large as the index takes every record.
probes
How many keys a search probes: for each chunk of chunk_bits bits probed within r bits, the number of keys within r bits of a key, the sum of C(chunk_bits, i) for i from 0 to r. Saturates at u64::MAX for chunks wider than the crate’s tables.
search
Search for one query.
search_batch
Search for many queries in one call: a scan reads each part of the set once for all of them. The results are in the order of the queries.
search_batch_excluding
search_batch, leaving out every record at a position for which exclude is true, as if the set didn’t hold it.

Type Aliases§

Result
The result of a fallible operation in this crate.