Unverified Commit 3b5d5b82 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: reuse the task ids freed by GC, after a deferral window

The motivation is density, not exhaustion: the persistent id space is ~1.07B
and real sessions stay under 15M, but keeping the live id set packed near
[1, max_live) is what makes flat, array-backed storage viable in place of a
sharded DashMap. The metric is peak minted id vs. peak live task count.

Ids are not handed back the moment they are freed. A stale reference that
outlived its task currently fails loudly — open_task with MustExist asserts
the task "exists in neither memory nor persistent storage" — and immediate
reuse would turn that panic into a silently wrong answer against an
unrelated live task. While an id waits in the deferral queue it is in
neither the resident map, the task cache, nor on disk, so that assert still
fires. The window preserves the existing loud failure rather than merely
making aliasing unlikely.

The window is counted in snapshot cycles because the hazard is
phase-relative, not volume-relative; a quiet session simply doesn't reuse.
Ids enter the queue from evict_after_snapshot only after the deferred
task_cache drain, since an id whose task_cache entry survives could still be
handed back by get_or_create_persistent_task.

Configurable via BackendOptions::id_reuse_delay_cycles or
TURBO_ENGINE_ID_REUSE_DELAY_CYCLES; 0 releases on the next cycle, which is
the maximum-aliasing-pressure setting for tests.

