Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions turbopack/crates/turbo-persistence/src/arc_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ impl ArcBytes {
pub fn is_mmap_backed(&self) -> bool {
matches!(self.backing, Backing::Mmap { .. })
}

/// Returns `true` if the backing storage is shared (i.e., there are other
/// references to the same `Arc` allocation). Mmap-backed bytes are always
/// considered unshared since they are cheap to keep and re-create.
pub fn is_shared(&self) -> bool {
match &self.backing {
Backing::Arc { _backing } => Arc::strong_count(_backing) > 1,
Backing::Mmap { .. } => false,
}
}
}

impl SharedBytes for ArcBytes {
Expand Down
3 changes: 2 additions & 1 deletion turbopack/crates/turbo-persistence/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub use key::{KeyBase, QueryKey, StoreKey, hash_key};
pub use meta_file::MetaEntryFlags;
pub use parallel_scheduler::{ParallelScheduler, SerialScheduler};
pub use static_sorted_file::{
BlockCache, BlockWeighter, SstLookupResult, StaticSortedFile, StaticSortedFileMetaData,
BlockCache, BlockCacheLifecycle, BlockWeighter, SstLookupResult, StaticSortedFile,
StaticSortedFileMetaData,
};
pub use static_sorted_file_builder::{
BLOCK_HEADER_SIZE, Entry, EntryValue, StreamingSstWriter, write_static_stored_file,
Expand Down
31 changes: 28 additions & 3 deletions turbopack/crates/turbo-persistence/src/static_sorted_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{

use anyhow::{Context, Result, bail, ensure};
use memmap2::Mmap;
use quick_cache::sync::GuardResult;
use quick_cache::{Lifecycle, sync::GuardResult};
use rustc_hash::FxHasher;
use smallvec::SmallVec;

Expand Down Expand Up @@ -100,8 +100,33 @@ impl quick_cache::Weighter<(u32, u16), ArcBytes> for BlockWeighter {
}
}

pub type BlockCache =
quick_cache::sync::Cache<(u32, u16), ArcBytes, BlockWeighter, BuildHasherDefault<FxHasher>>;
/// Lifecycle hooks for the block cache that prevent eviction of entries
/// still referenced outside the cache (i.e., with `Arc` strong count > 1).
#[derive(Clone, Default)]
pub struct BlockCacheLifecycle;

impl Lifecycle<(u32, u16), ArcBytes> for BlockCacheLifecycle {
type RequestState = ();

#[inline]
fn is_pinned(&self, _key: &(u32, u16), val: &ArcBytes) -> bool {
val.is_shared()
}

#[inline]
fn begin_request(&self) -> Self::RequestState {}

#[inline]
fn on_evict(&self, _state: &mut Self::RequestState, _key: (u32, u16), _val: ArcBytes) {}
}

pub type BlockCache = quick_cache::sync::Cache<
(u32, u16),
ArcBytes,
BlockWeighter,
BuildHasherDefault<FxHasher>,
BlockCacheLifecycle,
>;

/// Trait abstracting value block reading for `handle_key_match_generic`.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1224,25 +1224,17 @@ impl<W: Write> IndexBlockBuilder<W> {

#[cfg(test)]
mod tests {
use std::hash::BuildHasherDefault;

use quick_cache::sync::Cache;
use rustc_hash::FxHasher;

use super::*;
use crate::{
key::hash_key,
lookup_entry::LookupValue,
static_sorted_file::{
BlockWeighter, SstLookupResult, StaticSortedFile, StaticSortedFileMetaData,
BlockCache, SstLookupResult, StaticSortedFile, StaticSortedFileMetaData,
},
};

type TestBlockCache =
Cache<(u32, u16), crate::ArcBytes, BlockWeighter, BuildHasherDefault<FxHasher>>;

fn make_cache() -> TestBlockCache {
TestBlockCache::with(
fn make_cache() -> BlockCache {
BlockCache::with(
100,
4 * 1024 * 1024,
Default::default(),
Expand Down Expand Up @@ -1385,8 +1377,8 @@ mod tests {
fn assert_lookup(
sst: &StaticSortedFile,
entry: &TestEntry,
kc: &TestBlockCache,
vc: &TestBlockCache,
kc: &BlockCache,
vc: &BlockCache,
) -> Result<()> {
let result = sst.lookup::<_, false>(entry.hash, &entry.key, kc, vc)?;
match (&entry.value_kind, result) {
Expand Down
Loading