Skip to content

Commit a76870e

Browse files
committed
claude wip
1 parent cd8f21b commit a76870e

41 files changed

Lines changed: 469 additions & 432 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎lib/common/common/benches/universal_io.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn benches(c: &mut Criterion) {
6161
}
6262
}
6363

64-
fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
64+
fn read_benches<T: bytemuck::Pod, C: UniversalRead>(
6565
c: &mut Criterion,
6666
impl_name: &str, // Corresponds to `C`
6767
elem_size: &str, // Corresponds to `T`
@@ -78,7 +78,7 @@ fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
7878
let storage = C::open(path, options).unwrap();
7979
let len = FILE_SIZE_BYTES / size_of::<T>() as u64;
8080
let mut rng = rand::rng();
81-
assert_eq!(storage.len().unwrap(), len);
81+
assert_eq!(storage.len::<T>().unwrap(), len);
8282

8383
let low_mem = std::env::var_os(LIMIT_MEMORY_ENV_INTERNAL).is_some();
8484

@@ -96,7 +96,7 @@ fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
9696
let mut sum = 0u64;
9797
let offset = rng.random_range(0..len) * size_of::<T>() as u64;
9898
let data = storage
99-
.read::<Random>(ReadRange {
99+
.read::<T, Random>(ReadRange {
100100
byte_offset: offset,
101101
length: 1,
102102
})
@@ -119,7 +119,7 @@ fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
119119
})
120120
.map(|range| ((), range));
121121
storage
122-
.read_batch::<Random, ()>(ranges, |(), chunk| {
122+
.read_batch::<T, Random, ()>(ranges, |(), chunk| {
123123
for &item in bytemuck::cast_slice::<T, u64>(chunk) {
124124
sum = sum.wrapping_add(item);
125125
}
@@ -142,7 +142,7 @@ fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
142142
})
143143
.map(|range| ((), range));
144144
storage
145-
.read_batch::<Sequential, ()>(ranges, |(), chunk| {
145+
.read_batch::<T, Sequential, ()>(ranges, |(), chunk| {
146146
for &item in bytemuck::cast_slice::<T, u64>(chunk) {
147147
sum = sum.wrapping_add(item);
148148
}
@@ -158,7 +158,7 @@ fn read_benches<T: bytemuck::Pod, C: UniversalRead<T>>(
158158
let read_batch_full = || {
159159
let mut sum = 0u64;
160160
storage
161-
.read_batch::<Sequential, ()>(ranges_full_file::<T>(), |(), chunk| {
161+
.read_batch::<T, Sequential, ()>(ranges_full_file::<T>(), |(), chunk| {
162162
for &item in bytemuck::cast_slice::<T, u64>(chunk) {
163163
sum = sum.wrapping_add(item);
164164
}

‎lib/common/common/src/stored_bitslice.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//! Storage-agnostic bitslice backed by any [`UniversalRead<u64>`] /
2-
//! [`UniversalWrite<u64>`] backend.
1+
//! Storage-agnostic bitslice backed by any [`UniversalRead`] /
2+
//! [`UniversalWrite`] backend, fixed to the `u64` element type.
33
//!
44
//! Provides [`BitSliceStorage`], a wrapper that interprets the underlying
55
//! `u64`-element storage as a sequence of bits, supporting both read and write
@@ -15,7 +15,8 @@ use itertools::Itertools;
1515
use crate::bitvec::BitVec;
1616
use crate::generic_consts::Random;
1717
use crate::universal_io::{
18-
Flusher, OpenOptions, ReadRange, Result, UniversalIoError, UniversalRead, UniversalWrite,
18+
Flusher, OpenOptions, ReadRange, Result, TypedStorage, UniversalIoError, UniversalRead,
19+
UniversalWrite,
1920
};
2021

2122
/// Number of bits per `BitStore` element.
@@ -29,21 +30,21 @@ pub type MmapBitSlice = StoredBitSlice<crate::universal_io::MmapFile>;
2930

3031
/// A storage-agnostic bitslice that supports both reading and writing bits.
3132
///
32-
/// Wraps any [`UniversalRead<u64>`] / [`UniversalWrite<u64>`] backend and
33-
/// interprets the underlying `u64` elements as a sequence of bits.
34-
/// Bit-level operations are translated to element-level reads and writes
35-
/// on the backend.
33+
/// Wraps any [`UniversalRead`] / [`UniversalWrite`] backend, fixed to the
34+
/// `u64` element type, and interprets the underlying `u64` elements as a
35+
/// sequence of bits. Bit-level operations are translated to element-level
36+
/// reads and writes on the backend.
3637
#[derive(Debug)]
3738
pub struct StoredBitSlice<S> {
38-
storage: S,
39+
storage: TypedStorage<S, BitStore>,
3940
/// Total number of `BitStore` elements in the underlying storage.
4041
element_len: u64,
4142
}
4243

43-
impl<S: UniversalRead<BitStore>> StoredBitSlice<S> {
44+
impl<S: UniversalRead> StoredBitSlice<S> {
4445
/// Open a bitslice storage from the given path using backend `S`.
4546
pub fn open(path: impl AsRef<Path>, options: OpenOptions) -> Result<Self> {
46-
let storage = S::open(path, options)?;
47+
let storage = TypedStorage::<S, BitStore>::open(path, options)?;
4748
let element_len = storage.len()?;
4849
Ok(Self {
4950
storage,
@@ -171,7 +172,7 @@ impl<S: UniversalRead<BitStore>> StoredBitSlice<S> {
171172
}
172173
}
173174

174-
impl<S: UniversalWrite<u64>> StoredBitSlice<S> {
175+
impl<S: UniversalWrite> StoredBitSlice<S> {
175176
/// Set multiple individual bits in a batch.
176177
///
177178
/// Each `(bit_index, value)` pair sets a single bit. Bits within the same

‎lib/common/common/src/universal_io/disk_cache/cached_slice.rs‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,40 @@ impl<T: bytemuck::Pod> CachedSlice<T> {
4949
/// guarantees correct alignment for any `T`.
5050
pub fn get_range(&self, range: Range<usize>) -> io::Result<Cow<'_, [T]>> {
5151
let t_size = size_of::<T>();
52-
debug_assert!(t_size != 0, "cannot use zero-sized type");
52+
let byte_range = range.start * t_size..range.end * t_size;
53+
self.read_byte_range_as::<T>(byte_range)
54+
}
5355

54-
let total_elements = range.end - range.start;
55-
if total_elements == 0 {
56+
/// Read a byte range and reinterpret it as a slice of `T2`.
57+
///
58+
/// The byte range must be aligned with `size_of::<T2>()`. Single-block
59+
/// reads return a zero-copy borrow when possible; multi-block reads
60+
/// allocate a `Vec<T2>` for correct alignment.
61+
pub(crate) fn read_byte_range_as<T2: bytemuck::Pod>(
62+
&self,
63+
byte_range: Range<usize>,
64+
) -> io::Result<Cow<'_, [T2]>> {
65+
let t2_size = size_of::<T2>();
66+
debug_assert!(t2_size != 0, "cannot use zero-sized type");
67+
debug_assert_eq!(
68+
(byte_range.end - byte_range.start) % t2_size,
69+
0,
70+
"byte range length must be a multiple of size_of::<T2>()",
71+
);
72+
73+
if byte_range.is_empty() {
5674
return Ok(Cow::Borrowed(&[]));
5775
}
5876

59-
let byte_range = range.start * t_size..range.end * t_size;
77+
let total_elements = (byte_range.end - byte_range.start) / t2_size;
6078
let mut blocks_iter = self.blocks_for(byte_range);
6179

6280
// TODO(perf): if blocks are consecutive in the big cache file, we can still return without allocating.
6381
if blocks_iter.len() == 1 {
6482
let req = blocks_iter.next().expect("We just checked len() == 1");
6583
let result = self.controller.get_from_cache(req, |bytes| {
66-
let mut vec_t = vec![T::zeroed(); bytes.len() / t_size];
67-
bytemuck::cast_slice_mut::<T, u8>(&mut vec_t).copy_from_slice(bytes);
84+
let mut vec_t = vec![T2::zeroed(); bytes.len() / t2_size];
85+
bytemuck::cast_slice_mut::<T2, u8>(&mut vec_t).copy_from_slice(bytes);
6886
vec_t
6987
})?;
7088

@@ -74,9 +92,9 @@ impl<T: bytemuck::Pod> CachedSlice<T> {
7492
});
7593
}
7694

77-
// Multi-block: allocate Vec<T> directly for correct alignment.
78-
let mut result = vec![T::zeroed(); total_elements];
79-
let result_bytes = bytemuck::cast_slice_mut::<T, u8>(&mut result);
95+
// Multi-block: allocate Vec<T2> directly for correct alignment.
96+
let mut result = vec![T2::zeroed(); total_elements];
97+
let result_bytes = bytemuck::cast_slice_mut::<T2, u8>(&mut result);
8098
let mut copied = 0;
8199
let mut copy_block = |slice: &[u8]| {
82100
let end = copied + slice.len();
@@ -145,6 +163,10 @@ impl<T: bytemuck::Pod> CachedSlice<T> {
145163
self.len_bytes / size_of::<T>()
146164
}
147165

166+
pub(crate) fn byte_len(&self) -> usize {
167+
self.len_bytes
168+
}
169+
148170
/// Returns the block descriptor for the provided bytes range.
149171
fn blocks_for(&self, bytes_range: Range<usize>) -> impl ExactSizeIterator<Item = BlockRequest> {
150172
debug_assert!(bytes_range.start <= bytes_range.end);

‎lib/common/common/src/universal_io/disk_cache/mod.rs‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ impl<T> UniversalReadFileOps for CachedSlice<T> {
6666
}
6767
}
6868

69-
impl<T> UniversalRead<T> for CachedSlice<T>
69+
impl<T> UniversalRead for CachedSlice<T>
7070
where
7171
T: bytemuck::Pod,
7272
{
73-
type ReadPipeline<'a, Meta>
74-
= DiskCacheReadPipeline<'a, T, Meta>
73+
type ReadPipeline<'a, U, Meta>
74+
= DiskCacheReadPipeline<'a, T, U, Meta>
7575
where
76-
Self: 'a;
76+
Self: 'a,
77+
U: bytemuck::Pod;
7778

7879
fn open(path: impl AsRef<Path>, options: OpenOptions) -> Result<Self> {
7980
let Some(controller) = CacheController::global() else {
@@ -97,18 +98,22 @@ where
9798
Ok(CachedSlice::open(controller, path.as_ref())?)
9899
}
99100

100-
fn read<P: AccessPattern>(&self, range: ReadRange) -> Result<Cow<'_, [T]>> {
101-
let elem_start = usize::try_from(range.byte_offset).expect("range.start is within usize")
102-
/ size_of::<T>();
103-
let elem_length = usize::try_from(range.length).expect("range.length is within usize");
104-
105-
let range = elem_start..elem_start + elem_length;
106-
107-
Ok(self.get_range(range)?)
101+
fn read<U, P>(&self, range: ReadRange) -> Result<Cow<'_, [U]>>
102+
where
103+
U: bytemuck::Pod,
104+
P: AccessPattern,
105+
{
106+
let byte_start = usize::try_from(range.byte_offset).expect("range.start is within usize");
107+
let byte_length =
108+
usize::try_from(range.length).expect("range.length is within usize") * size_of::<U>();
109+
Ok(self.read_byte_range_as::<U>(byte_start..byte_start + byte_length)?)
108110
}
109111

110-
fn len(&self) -> Result<u64> {
111-
Ok(Self::len(self) as u64)
112+
fn len<U>(&self) -> Result<u64>
113+
where
114+
U: bytemuck::Pod,
115+
{
116+
Ok((self.byte_len() / size_of::<U>()) as u64)
112117
}
113118

114119
fn populate(&self) -> Result<()> {
@@ -125,21 +130,28 @@ where
125130
}
126131
}
127132

128-
pub struct DiskCacheReadPipeline<'file, T, Meta>
133+
pub struct DiskCacheReadPipeline<'file, T, U, Meta>
129134
where
130135
T: bytemuck::Pod,
136+
U: bytemuck::Pod,
131137
{
132-
result: Option<(Meta, Cow<'file, [T]>)>,
138+
result: Option<(Meta, Cow<'file, [U]>)>,
139+
_phantom: std::marker::PhantomData<&'file CachedSlice<T>>,
133140
}
134141

135-
impl<'file, T, Meta> UniversalReadPipeline<'file, T, Meta> for DiskCacheReadPipeline<'file, T, Meta>
142+
impl<'file, T, U, Meta> UniversalReadPipeline<'file, U, Meta>
143+
for DiskCacheReadPipeline<'file, T, U, Meta>
136144
where
137145
T: bytemuck::Pod,
146+
U: bytemuck::Pod,
138147
{
139148
type File = CachedSlice<T>;
140149

141150
fn new() -> Result<Self> {
142-
Ok(Self { result: None })
151+
Ok(Self {
152+
result: None,
153+
_phantom: std::marker::PhantomData,
154+
})
143155
}
144156

145157
fn can_schedule(&mut self) -> bool {
@@ -159,11 +171,11 @@ where
159171
return Err(UniversalIoError::QueueIsFull);
160172
}
161173

162-
self.result = Some((meta, file.read::<Random>(range)?));
174+
self.result = Some((meta, file.read::<U, Random>(range)?));
163175
Ok(())
164176
}
165177

166-
fn wait(&mut self) -> Result<Option<(Meta, Cow<'file, [T]>)>> {
178+
fn wait(&mut self) -> Result<Option<(Meta, Cow<'file, [U]>)>> {
167179
Ok(self.result.take())
168180
}
169181
}

‎lib/common/common/src/universal_io/family.rs‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)