Generation-tagged ids were considered as a stronger alternative and
rejected: they give every task a masked in-memory identity and an unmasked
on-disk key, and kv_backing_storage converts ids to key bytes by bare deref
in several places, so a missed mask is a silent wrong-row read. The
reasoning is recorded in the id_reuse module docs.
parent 24258f0c
......@@ -190,7 +190,6 @@ impl TurboTasksBackend {
let aged_out = self.gc_roots_refresh_and_age_out(&mut roots, now);
let aged_out_count = aged_out.len();
// TODO(perf): recycle the task ids of collected tasks.
let budget = if interruptible {
Some(GcBudget {
phase,
......
//! Deferred reuse of the task ids freed by GC.
//!
//! # Why reuse ids at all
//!
//! Not exhaustion — the persistent id space is ~1.07B and real sessions stay well under 15M. The
//! goal is **density**: keeping the live id set packed near `[1, max_live)` is what makes flat,
//! array-backed storage viable in place of a sharded `DashMap<TaskId, _>`. So the metric that
//! matters is peak `next_free_task_id` against peak live task count, not the reuse count itself.
//!
//! # Why the ids are held back
//!
//! Handing a freed id straight back to [`IdFactoryWithReuse`] would make a stale reference — one
//! that outlived the task it names — silently resolve to an unrelated live task. Today such a
//! reference fails loudly instead: `open_task` with `TaskAccess::MustExist` asserts that the task
//! "exists in neither memory nor persistent storage". Reuse is what would turn that panic into a
//! wrong answer.
//!
//! While an id sits in this queue it is in neither the resident map, the task cache, nor on disk,
//! so a stale reference still hits that assert. The queue is therefore not merely probabilistic
//! hardening: it *preserves the existing loud failure* for the whole deferral window. A bug in the
//! reuse pipeline shows up as the same panic it does today, just later.
//!
//! The window is measured in snapshot cycles rather than queue depth because the hazard is
//! phase-relative (a task is freed as part of one cycle's eviction), not volume-relative. A quiet
//! session simply doesn't reuse, which costs nothing but a little density.
//!
//! # Alternative considered and rejected: generation-tagged ids
//!
//! Stealing 2 bits of [`TaskId`] for a generation counter, bumped on each reuse, would make a
//! stale reference resolve to a *different* id and so fail loudly forever rather than for a
//! window. It was rejected because it gives every task two identities — a masked in-memory id and
//! an unmasked on-disk key — and `kv_backing_storage` converts ids to key bytes by bare deref in
//! several places (`IntKey::new(*task_id)`, `(*task_id).to_le_bytes()`), plus
//! `max_new_task_id.max(*task_id)`, which would compare generation bits as magnitude. Any missed
//! mask is a silent wrong-row read. It would also change the on-disk key format, requiring a
//! `db_versioning` bump. Revisit only if deferral proves insufficient in practice.
use std::collections::VecDeque;
use parking_lot::Mutex;
use turbo_tasks::TaskId;
/// How many snapshot cycles a freed id waits before it may be handed out again.
///
/// One cycle would already cover the eviction that freed it; the extra margin covers a stale
/// reference held across a cycle boundary. Overridable per-backend for tests.
pub(crate) const DEFAULT_ID_REUSE_DELAY_CYCLES: u32 = 2;
/// Freed task ids waiting out their deferral window, oldest first.
///
/// Ids enter from
/// [`Storage::evict_after_snapshot`](crate::backend::storage::Storage::evict_after_snapshot) once a
/// GC-deleted task has been erased from both the resident map and the task cache, and leave
/// once `delay_cycles` snapshot cycles have elapsed.
pub(crate) struct DeferredIdReuse {
/// `(id, cycle it was freed in)`, pushed in cycle order so the front is always the oldest.
queue: Mutex<VecDeque<(TaskId, u32)>>,
/// Monotonic count of completed snapshot cycles. Only ever advanced by
/// [`Self::advance_cycle`].
cycle: Mutex<u32>,
/// Cycles an id must wait. `0` releases immediately, which is what the aliasing-pressure tests
/// want.
delay_cycles: u32,
}
impl DeferredIdReuse {
pub(crate) fn new(delay_cycles: u32) -> Self {
Self {
queue: Mutex::new(VecDeque::new()),
cycle: Mutex::new(0),
delay_cycles,
}
}
/// Records ids freed by the eviction sweep of the current cycle.
///
/// The caller must have already removed each id from the resident map **and** the task cache;
/// an id whose `task_cache` entry survives could still be handed back by
/// `get_or_create_persistent_task`.
pub(crate) fn defer(&self, ids: impl IntoIterator<Item = TaskId>) {
let cycle = *self.cycle.lock();
let mut queue = self.queue.lock();
queue.extend(ids.into_iter().map(|id| (id, cycle)));
}
/// Ends the current snapshot cycle and returns the ids whose window has now elapsed.
///
/// Returned ids are safe to hand to [`IdFactoryWithReuse::reuse`].
pub(crate) fn advance_cycle(&self) -> Vec<TaskId> {
let cycle = {
let mut cycle = self.cycle.lock();
*cycle = cycle.saturating_add(1);
*cycle
};
let mut queue = self.queue.lock();
let mut released = Vec::new();
// The queue is in cycle order, so the first entry that is too young ends the scan.
while let Some(&(id, freed_at)) = queue.front() {
if cycle.saturating_sub(freed_at) < self.delay_cycles {
break;
}
queue.pop_front();
released.push(id);
}
released
}
/// Number of ids currently waiting out their window. Observability only.
pub(crate) fn pending(&self) -> usize {
self.queue.lock().len()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn id(n: u32) -> TaskId {
TaskId::try_from(n).unwrap()
}
#[test]
fn releases_after_the_delay() {
let q = DeferredIdReuse::new(2);
q.defer([id(1), id(2)]);
assert!(q.advance_cycle().is_empty(), "one cycle is not enough");
assert_eq!(q.pending(), 2);
assert_eq!(q.advance_cycle(), vec![id(1), id(2)]);
assert_eq!(q.pending(), 0);
}
#[test]
fn zero_delay_releases_on_the_next_cycle() {
// The aliasing-pressure configuration: maximum reuse, minimum window.
let q = DeferredIdReuse::new(0);
q.defer([id(7)]);
assert_eq!(q.advance_cycle(), vec![id(7)]);
}
#[test]
fn ids_freed_in_later_cycles_wait_their_own_window() {
let q = DeferredIdReuse::new(1);
q.defer([id(1)]);
assert_eq!(q.advance_cycle(), vec![id(1)]);
// Deferred during cycle 1, so it must not come back until cycle 2.
q.defer([id(2)]);
assert_eq!(q.advance_cycle(), vec![id(2)]);
}
#[test]
fn a_quiet_cycle_releases_nothing() {
let q = DeferredIdReuse::new(1);
assert!(q.advance_cycle().is_empty());
assert_eq!(q.pending(), 0);
}
#[test]
fn mixed_ages_release_in_order_without_stranding_the_tail() {
// Regression guard for the early-break scan: a young entry at the front must not hide
// older ones behind it, and must not be released with them either.
let q = DeferredIdReuse::new(2);
q.defer([id(1)]);
q.advance_cycle();
q.defer([id(2)]);
// cycle 2: id(1) is 2 cycles old, id(2) is only 1.
assert_eq!(q.advance_cycle(), vec![id(1)]);
assert_eq!(q.pending(), 1);
assert_eq!(q.advance_cycle(), vec![id(2)]);
}
}
......@@ -2,6 +2,7 @@ mod cell_data;
mod counter_map;
mod eviction;
mod gc;
mod id_reuse;
mod operation;
mod snapshot_coordinator;
mod storage;
......@@ -27,6 +28,7 @@ use auto_hash_map::{AutoMap, AutoSet};
use gc::DEFAULT_GC_ROOT_TTL;
pub use gc::{GcStats, TtlCounter};
use hashbrown::hash_table::Entry;
use id_reuse::{DEFAULT_ID_REUSE_DELAY_CYCLES, DeferredIdReuse};
use indexmap::IndexSet;
use parking_lot::{Mutex, RwLock};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
......@@ -169,6 +171,11 @@ pub struct BackendOptions {
/// How long a GC pass runs before it will honour an interrupt. `None` (default) uses
/// [`GC_MIN_PROGRESS`].
pub gc_min_progress: Option<Duration>,
/// How many snapshot cycles a GC-freed task id waits before it may be reused. `None`
/// (default) uses [`DEFAULT_ID_REUSE_DELAY_CYCLES`]. `0` reuses as soon as the next cycle
/// ends, which is what tests use to maximize aliasing pressure.
pub id_reuse_delay_cycles: Option<u32>,
}
impl Default for BackendOptions {
......@@ -181,8 +188,9 @@ impl Default for BackendOptions {
small_preallocation: false,
eviction_mode: EvictionMode::Off,
gc: None,
gc_root_ttl: None,
gc_min_progress: None,
gc_root_ttl: None,
id_reuse_delay_cycles: None,
}
}
}
......@@ -231,6 +239,9 @@ pub struct TurboTasksBackend {
start_time: Instant,
persisted_task_id_factory: IdFactoryWithReuse<TaskId>,
/// Task ids freed by GC that are waiting out their deferral window before being handed back to
/// `persisted_task_id_factory`. See [`DeferredIdReuse`].
deferred_id_reuse: DeferredIdReuse,
transient_task_id_factory: IdFactoryWithReuse<TaskId>,
storage: Storage,
......@@ -314,6 +325,9 @@ impl TurboTasksBackend {
let small_preallocation = options.small_preallocation;
let gc_root_ttl = options.gc_root_ttl.unwrap_or(DEFAULT_GC_ROOT_TTL);
let gc_min_progress = options.gc_min_progress.unwrap_or(GC_MIN_PROGRESS);
let id_reuse_delay_cycles = options
.id_reuse_delay_cycles
.unwrap_or(DEFAULT_ID_REUSE_DELAY_CYCLES);
let next_task_id = backing_storage
.next_free_task_id()
.expect("Failed to get task id");
......@@ -335,6 +349,7 @@ impl TurboTasksBackend {
options,
gc_enabled,
start_time: Instant::now(),
deferred_id_reuse: DeferredIdReuse::new(id_reuse_delay_cycles),
persisted_task_id_factory: IdFactoryWithReuse::new(
next_task_id,
TaskId::try_from(TRANSIENT_TASK_BIT - 1).unwrap(),
......@@ -410,6 +425,38 @@ impl TurboTasksBackend {
)
}
/// Hands the ids an eviction sweep freed to the deferral queue, ends the cycle, and returns
/// whatever has now waited long enough to the id factory.
///
/// # Safety of the `reuse` calls
///
/// Each id released here named a GC-deleted task that `evict_after_snapshot` erased from the
/// resident map and the task cache, whose tombstone was committed by the snapshot that ran
/// immediately before, and which has since sat out its deferral window. Nothing can resolve it
/// any more, which is what [`IdFactoryWithReuse::reuse`] requires.
fn recycle_freed_task_ids(&self, freed: Vec<TaskId>) {
let freed_count = freed.len();
self.deferred_id_reuse.defer(freed);
let released = self.deferred_id_reuse.advance_cycle();
// The headline number for this feature is density: peak allocated id vs. peak live tasks.
// A ratio that climbs over a long session means ids are not coming back.
tracing::trace!(
target: "turbo_tasks_backend::id_reuse",
freed = freed_count,
released = released.len(),
pending = self.deferred_id_reuse.pending(),
resident = self.storage.resident_persistent_task_count_for_testing(),
);
for id in released {
debug_assert!(
!id.is_transient(),
"only persistent task ids are collected by GC"
);
// SAFETY: see the doc comment above.
unsafe { self.persisted_task_id_factory.reuse(id) };
}
}
/// Perform a snapshot and then evict all evictable tasks from memory.
///
/// This is exposed for integration tests that need to verify the
......@@ -437,7 +484,8 @@ impl TurboTasksBackend {
return TestSnapshotOutcome::default();
}
};
let eviction_counts = self.storage.evict_after_snapshot(None);
let (eviction_counts, freed_ids) = self.storage.evict_after_snapshot(None);
self.recycle_freed_task_ids(freed_ids);
TestSnapshotOutcome {
had_new_data,
eviction_counts,
......@@ -448,6 +496,18 @@ impl TurboTasksBackend {
/// The number of persistent (non-transient) tasks resident in the map. Test-only hook; see
/// [`Storage::resident_persistent_task_count_for_testing`] for why the metric excludes
/// transient tasks.
#[doc(hidden)]
/// `(pending_deferral, next_fresh_id)`: how many freed ids are waiting out their window, and
/// the next id the factory would mint if its free list were empty. The second is the density
/// watermark — if reuse is working, it stops climbing once a workload reaches steady state.
#[doc(hidden)]
pub fn id_reuse_state_for_testing(&self) -> (usize, u64) {
(
self.deferred_id_reuse.pending(),
self.persisted_task_id_factory.peek_next_fresh(),
)
}
#[doc(hidden)]
pub fn resident_persistent_task_count_for_testing(&self) -> usize {
self.storage.resident_persistent_task_count_for_testing()
......@@ -3255,6 +3315,9 @@ impl TurboTasksBackend {
// memory so racing with execution is as likely to save time as
// cost it.
self.storage.evict_after_snapshot(background_span.id());
let (_counts, freed_ids) =
self.storage.evict_after_snapshot(background_span.id());
self.recycle_freed_task_ids(freed_ids);
true
} else {
false
......
......@@ -633,7 +633,11 @@ impl Storage {
/// - `No`: skip
///
/// Must be called when NOT in snapshot mode (i.e., after `end_snapshot()`).
pub fn evict_after_snapshot(&self, parent_span: Option<Id>) -> EvictionCounts {
/// Returns the eviction counts and the ids of the GC-deleted tasks this sweep erased. Those
/// ids are gone from both the resident map and the task cache, so they are candidates for
/// reuse — see [`DeferredIdReuse`](crate::backend::id_reuse::DeferredIdReuse) for why they are
/// not handed back immediately.
pub fn evict_after_snapshot(&self, parent_span: Option<Id>) -> (EvictionCounts, Vec<TaskId>) {
let span = tracing::trace_span!(
parent: parent_span,
"evict_after_snapshot",
......@@ -647,9 +651,15 @@ impl Storage {
"evict_after_snapshot must not be called during snapshot mode"
);
let counts: Vec<EvictionCounts> = parallel::map_collect(self.map.shards(), |shard| {
type ShardResult = (EvictionCounts, Vec<TaskId>);
let counts: Vec<ShardResult> = parallel::map_collect(self.map.shards(), |shard| {
let mut shard = shard.write();
let mut evicted = EvictionCounts::default();
// Ids of GC-deleted tasks erased below. Collected rather than released inline: an
// id is only safe to reuse once its `task_cache` entry is gone too,
// and a contended removal is deferred to after this shard's lock is
// dropped.
let mut freed_ids: Vec<TaskId> = Vec::new();
// task_cache removals that we couldn't perform inline because the target shard
// was contended. We defer them until after the map shard lock is released to
// avoid a lock cycle with get_or_create_persistent_task, which takes task_cache
......@@ -690,6 +700,7 @@ impl Storage {
&mut deferred_task_cache_removals,
task_type,
);
freed_ids.push(*task_id);
evicted.full += 1;
return false;
}
......@@ -701,8 +712,9 @@ impl Storage {
// re-populated by task_by_type() on the next cache miss.
let task_type = task.get_persistent_task_type().unwrap();
// Only try to acquire the lock, if we cannot just remove at the end
// Because `get_or_create_task` acquires 'task_cache' then `storage.map` and
// we do the opposite we need to be defensive here. Attempting here is just
// Because `get_or_create_task` acquires 'task_cache' then `storage.map`
// and we do the opposite we need to be
// defensive here. Attempting here is just
// an optimization to avoid pushing into `deferred_task_cache_removals`
remove_from_task_cache(
&mut evicted,
......@@ -752,12 +764,15 @@ impl Storage {
evicted.key_evictions += 1;
}
}
evicted
// Only now is every freed id absent from the task cache as well as the map.
(evicted, freed_ids)
});
let mut totals = EvictionCounts::default();
for evicted in counts {
let mut freed_ids = Vec::new();
for (evicted, mut ids) in counts {
totals += evicted;
freed_ids.append(&mut ids);
}
// Shrink task_cache only when we evicted more entries than remain — i.e. the map
// is less than half full. Rehashing each surviving CachedTaskType isn't free, so
......@@ -774,7 +789,7 @@ impl Storage {
}
span.record("counts", tracing::field::display(&totals));
totals
(totals, freed_ids)
}
}
......
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
//! Task ids freed by GC are handed back to the id factory, but only after a deferral window.
//!
//! The point of reuse is **density** — keeping the live id set packed so that flat, array-backed
//! storage stays viable — so these tests assert on the id watermark (`next_fresh`), not on reuse
//! counts. A watermark that keeps climbing while the live task count is flat means ids are leaking
//! out of the pipeline even if some are being recycled.
mod gc_fixture;
mod util;
use std::time::Duration;
use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc, unmark_top_level_task_may_leak_eventually_consistent_state};
use turbo_tasks_backend::{BackendOptions, EvictionMode};
use crate::{
gc_fixture::{Selector, create_selector},
util::create_tt_with_options,
};
#[turbo_tasks::function]
fn reuse_leaf(n: u32) -> Vc<u32> {
Vc::cell(n)
}
/// A root op that allocates `count` distinct new leaf tasks, so a test can force the factory to
/// hand out exactly that many ids.
#[turbo_tasks::function(operation, root)]
async fn allocate_fresh(base: u32, count: u32) -> Result<Vc<u32>> {
let mut sum = 0u32;
for n in base..(base + count) {
sum = sum.wrapping_add(*reuse_leaf(n).await?);
}
Ok(Vc::cell(sum))
}
#[turbo_tasks::function]
async fn reuse_branch(generation: u32) -> Result<Vc<u32>> {
Ok(Vc::cell(1 + *reuse_leaf(generation).await?))
}
/// Reads a branch keyed by the selector's generation. Bumping the generation disconnects the whole
/// previous branch, orphaning it for GC.
#[turbo_tasks::function(operation, root)]
async fn select_generation(selector: ResolvedVc<Selector>) -> Result<Vc<u32>> {
let use_second = *selector.await?.get();
let value = if use_second {
*reuse_branch(2).await?
} else {
*reuse_branch(1).await?
};
Ok(Vc::cell(value))
}
/// Options with GC on and the reuse delay pinned, so a test controls the window explicitly rather
/// than depending on the default or the env var.
fn options(delay_cycles: u32) -> BackendOptions {
BackendOptions {
num_workers: Some(2),
small_preallocation: true,
storage_mode: Some(turbo_tasks_backend::StorageMode::ReadWriteOnShutdown),
eviction_mode: EvictionMode::Full,
gc: Some(true),
gc_root_ttl: Some(Duration::from_secs(3600)),
id_reuse_delay_cycles: Some(delay_cycles),
..Default::default()
}
}
/// With the window at zero, an id freed by GC comes back on the very next cycle and the factory
/// hands it out again instead of minting a fresh one.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn freed_ids_are_reused_after_the_window() {
let (tt, _dir) = create_tt_with_options("gc_id_reuse_reused", options(0));
let tt2 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
unmark_top_level_task_may_leak_eventually_consistent_state();
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let selector = selector_op.read_strongly_consistent().await?;
let output = select_generation(selector_vc);
output.read_strongly_consistent().await?;
// Orphan the first branch and its leaf.
selector.set(true);
output.read_strongly_consistent().await?;
anyhow::Ok(())
})
.await;
result.unwrap();
let (pending_before, watermark_before) = tt2.backend().id_reuse_state_for_testing();
assert_eq!(pending_before, 0, "nothing deferred before the first sweep");
// Collect, tombstone, evict. With delay 0 the freed ids are released by this same cycle.
let collected = tt2.backend().gc_for_testing(&tt2);
assert!(collected > 0, "the orphaned branch should be collected");
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let (pending_after, watermark_after) = tt2.backend().id_reuse_state_for_testing();
assert_eq!(
pending_after, 0,
"with a zero-cycle window every freed id is released immediately"
);
assert_eq!(
watermark_before, watermark_after,
"eviction must not mint ids"
);
// Allocate exactly as many new tasks as GC freed. If reuse works they all come from the free
// list and the watermark — the count of ids ever *minted* — does not move.
let freshly_needed = collected as u32;
let tt3 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
// Distinct arguments, so these are genuinely new tasks the factory must allocate ids for.
allocate_fresh(100, freshly_needed)
.read_strongly_consistent()
.await?;
anyhow::Ok(())
})
.await;
result.unwrap();
let (_, watermark_rebuilt) = tt3.backend().id_reuse_state_for_testing();
// `allocate_fresh` is itself a new task and its id cannot come from the free list (it is
// created before any leaf), so the watermark may advance by at most that one. Every leaf must
// come from the free list.
let minted = watermark_rebuilt - watermark_after;
assert!(
minted <= 1,
"allocating {freshly_needed} tasks after freeing {collected} should draw from the free \
list; the factory minted {minted} fresh ids instead (watermark {watermark_after} -> \
{watermark_rebuilt})"
);
tt.stop_and_wait().await;
}
/// With a non-zero window, a freed id is held back: it is neither reusable nor lost, and the
/// factory mints fresh ids in the meantime rather than aliasing one that may still be referenced.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn freed_ids_wait_out_the_window() {
let (tt, _dir) = create_tt_with_options("gc_id_reuse_deferred", options(5));
let tt2 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
unmark_top_level_task_may_leak_eventually_consistent_state();
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let selector = selector_op.read_strongly_consistent().await?;
let output = select_generation(selector_vc);
output.read_strongly_consistent().await?;
selector.set(true);
output.read_strongly_consistent().await?;
anyhow::Ok(())
})
.await;
result.unwrap();
let collected = tt2.backend().gc_for_testing(&tt2);
assert!(collected > 0, "the orphaned branch should be collected");
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let (pending, _) = tt2.backend().id_reuse_state_for_testing();
assert_eq!(
pending, collected,
"every collected id must be waiting out the window, neither reused nor dropped"
);
// Further cycles with no new garbage must not release them early.
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let (still_pending, _) = tt2.backend().id_reuse_state_for_testing();
assert_eq!(
still_pending, collected,
"a 5-cycle window must not release after 2 cycles"
);
tt.stop_and_wait().await;
}
......@@ -118,5 +118,34 @@ pub fn create_tt_with_gc_min_progress(
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = create_persistence_dir(name);
let tt = open_tt_at_with_gc(dir.path(), 2, Some(true), None, Some(gc_min_progress));
(tt, dir)
}
/// A fresh persistent backend built from caller-supplied [`BackendOptions`], for tests that need
/// to pin an option the helpers above don't expose. The caller owns every option, including `gc`
/// — the other helpers' GC-on default does not apply.
pub fn create_tt_with_options(
name: &str,
options: BackendOptions,
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = create_persistence_dir(name);
let tt = TurboTasks::new(TurboTasksBackend::new(
options,
turbo_tasks_backend::turbo_backing_storage(
dir.path(),
&GitVersionInfo {
describe: "test-unversioned",
dirty: false,
},
BackingStorageOptions {
is_short_session: true,
skip_compaction: true,
..Default::default()
},
)
.unwrap()
.0,
));
(tt, dir)
}
......@@ -164,6 +164,15 @@ where
self.free_ids.pop().unwrap_or_else(|_| self.factory.get())
}
/// The next id this factory would mint from its counter, ignoring the free list.
///
/// A watermark, not an allocation: it is the high-water mark of the id space this factory has
/// consumed, which is what "are ids being reused" is measured against. Only meaningful as an
/// observation — a racing `get` can advance it immediately.
pub fn peek_next_fresh(&self) -> u64 {
self.factory.counter.load(Ordering::Relaxed) + self.factory.id_offset
}
/// Add an id to the free list, allowing it to be re-used on a subsequent call to
/// [`IdFactoryWithReuse::get`].
///
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment