#545·turbovec

Parallelize pack::repack (byte-identical, mirrors apply_native_transform)

Author: gburdCreated Sep 26, 2026Updated Sep 26, 2026

pack::repack (row-major → SIMD-blocked) is single-threaded and O(n·dim), so it is a serial floor on the load path that dominates as the corpus grows — every consumer that reconstructs an index from persisted codes pays it once per open. The sibling transform apply_native_transform in the same file is already parallelized with rayon par_chunks_mut above a 4 MiB PAR_THRESHOLD; repack is structurally identical — each output block is a disjoint byte range depending only on its own input rows — so the same pattern applies to the one hot transform that isn't parallel yet.

Proposed approach: parallelize repack over block-aligned ranges above the same threshold, serial below it (and for n_blocks <= 1). Each task computes its block range via the crate-internal repack_block_range and copy_from_slices into a disjoint slot of the pre-allocated output. Because block i's output occupies exactly [i·n_byte_groups·BLOCK, (i+1)·n_byte_groups·BLOCK) and depends only on rows [i·BLOCK, (i+1)·BLOCK), concatenation is byte-identical to the current serial path.

Determinism is the whole risk (a wrong byte is a silently mis-scored index, no crash), so it must be pinned by a test that asserts par == seq byte-for-byte — across the threshold crossing, tail padding (n not a multiple of BLOCK), a partial last task (n not a multiple of BLOCKS_PER_TASK·BLOCK), and 2/3/4-bit.

Measured: 250k × 1024d × 4-bit repack ~6 s → ~250 ms on 8 cores (I'll re-verify the micro-bench on an AVX2 host before quoting it as final). End-to-end, in a downstream PostgreSQL extension (pg_turbovec) that pays this once per backend at cold index-open, a 1M × 1024d flat cold-scan p50 dropped 1766 ms → 566 ms with warm latency unchanged.

rayon is already a dependency (rayon = "1.12"). No public API change; it composes with (but does not require) exposing repack publicly.

Per CONTRIBUTING I'm raising this as an issue first. I have a working, byte-identity-tested implementation and would be happy to take the PR — can I get contributor access for this one? It's the lowest-risk of the changes I'd like to propose (pure perf, byte-identical, mirrors machinery already in pack.rs).