class

RinhaDeBackend::References

Inherits Reference < Object

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:

  1. Row-major (vectors slice, block_count == 0) — produced by the legacy load_from_io JSON loader and used by spec fixtures. Each row is DIMS = 16 Int16 lanes (14 logical + 2 zero pad). Hot path is unchanged from RNH5/RNH6.

  2. AOSOA-8 dim-interleaved blocks (blocks slice, block_count > 0) — produced by IvfBuilder and 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], total LOGICAL_DIMS × SLOTS_PER_BLOCK = 112 Int16 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 centroid c to any vector in cell c (ceil). Computed over real rows only (pad slots/rows excluded — sentinel would torpedo every triangle-inequality prune).
  • max_cell_radius: max over cell_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

BLOCK_BYTES = BLOCK_LANES * (sizeof(Int16))
BLOCK_LANES = LOGICAL_DIMS * SLOTS_PER_BLOCK
DEFAULT_BIN_PATH = "resources/references.bin"
DEFAULT_CAPACITY = 3000000
DEFAULT_PATH = "resources/references.json.gz"
DIMS = 16

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.

HEADER_MAGIC = "RNH7"
HEADER_SIZE = 64
LABEL_FRAUD = 1_u8
LABEL_LEGIT = 0_u8
LOGICAL_DIMS = 14

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).

SCALE = 10000.0
SLOTS_PER_BLOCK = 8

AOSOA-8 block: 8 vectors per block, dim-interleaved. One block spans LOGICAL_DIMS * SLOTS_PER_BLOCK = 112 Int16 lanes = 224 B.

Constructors

load(path : String = DEFAULT_PATH) : References

Legacy gzip+JSON loader. Used by preprocess and by tests against example-references.json.

Source
load_from_io(io : IO, capacity : Int32 = DEFAULT_CAPACITY) : References

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).

Source
mmap(path : String = DEFAULT_BIN_PATH) : References

Mmap a pre-built binary file produced by preprocess.

Source

Class methods

preprocess(json_io : IO, output_io : IO, k : Int32 = IvfBuilder::DEFAULT_K, iterations : Int32 = IvfBuilder::DEFAULT_ITERATIONS) : Int32

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.

Source

Instance methods

bbox_max
Source
bbox_min
Source
block_count

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.

Source
blocks

AOSOA-8 dim-interleaved blocks, populated only by the IVF mmap path. Empty on the legacy JSON loader path.

Source
cell_offsets
Source
cell_radius
Source
centroids
Source
count
Source
labels
Source
log_smaps!

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.

Source
max_cell_radius
Source
padded_count

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.

Source
prefault!

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).

Source
vectors

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.

Source