Remove unsafe code from compute_vec_l2sq#1094
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1094 +/- ##
==========================================
+ Coverage 89.45% 89.48% +0.03%
==========================================
Files 458 474 +16
Lines 85398 89753 +4355
==========================================
+ Hits 76392 80317 +3925
- Misses 9006 9436 +430
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
hildebrandmw
approved these changes
May 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors compute_vec_l2sq/compute_vecs_l2sq in diskann-disk to remove unsafe slice construction by switching from manual pointer/index arithmetic to safe slice chunk iterators (sequential and Rayon-parallel), while preserving performance characteristics.
Changes:
- Refactors
compute_vec_l2sqto accept a&[f32]vector slice directly (removingunsafe { from_raw_parts(...) }). - Updates
compute_vecs_l2sqto drive computation viachunks_exact(dim)andpar_chunks_exact(dim)zipped with the output buffer.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
harsha-simhadri
approved these changes
May 21, 2026
hildebrandmw
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Removes the
unsafe { std::slice::from_raw_parts(...) }call fromcompute_vec_l2sqby replacing manual index arithmetic withchunks_exact/par_chunks_exactiterators zipped with the output slice.Changes
compute_vec_l2sq: Now takes &[f32] directly instead of (data, index, dim). No more unsafe pointer arithmetic.compute_vecs_l2sq: Uses zip(data.chunks_exact(dim)) (sequential path) and zip(data.par_chunks_exact(dim)) (parallel path) to feed slices into compute_vec_l2sq. Eliminates enumerate() and manual offset calculation.Benchmark Results
Benchmarks are flat or within noise threshold.
iai-callgrind (snrm2_benchmark_rust_iai — directly benchmarks compute_vecs_l2sq) shows small perf improvement (# of CPU instructions reduced by 2%):
Criterion (Snrm2 Rust Run): No statistically significant change (p > 0.05 in both before/after runs):