Repository navigation
Chunked vectors with UniversalWrite storage - #8233
Conversation
ba78fdd to
ff38e91
Compare
62baa0e to
87e7b74
Compare
UniversalWrite storage
There was a problem hiding this comment.
Actionable comments posted: 5
🧹 Nitpick comments (1)
lib/segment/src/data_types/named_vectors.rs (1)
27-35: Consider consolidating with existingas_vec_refmethod.The new
as_refmethod (lines 27-35) has nearly identical implementation to the existingas_vec_refmethod (lines 89-97). The only difference is the lifetime binding:as_refuses&selfwith inferred lifetime, whileas_vec_refexplicitly binds&'a selfto lifetime'a.If the simpler lifetime semantics of
as_refare sufficient for all use cases, consider deprecating or removingas_vec_refto reduce API surface area. Otherwise, a brief doc comment clarifying when to use each would be helpful.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/data_types/named_vectors.rs` around lines 27 - 35, The new as_ref method duplicates the logic of as_vec_ref for CowMultiVector and should be consolidated: either remove/deprecate as_vec_ref and have callers use as_ref, or keep both but add a clear doc comment on as_ref and as_vec_ref explaining the lifetime difference and intended use; update any call sites to use as_ref (or the chosen canonical method) and mark the other with #[deprecated] if removing is not immediately possible. Ensure references to the types (CowMultiVector, TypedMultiDenseVectorRef) remain consistent and tests/usage compile after the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/quantization/src/encoded_vectors_binary.rs`:
- Around line 879-883: Replace the deprecated transmute_from_u8_to_slice usage:
locate the calls to transmute_from_u8_to_slice (used to build
vector_data_usize_1, vector_data_usize_2 and the other occurrence around the
same block) and replace them with bytemuck::cast_slice(&vector_data_x) (the file
already imports bytemuck::cast_slice at line ~826); remove the
#[expect(deprecated, ...)] annotations and ensure the resulting slices have the
same target element type expected by the surrounding code (cast_slice will
handle the safe reinterpretation).
In `@lib/segment/src/common/operation_error.rs`:
- Around line 167-173: The match on `err` in the `impl From<UniversalIoError>
for OperationError` moves `err` and the arm `UniversalIoError::OutOfBounds { ..
} => OperationError::service_error(err.to_string())` illegally tries to use
`err` after a move; fix by binding the OutOfBounds variant to a name and using
that binding when calling to_string(), e.g. use a pattern like `out @
UniversalIoError::OutOfBounds { .. }` and return
`OperationError::service_error(out.to_string())`; keep the other arms
(`UniversalIoError::Io(io_err)` and `UniversalIoError::Mmap(error)`) as-is so
they continue to convert by value.
In `@lib/segment/src/vector_storage/chunked_vectors.rs`:
- Around line 278-291: get_many_impl currently only bounds-checks chunk indices
and chunk byte extents, but doesn't ensure the requested logical range
(start_key .. start_key + count) is within the stored vector count
(self.status.len); add a guard that computes the end index (use checked_add to
avoid overflow) and returns None if end > self.status.len (or if checked_add
returns None), so any request that extends past the logical length is rejected
before computing element_offset/element_end; update the checks around start_key,
count, and self.status.len in get_many_impl accordingly.
- Around line 123-126: Validate and guard the chunk geometry before computing
chunk_size_vectors: ensure dim > 0 and compute vector_size_bytes =
dim.checked_mul(std::mem::size_of::<T>()).ok_or(...) (or handle) and require
vector_size_bytes <= CHUNK_SIZE; then compute chunk_size_vectors with
checked_div (or chunk_size_bytes / vector_size_bytes after the check) and use
checked_mul for corrected_chunk_size_bytes to avoid overflow. Update the code
paths that use chunk_size_vectors/corrected_chunk_size_bytes to return an error
(or propagate a clear panic) when these validations fail so you never perform a
divide/modulo by zero; refer to the variables chunk_size_bytes,
vector_size_bytes, chunk_size_vectors, corrected_chunk_size_bytes, CHUNK_SIZE,
dim and type T to locate the changes.
In `@lib/segment/src/vector_storage/volatile_chunked_vectors.rs`:
- Around line 169-186: The calculation of last_chunk_idx in
VolatileChunkedVectors<T>::try_set_capacity_exact is off-by-one when capacity is
exactly divisible by chunk_capacity; change the logic so the "last" chunk index
is num_chunks.saturating_sub(1) (or otherwise compute last index as num_chunks -
1 when num_chunks > 0) and handle capacity == 0 gracefully, and set the
desired_capacity for that last chunk using: if capacity % self.chunk_capacity ==
0 then desired_capacity = self.chunk_capacity * self.dim else desired_capacity =
(capacity % self.chunk_capacity) * self.dim; update the loop over chunk_idx to
compare against the corrected last_chunk_idx and ensure chunks are
resized/initialized accordingly in try_set_capacity_exact.
---
Nitpick comments:
In `@lib/segment/src/data_types/named_vectors.rs`:
- Around line 27-35: The new as_ref method duplicates the logic of as_vec_ref
for CowMultiVector and should be consolidated: either remove/deprecate
as_vec_ref and have callers use as_ref, or keep both but add a clear doc comment
on as_ref and as_vec_ref explaining the lifetime difference and intended use;
update any call sites to use as_ref (or the chosen canonical method) and mark
the other with #[deprecated] if removing is not immediately possible. Ensure
references to the types (CowMultiVector, TypedMultiDenseVectorRef) remain
consistent and tests/usage compile after the change.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (42)
lib/common/common/src/mmap/chunked.rslib/common/common/src/mmap/mod.rslib/common/common/src/universal_io/mmap.rslib/common/common/src/universal_io/mod.rslib/gridstore/src/page.rslib/quantization/benches/binary.rslib/quantization/benches/encode.rslib/quantization/src/encoded_storage.rslib/quantization/src/encoded_vectors_binary.rslib/quantization/src/encoded_vectors_pq.rslib/quantization/src/encoded_vectors_u8.rslib/quantization/tests/integration/test_avx2.rslib/quantization/tests/integration/test_neon.rslib/quantization/tests/integration/test_simple.rslib/quantization/tests/integration/test_sse.rslib/segment/src/common/operation_error.rslib/segment/src/data_types/named_vectors.rslib/segment/src/index/hnsw_index/gpu/gpu_vector_storage/gpu_multivectors.rslib/segment/src/index/hnsw_index/gpu/gpu_vector_storage/mod.rslib/segment/src/index/hnsw_index/graph_links.rslib/segment/src/index/hnsw_index/graph_links/serializer.rslib/segment/src/vector_storage/chunked_mmap_vectors.rslib/segment/src/vector_storage/chunked_vectors.rslib/segment/src/vector_storage/dense/appendable_dense_vector_storage.rslib/segment/src/vector_storage/dense/immutable_dense_vectors.rslib/segment/src/vector_storage/dense/memmap_dense_vector_storage.rslib/segment/src/vector_storage/dense/simple_dense_vector_storage.rslib/segment/src/vector_storage/dense/volatile_dense_vector_storage.rslib/segment/src/vector_storage/mod.rslib/segment/src/vector_storage/multi_dense/appendable_mmap_multi_dense_vector_storage.rslib/segment/src/vector_storage/multi_dense/simple_multi_dense_vector_storage.rslib/segment/src/vector_storage/multi_dense/volatile_multi_dense_vector_storage.rslib/segment/src/vector_storage/quantized/quantized_chunked_mmap_storage.rslib/segment/src/vector_storage/quantized/quantized_mmap_storage.rslib/segment/src/vector_storage/quantized/quantized_multivector_storage.rslib/segment/src/vector_storage/quantized/quantized_ram_storage.rslib/segment/src/vector_storage/quantized/quantized_vectors.rslib/segment/src/vector_storage/query_scorer/multi_custom_query_scorer.rslib/segment/src/vector_storage/query_scorer/multi_metric_query_scorer.rslib/segment/src/vector_storage/tests/test_appendable_multi_dense_vector_storage.rslib/segment/src/vector_storage/vector_storage_base.rslib/segment/src/vector_storage/volatile_chunked_vectors.rs
💤 Files with no reviewable changes (3)
- lib/common/common/src/mmap/mod.rs
- lib/common/common/src/mmap/chunked.rs
- lib/segment/src/vector_storage/chunked_mmap_vectors.rs
| pub struct ChunkedVectors<T: Copy + Sized + 'static, S: UniversalWrite<T>> { | ||
| config: ChunkedVectorsConfig, | ||
| status: MmapType<Status>, | ||
| chunks: Vec<S>, | ||
| directory: PathBuf, | ||
| _vector_element_type: PhantomData<T>, | ||
| } |
There was a problem hiding this comment.
Implementation detail: I decided that Status was too small to generalize its storage, so I decided to keep it as mmap always
fix tests
09e034f to
0698c92
Compare
99f380f to
fbed14e
Compare
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
* use CowMultiVector as return type from storages * add advice to OpenOptions * Implement ChunkedVectors with generic storage * rename ChunkedVectors->VolatileChunkedVectors and ChunkedMmapVectors-> ChunkedVectors * propagate everywhere fix tests * [auto] rename BytesRange -> ElementsRange * [auto] rename BytesOffset -> ElementOffset * coderabbit nits --------- Co-authored-by: generall <[email protected]>
* use CowMultiVector as return type from storages * add advice to OpenOptions * Implement ChunkedVectors with generic storage * rename ChunkedVectors->VolatileChunkedVectors and ChunkedMmapVectors-> ChunkedVectors * propagate everywhere fix tests * [auto] rename BytesRange -> ElementsRange * [auto] rename BytesOffset -> ElementOffset * coderabbit nits --------- Co-authored-by: generall <[email protected]>
Uses
UniversalWritefor all appendable chunked storages.What's changed
ChunkedVectors->VolatileChunkedVectors: These were only in-memory anywayChunkedMmapVectors->ChunkedVectors: Now that the storage is generic, we can remove Mmap from the name.ChunkedVectors<T, MmapUniversal<T>>now.Cow<'_, [T]>as return type is propagated a lot further, including quantized & multivector storages.Pro tip: the
ChunkedVectors<T, UniversalWrite<T>>implementation is easier to see in commit cd8e8b4, because of the file renaming that happened afterwords.