Skip to content

Commit

Permalink
perf: introduce size hint for storage key and value (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCroxx authored Sep 9, 2024
1 parent 4d9be75 commit 260ef14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
18 changes: 14 additions & 4 deletions foyer-common/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ impl<T: Send + Sync + 'static + std::hash::Hash + Eq> Key for T {}
impl<T: Send + Sync + 'static> Value for T {}

/// Key trait for the disk cache.
pub trait StorageKey: Key + Serialize + DeserializeOwned {}
impl<T> StorageKey for T where T: Key + Serialize + DeserializeOwned {}
pub trait StorageKey: Key + Serialize + DeserializeOwned + Hint {}
impl<T> StorageKey for T where T: Key + Serialize + DeserializeOwned + Hint {}

/// Value trait for the disk cache.
pub trait StorageValue: Value + 'static + Serialize + DeserializeOwned {}
impl<T> StorageValue for T where T: Value + Serialize + DeserializeOwned {}
pub trait StorageValue: Value + 'static + Serialize + DeserializeOwned + Hint {}
impl<T> StorageValue for T where T: Value + Serialize + DeserializeOwned + Hint {}

/// Hash builder trait.
pub trait HashBuilder: BuildHasher + Send + Sync + 'static {}
impl<T> HashBuilder for T where T: BuildHasher + Send + Sync + 'static {}

/// Hint functions collection.
pub trait Hint {
/// Size hint for serialization.
fn serialized_size_hint(&self) -> usize {
0
}
}

impl<T: Send + Sync + 'static> Hint for T {}
11 changes: 11 additions & 0 deletions foyer-storage/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ impl EntrySerializer {

Ok(KvInfo { key_len, value_len })
}

pub fn size_hint<'a, K, V>(key: &'a K, value: &'a V, compression: &'a Compression) -> usize
where
K: StorageKey,
V: StorageValue,
{
match compression {
Compression::Zstd | Compression::Lz4 => 0,
Compression::None => key.serialized_size_hint() + value.serialized_size_hint(),
}
}
}

#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion foyer-storage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ where

self.inner.write_runtime_handle.spawn(async move {
if force || this.pick(entry.key()) {
let mut buffer = IoBytesMut::new();
let mut buffer =
IoBytesMut::with_capacity(EntrySerializer::size_hint(entry.key(), entry.value(), &compression));
match EntrySerializer::serialize(
entry.key(),
entry.value(),
Expand Down

0 comments on commit 260ef14

Please sign in to comment.