Fix unaligned distance metadata loads [MOD-15303] - #1001
Conversation
Performance benchmark resultsI compared PR #1001 against its exact base:
SummaryI found no meaningful alignment-specific performance regression. The important comparison is:
Unaligned dimensions were therefore not affected more than the aligned controls. The benchmark covered every modulo-4 remainder—not only odd dimensions—because the unaligned condition is Main results
The small overall difference appears in both aligned and unaligned cases. There is no indication that the safe unaligned metadata load added measurable overhead. Outlier follow-upThe first pass showed several apparent 4–5% SSE4 slowdowns. I ran eight additional alternating baseline/PR pairs covering those cases and related controls. Combined across 14 launches per revision:
The initial SSE4 slowdowns did not reproduce. Two tiny scalar SQ8-to-SQ8 functions moved in opposite directions: L2 was approximately 3.4% slower, while inner product was approximately 3.4% faster. Their combined result was 0.06% faster. This occurred identically for aligned and unaligned dimensions. Disassembly showed linked code-placement and instruction-scheduling differences, consistent with tiny-function layout sensitivity rather than alignment-dependent overhead. Benchmark details
PR #1001 also fixes the SQ8-to-FP32 benchmark harness. I applied only that harness correction to the baseline so that both revisions used byte-identical benchmark code. The baseline production distance code remained exactly at the base commit. Conclusion
I do not see a performance reason to block PR #1001. Full methodology, environment details, per-case results, and raw JSON are available in Jira: |
Summary
Root cause
Trailing FP32 metadata follows byte-width vector payloads. For dimensions not divisible by four, casting
payload + dimensiontofloat *creates a misaligned pointer and undefined behavior. The SQ8-FP32 benchmark also supplied its FP32 query and SQ8 storage in reverse order, producing an out-of-bounds read.Impact
Odd-dimensional SQ8/SQ8-FP32 and INT8/UINT8 cosine distance calculations no longer rely on misaligned FP32 loads, including dispatched SIMD implementations and strict-alignment architectures.
Validation
./check-format.shDeterministic alignment regression proof
Built the regression test with Clang 18.1.8 and fatal AddressSanitizer/alignment sanitization:
c724d32b: exited0;SpacesTest.SQ8_FP32_odd_dim_unaligned_metadata_testpassed.dd6879e8, with only the test and safe reference-helper changes applied: exited1.SQ8_FP32_InnerProduct_Impl:The forced address ends in
...691, proving it is not four-byte aligned. The sanitizer stack traces the failure fromSQ8_FP32_InnerProduct_Impldirectly to the odd-dimension regression test.Jira: MOD-15303
Note
Medium Risk
Touches hot-path distance math across many SIMD backends; behavior should be equivalent but incorrect offset math would skew all SQ8/integer cosine results for affected dimensions.
Overview
Fixes undefined behavior when VecSim reads trailing FP32 metadata (SQ8 min/delta/sum, INT8/UINT8 cosine norms) after byte-sized payloads: odd dimensions leave metadata at addresses that are not 4-byte aligned, so
reinterpret_cast<const float *>loads were unsafe.Scalar and SIMD inner product / L2 paths for SQ8↔FP32, SQ8↔SQ8, and INT8/UINT8 cosine now use
load_unalignedwith byte offsets (sq8::* * sizeof(float)). Integer normalization writes the stored norm viamemcpyinstead of a misaligned float store. Reference helpers, unit tests, and the SQ8-FP32 benchmark follow the same pattern; the benchmark also swaps storage vs query to match API argument order and fixesdelete[].Adds
SQ8_FP32_odd_dim_unaligned_metadata_testto exercise deliberately misaligned storage blobs against scalar and dispatched kernels.Reviewed by Cursor Bugbot for commit c724d32. Bugbot is set up for automated code reviews on this repo. Configure here.