Repository navigation
Move element type from UniversalRead trait to method generics - #8955
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR moves element-type generics off UniversalRead/UniversalWrite into method-level generics (T: bytemuck::Pod), removes UniversalReadFamily, converts TypedStorage to inherent typed methods, updates backend impls (mmap, io_uring, disk_cache) to non-generic impls, and updates all callsites, tests, benchmarks, and API handlers to supply explicit T turbofish (e.g., read::<P, T>, len::, read_multi_iter::<P, T, _>). Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/common/common/src/stored_bitslice.rs (1)
1-6: 💤 Low valueOutdated doc comment references old trait syntax.
The module doc references
UniversalRead<u64>andUniversalWrite<u64>, but after this refactor these traits are no longer generic. Consider updating to reflect the new API.📝 Suggested doc update
-//! Storage-agnostic bitslice backed by any [`UniversalRead<u64>`] / -//! [`UniversalWrite<u64>`] backend. +//! Storage-agnostic bitslice backed by any [`UniversalRead`] / +//! [`UniversalWrite`] backend.Similarly for the struct doc at lines 32-33.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/common/common/src/stored_bitslice.rs` around lines 1 - 6, Update the module and struct docs to use the current, non-generic trait names instead of the old generic `UniversalRead<u64>` and `UniversalWrite<u64>` references: change occurrences mentioning `UniversalRead<u64>` and `UniversalWrite<u64>` to `UniversalRead` and `UniversalWrite`, and ensure the `BitSliceStorage` doc comment describes that it wraps any `UniversalRead` / `UniversalWrite` backend (no generic parameter). Keep wording consistent wherever those traits are documented in this file.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/common/common/src/stored_bitslice.rs`:
- Around line 1-6: Update the module and struct docs to use the current,
non-generic trait names instead of the old generic `UniversalRead<u64>` and
`UniversalWrite<u64>` references: change occurrences mentioning
`UniversalRead<u64>` and `UniversalWrite<u64>` to `UniversalRead` and
`UniversalWrite`, and ensure the `BitSliceStorage` doc comment describes that it
wraps any `UniversalRead` / `UniversalWrite` backend (no generic parameter).
Keep wording consistent wherever those traits are documented in this file.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7bf9d1e2-4f0d-491f-9812-2a880f7c58cc
📒 Files selected for processing (42)
lib/common/common/benches/universal_io.rslib/common/common/src/stored_bitslice.rslib/common/common/src/universal_io/disk_cache/cached_slice.rslib/common/common/src/universal_io/disk_cache/mod.rslib/common/common/src/universal_io/disk_cache/tests.rslib/common/common/src/universal_io/family.rslib/common/common/src/universal_io/io_uring/mod.rslib/common/common/src/universal_io/io_uring/tests.rslib/common/common/src/universal_io/mmap.rslib/common/common/src/universal_io/mod.rslib/common/common/src/universal_io/read.rslib/common/common/src/universal_io/wrappers/buffered_update.rslib/common/common/src/universal_io/wrappers/mod.rslib/common/common/src/universal_io/wrappers/read_only.rslib/common/common/src/universal_io/wrappers/stored_struct.rslib/common/common/src/universal_io/wrappers/typed.rslib/common/common/src/universal_io/wrappers/wrapped_pipeline.rslib/common/common/src/universal_io/write.rslib/gridstore/src/bitmask/gaps.rslib/gridstore/src/bitmask/mod.rslib/gridstore/src/gridstore/view.rslib/gridstore/src/pages.rslib/gridstore/src/tracker.rslib/segment/src/common/buffered_update_bitslice.rslib/segment/src/common/flags/dynamic_stored_flags.rslib/segment/src/id_tracker/immutable_id_tracker/mod.rslib/segment/src/index/field_index/full_text_index/inverted_index/mmap_inverted_index/uio_postings.rslib/segment/src/index/field_index/geo_index/immutable_geo_index.rslib/segment/src/index/field_index/geo_index/mmap_geo_index.rslib/segment/src/index/field_index/numeric_index/mmap_numeric_index.rslib/segment/src/index/field_index/stored_point_to_values.rslib/segment/src/vector_storage/chunked_vectors/chunks.rslib/segment/src/vector_storage/chunked_vectors/read.rslib/segment/src/vector_storage/chunked_vectors/write.rslib/segment/src/vector_storage/dense/dense_vector_storage.rslib/segment/src/vector_storage/dense/immutable_dense_vectors.rslib/segment/src/vector_storage/dense/read_only/chucked_vector_storage.rslib/segment/src/vector_storage/multi_dense/appendable_mmap_multi_dense_vector_storage.rslib/segment/src/vector_storage/multi_dense/read_only/chunked_vector_storage.rslib/segment/src/vector_storage/read_only/mod.rssrc/tonic/api/storage_read_api/helpers.rssrc/tonic/api/storage_read_api/mod.rs
💤 Files with no reviewable changes (3)
- lib/common/common/src/universal_io/wrappers/mod.rs
- lib/common/common/src/universal_io/wrappers/typed.rs
- lib/common/common/src/universal_io/family.rs
| pub trait BitmaskStorage: UniversalWrite {} | ||
| impl<T> BitmaskStorage for T where T: UniversalWrite {} |
There was a problem hiding this comment.
We don't need this trait now.
| pub trait BitmaskStorage: UniversalWrite {} | |
| impl<T> BitmaskStorage for T where T: UniversalWrite {} |
| pub struct TypedStorage<S, T> { | ||
| pub inner: S, | ||
| _phantom: PhantomData<T>, | ||
| } | ||
|
|
||
| impl<S, T> TypedStorage<S, T> { | ||
| /// Approximate RAM usage in bytes. IO-backed storage has no significant | ||
| /// heap allocations; on-disk data is accounted via `files()`. | ||
| pub fn ram_usage_bytes(&self) -> usize { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| impl<S, T> UniversalReadFileOps for TypedStorage<S, T> | ||
| where | ||
| S: UniversalReadFileOps, | ||
| { |
There was a problem hiding this comment.
I'd keep TypedStorage<T>, but make it a 'generic structure' with 'non-generic methods' without implementing UniversalRead trait.
Like here: a76870eb9454#diff-28f9341127cf44ad2ea45a529c522cfcd043f00826820440387a45105fc1b458 (another commit that implements the same idea as this PR)
Why: to specify the type once, not at every .read() callsite.
Some ideas: it should be able to wrap both owned and non-owned UniversalRead implementations. E.g.
// to temporarily "lock" the type within local scope
let by_ref = TypedStorage::<&MmapFile, u32>(&file);
// to store in structures
let owned = TypedStorage::<MmapFile, u32>(file);There was a problem hiding this comment.
it should be able to wrap both owned and non-owned UniversalRead implementations
impl<T: UniversalRead> UniversalRead for &T { ... }?
There was a problem hiding this comment.
impl<T: UniversalRead> UniversalRead for &T { ... }?
Yep, like so.
| pub trait UioDynamicFlags: UniversalWrite + Send + 'static {} | ||
| impl<T> UioDynamicFlags for T where T: UniversalWrite + Send + 'static {} |
There was a problem hiding this comment.
Probably we don't need this too (akin to BitmaskStorage).
| counts_per_hash.ram_usage_bytes() | ||
| + points_map.ram_usage_bytes() | ||
| + points_map_ids.ram_usage_bytes() | ||
| + point_to_values.ram_usage_bytes() |
There was a problem hiding this comment.
Are these changes related to this PR?
(same question to let _ = pairs; // io-backed: no significant heap allocations below)
There was a problem hiding this comment.
Unlike TypedStorage, UniversalRead doesn't have ram_usage_bytes, but TypedStorage hard-coded 0 as RAM usage, so this change is technically correct
Lifts the `T` parameter off `trait UniversalRead<T>` (and the matching `UniversalWrite<T>`) and onto the read/write methods themselves. The `ReadPipeline` associated type becomes a GAT over `T`. With per-method generics, callers that need to read several element types from one storage just write `S: UniversalRead` instead of stacking `UniversalRead<u8> + UniversalRead<Counts> + ...`. Removes the workarounds the old shape required: - `TypedStorage<S, T>` newtype (sole purpose was disambiguating multi-bounds) - `UniversalReadFamily` HKT shim - `StoredGeoMapIndexStorage` four-bound trait alias - `CachedSlice<T>` is now non-generic; `T` moves to `get_range`/`len` No runtime behavior change: alignment in `IoUringRuntime` and `CachedSlice` is preserved because `T` is still known at each call site. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
e63e7bf to
cde0a6f
Compare
| pub struct TypedStorage<S, T> { | ||
| pub inner: S, | ||
| _phantom: PhantomData<T>, | ||
| } | ||
|
|
||
| impl<S, T> TypedStorage<S, T> { | ||
| /// Approximate RAM usage in bytes. IO-backed storage has no significant | ||
| /// heap allocations; on-disk data is accounted via `files()`. | ||
| pub fn ram_usage_bytes(&self) -> usize { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| impl<S, T> UniversalReadFileOps for TypedStorage<S, T> | ||
| where | ||
| S: UniversalReadFileOps, | ||
| { |
| let bytes = self.storage.read::<Random, u8>(ReadRange { | ||
| byte_offset: start_offset as u64, | ||
| length: std::mem::size_of::<Optional<ValuePointer>>() as u64, | ||
| })?; |
There was a problem hiding this comment.
I guess this is refactor for another PR
Reintroduces `TypedStorage<S, T>` as a transparent wrapper around `UniversalRead`/`UniversalWrite` storage that fixes the element type to `T`. With per-method generics on the underlying traits, callers can otherwise read or write any `T` from the same handle; this wrapper binds it at the type level so accidental cross-type access fails to compile. The wrapper exposes inherent typed methods (`read::<P>`, `read_iter`, `write`, `len`, …) that delegate to the inner storage with `T` fixed. It does not implement `UniversalRead`/`UniversalWrite` itself — those are intentionally avoided to prevent the typed binding from being bypassed via the generic trait methods. Restores the wrapping at the previous call sites: `StoredStruct`'s inner storage, `ImmutableIdTracker`'s version mmap, the geo and numeric index storages, the chunked-vectors chunks, and the immutable dense vector storage. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`TypedStorage::len` returns `Result<u64>` (a fallible byte length from the underlying storage), so an `is_empty` companion would also be fallible and offer nothing over `len()? == 0`. Suppress the lint at the impl block, matching how the underlying `UniversalRead::len` is already exempted. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
* derive bytemuck::Pod on `OptionalPointer`, get rid of legacy transmute * remove one layer of Option in `get_raw` * keep `UniversalRead<u8>` bound, do bytemuck conversion at call site (#8953) Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>` trait bounds added in the previous two commits. Reads and writes of `OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` / `bytemuck::bytes_of` on the existing `u8` byte slices instead. This keeps the unsafe-transmute removal but avoids the turbofish noise (`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`, …) that two `T`s on `S` forced everywhere. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> * extra refactoring after #8955 * explicit zeroed instantiation --------- Co-authored-by: Andrey Vasnetsov <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
* Move element type from UniversalRead trait to method generics Lifts the `T` parameter off `trait UniversalRead<T>` (and the matching `UniversalWrite<T>`) and onto the read/write methods themselves. The `ReadPipeline` associated type becomes a GAT over `T`. With per-method generics, callers that need to read several element types from one storage just write `S: UniversalRead` instead of stacking `UniversalRead<u8> + UniversalRead<Counts> + ...`. Removes the workarounds the old shape required: - `TypedStorage<S, T>` newtype (sole purpose was disambiguating multi-bounds) - `UniversalReadFamily` HKT shim - `StoredGeoMapIndexStorage` four-bound trait alias - `CachedSlice<T>` is now non-generic; `T` moves to `get_range`/`len` No runtime behavior change: alignment in `IoUringRuntime` and `CachedSlice` is preserved because `T` is still known at each call site. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Restore TypedStorage as a typed-access fail-safe Reintroduces `TypedStorage<S, T>` as a transparent wrapper around `UniversalRead`/`UniversalWrite` storage that fixes the element type to `T`. With per-method generics on the underlying traits, callers can otherwise read or write any `T` from the same handle; this wrapper binds it at the type level so accidental cross-type access fails to compile. The wrapper exposes inherent typed methods (`read::<P>`, `read_iter`, `write`, `len`, …) that delegate to the inner storage with `T` fixed. It does not implement `UniversalRead`/`UniversalWrite` itself — those are intentionally avoided to prevent the typed binding from being bypassed via the generic trait methods. Restores the wrapping at the previous call sites: `StoredStruct`'s inner storage, `ImmutableIdTracker`'s version mmap, the geo and numeric index storages, the chunked-vectors chunks, and the immutable dense vector storage. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Silence clippy::len_without_is_empty on TypedStorage `TypedStorage::len` returns `Result<u64>` (a fallible byte length from the underlying storage), so an `is_empty` companion would also be fallible and offer nothing over `len()? == 0`. Suppress the lint at the impl block, matching how the underlying `UniversalRead::len` is already exempted. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fmt --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
* derive bytemuck::Pod on `OptionalPointer`, get rid of legacy transmute * remove one layer of Option in `get_raw` * keep `UniversalRead<u8>` bound, do bytemuck conversion at call site (#8953) Drop the `+ UniversalRead<OptionalPointer>` / `+ UniversalWrite<OptionalPointer>` trait bounds added in the previous two commits. Reads and writes of `OptionalPointer` and `TrackerHeader` now go through `bytemuck::from_bytes` / `bytemuck::bytes_of` on the existing `u8` byte slices instead. This keeps the unsafe-transmute removal but avoids the turbofish noise (`UniversalWrite::<u8>::flusher(&self.storage)`, `<S as UniversalRead<u8>>::open`, …) that two `T`s on `S` forced everywhere. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> * extra refactoring after #8955 * explicit zeroed instantiation --------- Co-authored-by: Andrey Vasnetsov <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Move generic from trait into method in Universal IO
Motivation
Currently, we have
trait UniversalRead<T>, which has generic paramTthat devines what type of structures we can read from file.Knowing
Twe can properly allign in memory structures we read, especially in io_uring implementation of UniversalRead.There is, however a problem in practice, that many structures expect different types stored in the same file.
As a result we had to use UniversalRead in a strange ways, like for examples
S: UniversalRead<u8> + UniversalRead<u32>, which forces us to use turbo-fish and is overall inconvenient and ugly.Proposal
Instead of using trait parameter
T, move the parameter to the method level.For example:
would become:
This way we can entirely avoid using trait parameters and use method generics instead.
Plan
Check the scope of refactoring and propose a plan.
If some components become obsolete after refactoring, include them in plan.
--- AI from here ---
Summary
Tofftrait UniversalRead<T>/UniversalWrite<T>and onto the read/write methods themselves;ReadPipelinebecomes a GAT overT.S: UniversalReadinstead of stackingUniversalRead<u8> + UniversalRead<Counts> + ….TypedStorage<S, T>,UniversalReadFamily(HKT shim), theStoredGeoMapIndexStoragefour-bound trait alias, and the<T>parameter onCachedSlice.No runtime behavior change: alignment in
IoUringRuntimeandCachedSliceis preserved becauseTis still known at each call site.Test plan
cargo check --workspace --tests --benchescargo clippy --workspace --tests --benches -- -D warningscargo test -p common --lib universal_io— 17/17 (incl. direct-IO + EINTR alignment-sensitive cases)cargo test -p segment --lib {numeric_index,geo_index,id_tracker,chunked,dense}— 26 + 40 + 37 + 4 + 33 passcargo test -p gridstore— 58 passcargo bench -p common --no-run🤖 Generated with Claude Code