RinhaDeBackend::References
Reference dataset for KNN/IVF. Stored densely in a single binary file produced once at Docker build time and mmapped read-only at runtime.
Two on-disk slice layouts are kept, both read by the same References
instance depending on which loader was used:
-
Row-major (
vectorsslice,block_count == 0) — produced by the legacyload_from_ioJSON loader and used by spec fixtures. Each row isDIMS = 16Int16 lanes (14 logical + 2 zero pad). Hot path is unchanged from RNH5/RNH6. -
AOSOA-8 dim-interleaved blocks (
blocksslice,block_count > 0) — produced byIvfBuilderand consumed by the runtime IVF kernel. Each block holds 8 vectors with their dims interleaved: lanes[d0_v0..d0_v7, d1_v0..d1_v7, ..., d13_v0..d13_v7], totalLOGICAL_DIMS × SLOTS_PER_BLOCK = 112Int16 lanes = 224 B per block. The runtime scan loads 8 i16 of one dim with a single VPMOVSXWD / VPSUBD / VPMULLD / VPADDQ chain, producing 8 squared distances per dim instead of one per row. Multi-stage early exit (between dim 4 and dim 8) skips a whole block of 8 vectors when every lane already exceeds the current top-5 worst.
Common (both layouts):
labels: Slice(UInt8). Row-major: length = padded_count, one per row. Block: length = block_count * SLOTS_PER_BLOCK = padded_count, one per slot, in block-major slot order (block 0 slots 0..7, block 1 slots 0..7, ...).centroids: Slice(Int16), length = k * DIMS, the IVF cell centers (quantized, 14+2 zero-pad row stride — keeps centroid scan on the VPMADDWD path).cell_offsets: Slice(UInt32), length = k + 1. Row layout: row index. Block layout: block index (always even, so cell starts land on a 64 B boundary). Cell c spans[cell_offsets[c], cell_offsets[c+1])in whichever unit applies.cell_radius: Slice(UInt32), length = k; max non-squared L2 distance from quantized centroidcto any vector in cellc(ceil). Computed over real rows only (pad slots/rows excluded — sentinel would torpedo every triangle-inequality prune).max_cell_radius: max overcell_radius. Cached in the header so the runtime can use it for a global outer-break check without scanning the per-cell array.bbox_min/bbox_max: Slice(Int16), length = k * DIMS. Per-cell axis-aligned bounding box per dimension (pad lanes pinned to 0). Used at query time for an exact cell-pruning check tighter than triangle- inequality.
Floats are quantized as (v * 10_000).round.to_i16. The -1
sentinel for indices 5/6 (no last_transaction) maps to -10_000,
naturally outside the [0, 10_000] band.
Binary file format (little-endian, x86_64) — bumped to RNH7 with AOSOA-8 dim-interleaved blocks:
bytes 0..3 : magic "RNH7" (was RNH6 row-major stride-16) bytes 4..7 : count u32 (real, unpadded — for stats) bytes 8..11 : dims u32 (= 16, kept for centroid/bbox stride) bytes 12..15 : k u32 (number of IVF cells) bytes 16..19 : max_cell_radius u32 bytes 20..23 : padded_count u32 (= block_count * SLOTS_PER_BLOCK) bytes 24..27 : block_count u32 (total blocks, including alignment pads) bytes 28..63 : reserved (zeroed) bytes 64.. : blocks (block_count * BLOCK_LANES * Int16, dim- interleaved, each cell starts at an even block index = 64 B aligned) then : labels (block_count * SLOTS_PER_BLOCK * UInt8, slot-major; pad slots carry label 0) then : centroids (k * dims * Int16, stride 16) then : cell_offsets ((k + 1) * UInt32, block indices) then : cell_radius (k * UInt32) then : bbox_min (k * dims * Int16, stride 16) then : bbox_max (k * dims * Int16, stride 16)
Constants
Row stride in Int16 lanes for centroids and bbox (kept at 16 so the centroid scan keeps emitting one VPMADDWD ymm per centroid). The 14 logical feature dimensions live at indices 0..13; indices 14..15 are always zero.
Number of logical (non-pad) feature dims. Block dim-interleaved layout uses exactly LOGICAL_DIMS dims (no pad lanes — the AOSOA-8 kernel processes one dim at a time so unused lanes would be wasted work).
AOSOA-8 block: 8 vectors per block, dim-interleaved. One block spans LOGICAL_DIMS * SLOTS_PER_BLOCK = 112 Int16 lanes = 224 B.
Constructors
Legacy gzip+JSON loader. Used by preprocess and by tests
against example-references.json.
Stream-parse the JSON array from io into pre-allocated slices.
Each row is laid out at stride DIMS = 16 with the 14 logical
feature lanes followed by 2 zero-pad lanes (left at 0 by the
zeroed slice initializer, never written here).
Class methods
Builds the binary file by parsing JSON, running k-means and
writing all sections in order. output_io must be seekable.
Returns the number of records written.
Instance methods
Number of blocks, total (including alignment pads inserted to keep each cell's start at an even block index). 0 on the legacy row-major path.
AOSOA-8 dim-interleaved blocks, populated only by the IVF mmap path. Empty on the legacy JSON loader path.
T1.5 diagnostic: locate the references.bin region in
/proc/self/smaps and dump the lines that tell us whether THP
actually backs the mapping (AnonHugePages, FilePmdMapped,
THPeligible) plus prefault evidence (Rss) and the kernel's
advice flags (VmFlags hg). One-shot at boot, off the hot path.
No-op on macOS dev boxes (no /proc) and on non-mmap loaders.
Total slot count exposed by labels and (in row layout) vectors.
Row layout: padded_count rows in vectors. Block layout:
block_count * SLOTS_PER_BLOCK slots, in slot-major order across
labels. Pad slots/rows carry IvfBuilder::PAD_SENTINEL per lane
(or label 0) and are guaranteed never to enter the top-5 ranking.
Walk the mmapped region, reading one byte every 4 KiB to force the kernel to populate any pages that haven't been brought in yet (and give MADV_HUGEPAGE an opportunity to fold them into 2 MiB pages). MAP_POPULATE already prefaults at mmap time, but a manual touch post-madvise is the canonical way to nudge khugepaged into action on file-backed mappings.
No-op on non-mmap loads (load, load_from_io).
Row-major vectors slice, populated only by load_from_io (legacy
JSON path, fixture tests). Empty Slice(Int16).new(0, 0_i16) on
the IVF mmap path — use blocks instead.