Commit d8bc4cd5 authored by Luke Sandberg's avatar Luke Sandberg

Turbopack: Add task GC infrastructure (Phase 1)

Implement the core garbage collection framework for turbopack tasks.
Tasks become GC-eligible when they have no incoming edges (no dependents,
no activeness state, not executing). The GC processor disconnects eligible
tasks from the aggregation tree, removes outgoing dependency edges, and
cleans up storage.

Key components:
- TaskStorage::is_gc_eligible() checks for zero incoming edges
- GcOperation processes batched GC candidates with aggregation tree cleanup
- GC triggers in decrease_active_count and cleanup_old_edges
- Storage::remove_task/mark_deleted for memory and DB cleanup tracking
- ConcurrentQueue-based deferred GC queue to avoid cascading latency
- IdFactoryWithReuse unit test coverage
parent bf56c712
...@@ -9578,6 +9578,7 @@ dependencies = [ ...@@ -9578,6 +9578,7 @@ dependencies = [
"bitfield", "bitfield",
"byteorder", "byteorder",
"codspeed-criterion-compat", "codspeed-criterion-compat",
"concurrent-queue",
"dashmap 6.1.0", "dashmap 6.1.0",
"either", "either",
"futures", "futures",
......
...@@ -36,6 +36,7 @@ lmdb = ["dep:lmdb-rkv"] ...@@ -36,6 +36,7 @@ lmdb = ["dep:lmdb-rkv"]
anyhow = { workspace = true } anyhow = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
arc-swap = { version = "1.8.2" } arc-swap = { version = "1.8.2" }
concurrent-queue = { workspace = true }
auto-hash-map = { workspace = true } auto-hash-map = { workspace = true }
bitfield = { workspace = true } bitfield = { workspace = true }
byteorder = { workspace = true } byteorder = { workspace = true }
......
...@@ -19,6 +19,7 @@ use std::{ ...@@ -19,6 +19,7 @@ use std::{
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use auto_hash_map::{AutoMap, AutoSet}; use auto_hash_map::{AutoMap, AutoSet};
use concurrent_queue::ConcurrentQueue;
use indexmap::IndexSet; use indexmap::IndexSet;
use parking_lot::{Condvar, Mutex}; use parking_lot::{Condvar, Mutex};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher}; use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
...@@ -212,6 +213,16 @@ struct TurboTasksBackendInner<B: BackingStorage> { ...@@ -212,6 +213,16 @@ struct TurboTasksBackendInner<B: BackingStorage> {
task_statistics: TaskStatisticsApi, task_statistics: TaskStatisticsApi,
/// Queue of task IDs that have been identified as GC-eligible (no incoming edges).
/// Tasks are enqueued when an edge removal makes them eligible, and processed
/// in batches by `GcOperation`.
gc_queue: ConcurrentQueue<TaskId>,
/// Task cache entries pending deletion from the database. Populated by the GC
/// processor when removing tasks from the in-memory cache. Drained during
/// `save_snapshot` to emit database deletion records.
pending_task_cache_deletions: ConcurrentQueue<(Arc<CachedTaskType>, TaskId)>,
backing_storage: B, backing_storage: B,
#[cfg(feature = "verify_aggregation_graph")] #[cfg(feature = "verify_aggregation_graph")]
...@@ -272,6 +283,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -272,6 +283,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
#[cfg(feature = "verify_aggregation_graph")] #[cfg(feature = "verify_aggregation_graph")]
is_idle: AtomicBool::new(false), is_idle: AtomicBool::new(false),
task_statistics: TaskStatisticsApi::default(), task_statistics: TaskStatisticsApi::default(),
gc_queue: ConcurrentQueue::unbounded(),
pending_task_cache_deletions: ConcurrentQueue::unbounded(),
backing_storage, backing_storage,
#[cfg(feature = "verify_aggregation_graph")] #[cfg(feature = "verify_aggregation_graph")]
root_tasks: Default::default(), root_tasks: Default::default(),
...@@ -376,6 +389,38 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -376,6 +389,38 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
.map(|stats| stats.increment_cache_miss(task_type.native_fn)); .map(|stats| stats.increment_cache_miss(task_type.native_fn));
} }
/// Enqueue a task for garbage collection. Called when an edge removal makes
/// a task potentially eligible for GC.
fn enqueue_gc(&self, task_id: TaskId) {
let _ = self.gc_queue.push(task_id);
}
/// Remove a GC'd task from Storage and task_cache. Called by the GC processor
/// after verifying the task has no incoming edges and disconnecting it from
/// the aggregation tree.
fn gc_remove_task(&self, task_id: TaskId) {
// Remove from storage, getting the task data for task_cache cleanup
if let Some(task_storage) = self.storage.remove_task(task_id) {
// Remove from task_cache using the persistent_task_type as key
if let Some(task_type) = task_storage.get_persistent_task_type() {
self.task_cache.remove(task_type.as_ref());
// Queue for database deletion
let _ = self
.pending_task_cache_deletions
.push((task_type.clone(), task_id));
}
// Mark for database deletion (TaskMeta + TaskData key spaces)
if !task_id.is_transient() {
self.storage.mark_deleted(task_id);
}
}
}
/// Returns the number of tasks currently stored in memory.
pub fn get_task_count(&self) -> usize {
self.storage.get_task_count()
}
/// Reconstructs a full [`TurboTasksExecutionError`] from the compact [`TaskError`] storage /// Reconstructs a full [`TurboTasksExecutionError`] from the compact [`TaskError`] storage
/// representation. For [`TaskError::TaskChain`], this looks up the source error from the last /// representation. For [`TaskError::TaskChain`], this looks up the source error from the last
/// task's output and rebuilds the nested `TaskContext` wrappers with `TurboTasksCallApi` /// task's output and rebuilds the nested `TaskContext` wrappers with `TurboTasksCallApi`
......
...@@ -2591,6 +2591,11 @@ impl AggregationUpdateQueue { ...@@ -2591,6 +2591,11 @@ impl AggregationUpdateQueue {
task_ids: followers, task_ids: followers,
}); });
} }
// Activeness removed - task may now be GC-eligible.
// Enqueue for GC check (the GC processor will re-verify all conditions).
if is_empty {
ctx.enqueue_gc(task_id);
}
} else { } else {
drop(task); drop(task);
} }
......
...@@ -164,6 +164,11 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -164,6 +164,11 @@ impl Operation for CleanupOldEdgesOperation {
{ {
let mut task = ctx.task(cell_task_id, TaskDataCategory::Data); let mut task = ctx.task(cell_task_id, TaskDataCategory::Data);
task.remove_cell_dependents(&(cell, key, task_id)); task.remove_cell_dependents(&(cell, key, task_id));
// Cell dependent removed - target may now be GC-eligible
if task.typed().is_gc_eligible() {
drop(task);
ctx.enqueue_gc(cell_task_id);
}
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
...@@ -187,6 +192,11 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -187,6 +192,11 @@ impl Operation for CleanupOldEdgesOperation {
{ {
let mut task = ctx.task(output_task_id, TaskDataCategory::Data); let mut task = ctx.task(output_task_id, TaskDataCategory::Data);
task.remove_output_dependent(&task_id); task.remove_output_dependent(&task_id);
// Output dependent removed - target may now be GC-eligible
if task.typed().is_gc_eligible() {
drop(task);
ctx.enqueue_gc(output_task_id);
}
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
...@@ -204,6 +214,12 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -204,6 +214,12 @@ impl Operation for CleanupOldEdgesOperation {
collectible_type, collectible_type,
task_id, task_id,
)); ));
// Collectibles dependent removed - target may now be
// GC-eligible
if task.typed().is_gc_eligible() {
drop(task);
ctx.enqueue_gc(dependent_task_id);
}
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
......
use bincode::{Decode, Encode};
use smallvec::SmallVec;
use turbo_tasks::TaskId;
use crate::backend::{
TaskDataCategory,
operation::{
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, Operation,
aggregation_update::{
InnerOfUppersLostFollowersJob, get_aggregation_number, get_uppers, is_aggregating_node,
},
},
storage_schema::TaskStorageAccessors,
};
#[derive(Encode, Decode, Clone, Default)]
pub enum GcOperation {
/// Process a batch of task IDs from the GC queue.
ProcessTasks {
task_ids: Vec<TaskId>,
queue: AggregationUpdateQueue,
},
/// Run pending aggregation updates after disconnecting tasks.
AggregationUpdate { queue: AggregationUpdateQueue },
#[default]
Done,
}
impl GcOperation {
pub fn run(task_ids: Vec<TaskId>, ctx: &mut impl ExecuteContext<'_>) {
if task_ids.is_empty() {
return;
}
GcOperation::ProcessTasks {
task_ids,
queue: AggregationUpdateQueue::new(),
}
.execute(ctx);
}
}
impl Operation for GcOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop {
ctx.operation_suspend_point(&self);
match self {
GcOperation::ProcessTasks {
ref mut task_ids,
ref mut queue,
} => {
if let Some(task_id) = task_ids.pop() {
gc_single_task(task_id, queue, ctx);
}
if task_ids.is_empty() {
self = GcOperation::AggregationUpdate {
queue: std::mem::take(queue),
};
}
}
GcOperation::AggregationUpdate { ref mut queue } => {
if queue.process(ctx) {
self = GcOperation::Done;
}
}
GcOperation::Done => {
return;
}
}
}
}
}
/// Process a single task for GC.
///
/// Steps:
/// 1. Re-verify GC eligibility (a new edge may have been added since enqueue)
/// 2. Read aggregation tree state and outgoing edges
/// 3. Disconnect from aggregation tree using existing machinery
/// 4. Remove outgoing dependency edges (may trigger GC of target tasks)
/// 5. Final re-verify (aggregation rebalancing may have added new edges)
/// 6. Remove from task_cache, mark for DB deletion, remove from Storage
fn gc_single_task(
task_id: TaskId,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
) {
// Step 1: Re-verify GC eligibility under lock
let task = ctx.task(task_id, TaskDataCategory::All);
if !task.typed().is_gc_eligible() {
return;
}
// Step 2: Read all state needed for disconnection
let aggregation_number = get_aggregation_number(&task);
let upper_ids = get_uppers(&task);
let follower_ids: SmallVec<[TaskId; 4]> = task
.followers()
.map(|f| f.iter().map(|(&id, _)| id).collect())
.unwrap_or_default();
let children: SmallVec<[TaskId; 4]> = task
.children()
.map(|c| c.iter().copied().collect())
.unwrap_or_default();
// Note: active_counter should be 0 since is_gc_eligible() passed
// (no activeness state), but we check anyway for safety
let has_active_count = ctx.should_track_activeness()
&& task.get_activeness().is_some_and(|a| a.active_counter > 0);
let output_deps: SmallVec<[TaskId; 4]> = task
.output_dependencies()
.map(|d| d.iter().copied().collect())
.unwrap_or_default();
let cell_deps: SmallVec<[(crate::data::CellRef, Option<u64>); 4]> = task
.cell_dependencies()
.map(|d| d.iter().cloned().collect())
.unwrap_or_default();
let collectibles_deps: SmallVec<[crate::data::CollectiblesRef; 4]> = task
.collectibles_dependencies()
.map(|d| d.iter().cloned().collect())
.unwrap_or_default();
// Drop the task lock before modifying other tasks
drop(task);
// Step 3: Disconnect from aggregation tree
// Same pattern as cleanup_old_edges.rs:75-114
if is_aggregating_node(aggregation_number) && !follower_ids.is_empty() {
queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers {
upper_id: task_id,
lost_follower_ids: follower_ids,
retry: 0,
});
}
if !upper_ids.is_empty() {
if has_active_count {
queue.push(AggregationUpdateJob::DecreaseActiveCounts {
task_ids: SmallVec::from_slice(&[task_id]),
});
}
queue.push(
InnerOfUppersLostFollowersJob {
upper_ids,
lost_follower_ids: SmallVec::from_slice(&[task_id]),
}
.into(),
);
}
// Propagate active count decrease to children
if has_active_count && !children.is_empty() {
queue.push(AggregationUpdateJob::DecreaseActiveCounts { task_ids: children });
}
// Step 4: Remove outgoing dependency edges on other tasks
// This may trigger GC eligibility on those tasks
for dep_task_id in output_deps {
let mut dep_task = ctx.task(dep_task_id, TaskDataCategory::Data);
dep_task.remove_output_dependent(&task_id);
if dep_task.typed().is_gc_eligible() {
drop(dep_task);
ctx.enqueue_gc(dep_task_id);
}
}
for (cell_ref, key) in cell_deps {
let mut dep_task = ctx.task(cell_ref.task, TaskDataCategory::Data);
dep_task.remove_cell_dependents(&(cell_ref.cell, key, task_id));
if dep_task.typed().is_gc_eligible() {
drop(dep_task);
ctx.enqueue_gc(cell_ref.task);
}
}
for collectibles_ref in collectibles_deps {
let mut dep_task = ctx.task(collectibles_ref.task, TaskDataCategory::Data);
dep_task.remove_collectibles_dependents(&(collectibles_ref.collectible_type, task_id));
if dep_task.typed().is_gc_eligible() {
drop(dep_task);
ctx.enqueue_gc(collectibles_ref.task);
}
}
// Step 5: Final re-verify under lock
// Aggregation rebalancing (triggered by step 3) may have added new edges
{
let task = ctx.task(task_id, TaskDataCategory::All);
if !task.typed().is_gc_eligible() {
// Something reconnected this task during aggregation processing
return;
}
}
// Step 6: Remove from task_cache, mark for DB deletion, remove from Storage
ctx.gc_remove_task(task_id);
}
...@@ -2,6 +2,7 @@ mod aggregation_update; ...@@ -2,6 +2,7 @@ mod aggregation_update;
mod cleanup_old_edges; mod cleanup_old_edges;
mod connect_child; mod connect_child;
mod connect_children; mod connect_children;
pub(crate) mod gc;
mod invalidate; mod invalidate;
mod leaf_distance_update; mod leaf_distance_update;
mod prepare_new_children; mod prepare_new_children;
...@@ -91,6 +92,13 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -91,6 +92,13 @@ pub trait ExecuteContext<'e>: Sized {
fn suspending_requested(&self) -> bool; fn suspending_requested(&self) -> bool;
fn should_track_dependencies(&self) -> bool; fn should_track_dependencies(&self) -> bool;
fn should_track_activeness(&self) -> bool; fn should_track_activeness(&self) -> bool;
/// Enqueue a task for garbage collection. Called when an edge removal
/// makes a task potentially eligible for GC.
fn enqueue_gc(&self, task_id: TaskId);
/// Remove a GC'd task from Storage and task_cache. Called after verifying
/// the task has no incoming edges and disconnecting it from the aggregation tree.
/// Returns true if the task was successfully removed.
fn gc_remove_task(&self, task_id: TaskId);
fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi>; fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi>;
/// Look up a TaskId from the backing storage for a given task type. /// Look up a TaskId from the backing storage for a given task type.
/// ///
...@@ -631,6 +639,14 @@ where ...@@ -631,6 +639,14 @@ where
self.backend.should_track_activeness() self.backend.should_track_activeness()
} }
fn enqueue_gc(&self, task_id: TaskId) {
self.backend.enqueue_gc(task_id);
}
fn gc_remove_task(&self, task_id: TaskId) {
self.backend.gc_remove_task(task_id);
}
fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi> { fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi> {
self.turbo_tasks.pin() self.turbo_tasks.pin()
} }
...@@ -1112,6 +1128,7 @@ pub enum AnyOperation { ...@@ -1112,6 +1128,7 @@ pub enum AnyOperation {
CleanupOldEdges(cleanup_old_edges::CleanupOldEdgesOperation), CleanupOldEdges(cleanup_old_edges::CleanupOldEdgesOperation),
AggregationUpdate(aggregation_update::AggregationUpdateQueue), AggregationUpdate(aggregation_update::AggregationUpdateQueue),
LeafDistanceUpdate(leaf_distance_update::LeafDistanceUpdateQueue), LeafDistanceUpdate(leaf_distance_update::LeafDistanceUpdateQueue),
Gc(gc::GcOperation),
Nested(Vec<AnyOperation>), Nested(Vec<AnyOperation>),
} }
...@@ -1124,6 +1141,7 @@ impl AnyOperation { ...@@ -1124,6 +1141,7 @@ impl AnyOperation {
AnyOperation::CleanupOldEdges(op) => op.execute(ctx), AnyOperation::CleanupOldEdges(op) => op.execute(ctx),
AnyOperation::AggregationUpdate(op) => op.execute(ctx), AnyOperation::AggregationUpdate(op) => op.execute(ctx),
AnyOperation::LeafDistanceUpdate(op) => op.execute(ctx), AnyOperation::LeafDistanceUpdate(op) => op.execute(ctx),
AnyOperation::Gc(op) => op.execute(ctx),
AnyOperation::Nested(ops) => { AnyOperation::Nested(ops) => {
for op in ops { for op in ops {
op.execute(ctx); op.execute(ctx);
...@@ -1139,6 +1157,25 @@ impl_operation!(UpdateCell update_cell::UpdateCellOperation); ...@@ -1139,6 +1157,25 @@ impl_operation!(UpdateCell update_cell::UpdateCellOperation);
impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation); impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation);
impl_operation!(AggregationUpdate aggregation_update::AggregationUpdateQueue); impl_operation!(AggregationUpdate aggregation_update::AggregationUpdateQueue);
impl_operation!(LeafDistanceUpdate leaf_distance_update::LeafDistanceUpdateQueue); impl_operation!(LeafDistanceUpdate leaf_distance_update::LeafDistanceUpdateQueue);
// GcOperation: manually implement instead of using impl_operation! macro
// because the macro also does `pub use` which triggers an unused import warning
// since GcOperation is only used internally.
impl From<gc::GcOperation> for AnyOperation {
fn from(op: gc::GcOperation) -> Self {
AnyOperation::Gc(op)
}
}
impl TryFrom<AnyOperation> for gc::GcOperation {
type Error = ();
fn try_from(op: AnyOperation) -> Result<Self, Self::Error> {
match op {
AnyOperation::Gc(op) => Ok(op),
_ => Err(()),
}
}
}
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
pub use self::invalidate::TaskDirtyCause; pub use self::invalidate::TaskDirtyCause;
......
...@@ -78,6 +78,9 @@ enum ModifiedState { ...@@ -78,6 +78,9 @@ enum ModifiedState {
pub struct Storage { pub struct Storage {
snapshot_mode: AtomicBool, snapshot_mode: AtomicBool,
modified: FxDashMap<TaskId, ModifiedState>, modified: FxDashMap<TaskId, ModifiedState>,
/// Tracks GC'd tasks pending database deletion. Populated by the GC processor,
/// drained during the next snapshot to emit deletion records.
deleted: FxDashMap<TaskId, ()>,
map: FxDashMap<TaskId, Box<TaskStorage>>, map: FxDashMap<TaskId, Box<TaskStorage>>,
} }
...@@ -97,6 +100,11 @@ impl Storage { ...@@ -97,6 +100,11 @@ impl Storage {
Default::default(), Default::default(),
shard_amount, shard_amount,
), ),
deleted: FxDashMap::with_capacity_and_hasher_and_shard_amount(
0,
Default::default(),
shard_amount,
),
map: FxDashMap::with_capacity_and_hasher_and_shard_amount( map: FxDashMap::with_capacity_and_hasher_and_shard_amount(
map_capacity, map_capacity,
Default::default(), Default::default(),
...@@ -288,9 +296,42 @@ impl Storage { ...@@ -288,9 +296,42 @@ impl Storage {
) )
} }
/// Remove a task from storage entirely. Called by the GC processor after
/// verifying the task has no incoming edges and disconnecting it from the
/// aggregation tree.
///
/// Returns the removed TaskStorage if it existed.
pub fn remove_task(&self, task_id: TaskId) -> Option<Box<TaskStorage>> {
self.modified.remove(&task_id);
self.map.remove(&task_id).map(|(_, v)| v)
}
/// Mark a task as deleted for database cleanup during the next snapshot.
/// The task should already be removed from the in-memory map via `remove_task()`.
pub fn mark_deleted(&self, task_id: TaskId) {
self.deleted.insert(task_id, ());
}
/// Drain all deleted task IDs, returning them for database deletion processing.
/// Called during snapshot to collect tasks that need their DB entries removed.
pub fn take_deleted(&self) -> Vec<TaskId> {
let mut deleted = Vec::with_capacity(self.deleted.len());
self.deleted.retain(|k, _| {
deleted.push(*k);
false
});
deleted
}
/// Returns the number of tasks currently in storage.
pub fn get_task_count(&self) -> usize {
self.map.len()
}
pub fn drop_contents(&self) { pub fn drop_contents(&self) {
drop_contents(&self.map); drop_contents(&self.map);
drop_contents(&self.modified); drop_contents(&self.modified);
drop_contents(&self.deleted);
} }
} }
......
...@@ -569,6 +569,27 @@ impl TaskStorage { ...@@ -569,6 +569,27 @@ impl TaskStorage {
}); });
} }
/// Returns `true` if this task has no incoming edges and is not currently executing,
/// meaning it is eligible for garbage collection.
///
/// A task is GC-eligible when ALL of the following are true:
/// - No activeness state (active_counter=0, no root type, no active_until_clean)
/// - No tasks reading this task's output (`output_dependent` is empty)
/// - No tasks reading this task's cells (`cell_dependents` is empty)
/// - No tasks reading this task's collectibles (`collectibles_dependents` is empty)
/// - Not currently executing (`in_progress` is absent)
///
/// Note: Aggregation tree edges (`upper`/`followers`) are NOT checked. They are a
/// bookkeeping overlay derived from the task graph, not independent liveness roots.
/// The GC processor actively cleans them up during task freeing.
pub fn is_gc_eligible(&self) -> bool {
self.get_activeness().is_none()
&& self.output_dependent().is_empty()
&& self.cell_dependents().is_none_or(|s| s.is_empty())
&& self.collectibles_dependents().is_none_or(|s| s.is_empty())
&& self.get_in_progress().is_none()
}
/// Returns counts for aggregation tree and collectibles fields. /// Returns counts for aggregation tree and collectibles fields.
/// Used for cache size statistics. /// Used for cache size statistics.
pub fn meta_counts(&self) -> MetaCounts { pub fn meta_counts(&self) -> MetaCounts {
...@@ -1034,6 +1055,99 @@ mod tests { ...@@ -1034,6 +1055,99 @@ mod tests {
// Schema Size Tests // Schema Size Tests
// ========================================================================== // ==========================================================================
#[test]
fn test_gc_eligible_empty_task() {
// A fresh task with no edges should be GC-eligible
let storage = TaskStorage::new();
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_with_output_dependent() {
let mut storage = TaskStorage::new();
storage
.output_dependent_mut()
.insert(TaskId::new(1).unwrap());
assert!(!storage.is_gc_eligible());
// Remove the dependent
storage
.output_dependent_mut()
.remove(&TaskId::new(1).unwrap());
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_with_cell_dependents() {
let mut storage = TaskStorage::new();
let cell_dep = (
CellId {
type_id: unsafe { turbo_tasks::ValueTypeId::new_unchecked(1) },
index: 0,
},
None,
TaskId::new(1).unwrap(),
);
storage.cell_dependents_mut().insert(cell_dep);
assert!(!storage.is_gc_eligible());
// Remove the dependent
storage.cell_dependents_mut().remove(&cell_dep);
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_with_collectibles_dependents() {
let mut storage = TaskStorage::new();
let trait_type = unsafe { TraitTypeId::new_unchecked(1) };
storage
.collectibles_dependents_mut()
.insert((trait_type, TaskId::new(1).unwrap()));
assert!(!storage.is_gc_eligible());
// Remove the dependent
storage
.collectibles_dependents_mut()
.remove(&(trait_type, TaskId::new(1).unwrap()));
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_with_activeness() {
let mut storage = TaskStorage::new();
storage.set_activeness(ActivenessState::new(TaskId::new(1).unwrap()));
assert!(!storage.is_gc_eligible());
// Remove activeness
storage.take_activeness();
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_with_in_progress() {
let mut storage = TaskStorage::new();
use turbo_tasks::event::Event;
storage.set_in_progress(InProgressState::Scheduled {
done_event: Event::new(|| || "test".to_string()),
reason: TaskExecutionReason::Initial,
});
assert!(!storage.is_gc_eligible());
// Remove in_progress
storage.take_in_progress();
assert!(storage.is_gc_eligible());
}
#[test]
fn test_gc_eligible_ignores_aggregation_edges() {
// Aggregation tree edges (upper/followers) should NOT affect GC eligibility
let mut storage = TaskStorage::new();
storage.upper_mut().insert(TaskId::new(1).unwrap(), 1);
storage.followers_mut().insert(TaskId::new(2).unwrap(), 1);
// Still eligible despite having aggregation edges
assert!(storage.is_gc_eligible());
}
#[test] #[test]
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
fn test_schema_size() { fn test_schema_size() {
......
...@@ -201,4 +201,108 @@ mod tests { ...@@ -201,4 +201,108 @@ mod tests {
factory.get(); factory.get();
} }
} }
#[test]
fn test_reuse_basic() {
let factory = IdFactoryWithReuse::new(NonZeroU8::MIN, NonZeroU8::MAX);
let id1 = factory.get();
let id2 = factory.get();
assert_eq!(id1, NonZeroU8::new(1).unwrap());
assert_eq!(id2, NonZeroU8::new(2).unwrap());
// Return id1 to the free list
unsafe { factory.reuse(id1) };
// Next get should return the reused id1
let id3 = factory.get();
assert_eq!(id3, id1);
// Next get should allocate a new id (3)
let id4 = factory.get();
assert_eq!(id4, NonZeroU8::new(3).unwrap());
}
#[test]
fn test_reuse_multiple() {
let factory = IdFactoryWithReuse::new(NonZeroU8::MIN, NonZeroU8::MAX);
let id1 = factory.get();
let id2 = factory.get();
let id3 = factory.get();
// Return multiple ids
unsafe { factory.reuse(id2) };
unsafe { factory.reuse(id1) };
// Should get back the reused ids (FIFO order from ConcurrentQueue)
let got1 = factory.get();
let got2 = factory.get();
assert_eq!(got1, id2);
assert_eq!(got2, id1);
// Now should allocate fresh
let got3 = factory.get();
assert_eq!(got3, NonZeroU8::new(4).unwrap());
assert_ne!(got3, id3); // id3 was never returned
}
#[test]
fn test_reuse_extends_capacity() {
// Verify that reuse prevents overflow by recycling ids
let factory =
IdFactoryWithReuse::new(NonZeroU8::new(1).unwrap(), NonZeroU8::new(3).unwrap());
let id1 = factory.get(); // 1
let id2 = factory.get(); // 2
let id3 = factory.get(); // 3 -- now at capacity
// Return id1 so we can get one more
unsafe { factory.reuse(id1) };
let id4 = factory.get(); // Should get id1 back
assert_eq!(id4, id1);
// All still alive: id2, id3, id4(=id1)
let _ = (id2, id3, id4);
}
#[test]
fn test_reuse_concurrent() {
use std::{sync::Arc, thread};
let factory = Arc::new(IdFactoryWithReuse::new(
NonZeroU64::new(1).unwrap(),
NonZeroU64::MAX,
));
let num_threads = 4;
let ops_per_thread = 1000;
let handles: Vec<_> = (0..num_threads)
.map(|_| {
let factory = factory.clone();
thread::spawn(move || {
let mut ids = Vec::new();
for _ in 0..ops_per_thread {
ids.push(factory.get());
}
// Return half the ids
for id in ids.drain(..ops_per_thread / 2) {
unsafe { factory.reuse(id) };
}
// Get some more (should reuse)
for _ in 0..ops_per_thread / 4 {
ids.push(factory.get());
}
ids
})
})
.collect();
let all_ids: Vec<Vec<NonZeroU64>> =
handles.into_iter().map(|h| h.join().unwrap()).collect();
// Just verify no panics occurred and we got valid ids
let total = all_ids.iter().map(|v| v.len()).sum::<usize>();
assert_eq!(
total,
num_threads * (ops_per_thread / 2 + ops_per_thread / 4)
);
}
} }
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