Unverified Commit 08668326 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

refactor: migrate to typed accessors and remove CachedDataItem adapter (#88397)

Adopt the new generated accessor methods in turbo-tasks-backend.

Remove the CachedDataItem enum and related support macros.

This new approach is more ergonomic, memory efficient, and generates slightly smaller serialized payloads.  

From measuring vercel-site:
```
canary@e05510ab3bf91c02c7381e2c471363561ffa1198:
$ hyperfine -p 'rm -rf .next' -w 2 -r 10  'pnpm next build --turbopack --experimental-build-mode=compile'
Benchmark 1: pnpm next build --turbopack --experimental-build-mode=compile
  Time (mean ± σ):     55.981 s ±  0.885 s    [User: 386.683 s, System: 111.484 s]
  Range (min … max):   54.438 s … 57.723 s    10 runs
```

```
01-09-migrate-to-typed-accessors@d343fad5bf92af9c5271c62a24aedcf02c3d83d4
$ hyperfine -p 'rm -rf .next' -w 2 -r 10  'pnpm next build --turbopack --experimental-build-mode=compile'
Benchmark 1: pnpm next build --turbopack --experimental-build-mode=compile
  Time (mean ± σ):     54.897 s ±  0.496 s    [User: 389.400 s, System: 127.353 s]
  Range (min … max):   54.298 s … 55.681 s    10 runs
```

so a small savings of a little over a second.  Not too bad for mostly removing a bunch of `match` expressions that were required in the old model.

similarly we should expect a small progression in disk size since we no longer explode into `Vec<CachedDataItem>` which induces some overhead from redundantly encoding enum descriminents and not being able to 'length prefix' collections.

from a clean `.next` dir on vercel-site, and a production build with caching enabled, the cache size is

before
```
$ du -h .next/cache
4.3G	.next/cache/turbopack/v16.1.0-canary.0-518-ge05510ab
```
after
```
$ du -h .next/cache
4.1G	.next/cache/turbopack/v16.1.0-canary.0-546-gd343fad5bf92
```
Which is expected, the main thing we are winning in terms of cache size is the CachedDataItem descriminent values, and in this build we were serializing about 260M items, so we save 1 byte for each of them.

In some measurements i saw much more dramatic wins of >1gb, which i do not have a great explanation for.
parent 78bc2bad
...@@ -106,15 +106,7 @@ impl<K, V> CounterMap<K, V> { ...@@ -106,15 +106,7 @@ impl<K, V> CounterMap<K, V> {
{ {
self.0.get(key) self.0.get(key)
} }
// TODO(lukesandberg): this is just here for the CachedDataItem adaptor layer, can be removed
// once that is gone.
#[doc(hidden)]
pub fn get_mut(&mut self, _key: &K) -> Option<&mut V>
where
K: Eq + Hash,
{
unreachable!("This should never be called, please insert instead")
}
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> { pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
self.0.iter() self.0.iter()
} }
......
...@@ -7,7 +7,7 @@ use turbo_tasks::TaskId; ...@@ -7,7 +7,7 @@ use turbo_tasks::TaskId;
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, get, get_many, TaskDataCategory,
operation::{ operation::{
AggregatedDataUpdate, ExecuteContext, Operation, AggregatedDataUpdate, ExecuteContext, Operation,
aggregation_update::{ aggregation_update::{
...@@ -15,10 +15,9 @@ use crate::{ ...@@ -15,10 +15,9 @@ use crate::{
get_aggregation_number, get_uppers, is_aggregating_node, get_aggregation_number, get_uppers, is_aggregating_node,
}, },
}, },
storage::update_count,
storage_schema::TaskStorageAccessors, storage_schema::TaskStorageAccessors,
}, },
data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef}, data::{CellRef, CollectibleRef, CollectiblesRef},
}; };
#[derive(Encode, Decode, Clone, Default)] #[derive(Encode, Decode, Clone, Default)]
...@@ -50,7 +49,7 @@ impl CleanupOldEdgesOperation { ...@@ -50,7 +49,7 @@ impl CleanupOldEdgesOperation {
task_id: TaskId, task_id: TaskId,
outdated: Vec<OutdatedEdge>, outdated: Vec<OutdatedEdge>,
queue: AggregationUpdateQueue, queue: AggregationUpdateQueue,
ctx: &mut impl ExecuteContext, ctx: &mut impl ExecuteContext<'_>,
) { ) {
CleanupOldEdgesOperation::RemoveEdges { CleanupOldEdgesOperation::RemoveEdges {
task_id, task_id,
...@@ -62,7 +61,7 @@ impl CleanupOldEdgesOperation { ...@@ -62,7 +61,7 @@ impl CleanupOldEdgesOperation {
} }
impl Operation for CleanupOldEdgesOperation { impl Operation for CleanupOldEdgesOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) { fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop { loop {
ctx.operation_suspend_point(&self); ctx.operation_suspend_point(&self);
match self { match self {
...@@ -84,8 +83,8 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -84,8 +83,8 @@ impl Operation for CleanupOldEdgesOperation {
_ => true, _ => true,
}); });
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::All);
for &child_id in children.iter() { for task_id in children.iter() {
task.remove(&CachedDataItemKey::Child { task: child_id }); task.remove_children(task_id);
} }
if is_aggregating_node(get_aggregation_number(&task)) { if is_aggregating_node(get_aggregation_number(&task)) {
queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers { queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers {
...@@ -96,7 +95,8 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -96,7 +95,8 @@ impl Operation for CleanupOldEdgesOperation {
} else { } else {
let upper_ids = get_uppers(&task); let upper_ids = get_uppers(&task);
let has_active_count = ctx.should_track_activeness() let has_active_count = ctx.should_track_activeness()
&& get!(task, Activeness) && task
.get_activeness()
.is_some_and(|a| a.active_counter > 0); .is_some_and(|a| a.active_counter > 0);
drop(task); drop(task);
if has_active_count { if has_active_count {
...@@ -127,19 +127,20 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -127,19 +127,20 @@ impl Operation for CleanupOldEdgesOperation {
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::All);
let mut emptied_collectables = FxHashSet::default(); let mut emptied_collectables = FxHashSet::default();
for (collectible, count) in collectibles.iter_mut() { for (collectible, count) in collectibles.iter_mut() {
if update_count!( if task
task, .update_collectibles_positive_crossing(*collectible, *count)
Collectible { {
collectible: *collectible
},
*count
) {
emptied_collectables.insert(collectible.collectible_type); emptied_collectables.insert(collectible.collectible_type);
} }
} }
for ty in emptied_collectables { for ty in emptied_collectables {
let task_ids = get_many!(task, CollectiblesDependent { collectible_type, task } if collectible_type == ty => { task }); let task_ids: SmallVec<[_; 4]> = task
.iter_collectibles_dependents()
.filter_map(|(collectible_type, task)| {
(collectible_type == ty).then_some(task)
})
.collect();
queue.push( queue.push(
AggregationUpdateJob::InvalidateDueToCollectiblesChange { AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids, task_ids,
...@@ -162,21 +163,17 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -162,21 +163,17 @@ 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(&CachedDataItemKey::CellDependent { task.remove_cell_dependents(&(cell, key, task_id));
cell,
key,
task: task_id,
});
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CellDependency { task.remove_cell_dependencies(&(
target: CellRef { CellRef {
task: cell_task_id, task: cell_task_id,
cell, cell,
}, },
key, key,
}); ));
} }
} }
OutdatedEdge::OutputDependency(output_task_id) => { OutdatedEdge::OutputDependency(output_task_id) => {
...@@ -189,15 +186,11 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -189,15 +186,11 @@ impl Operation for CleanupOldEdgesOperation {
.entered(); .entered();
{ {
let mut task = ctx.task(output_task_id, TaskDataCategory::Data); let mut task = ctx.task(output_task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::OutputDependent { task.remove_output_dependent(&task_id);
task: task_id,
});
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::OutputDependency { task.remove_output_dependencies(&output_task_id);
target: output_task_id,
});
} }
} }
OutdatedEdge::CollectiblesDependency(CollectiblesRef { OutdatedEdge::CollectiblesDependency(CollectiblesRef {
...@@ -207,18 +200,16 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -207,18 +200,16 @@ impl Operation for CleanupOldEdgesOperation {
{ {
let mut task = let mut task =
ctx.task(dependent_task_id, TaskDataCategory::Data); ctx.task(dependent_task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CollectiblesDependent { task.remove_collectibles_dependents(&(
collectible_type, collectible_type,
task: task_id, task_id,
}); ));
} }
{ {
let mut task = ctx.task(task_id, TaskDataCategory::Data); let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CollectiblesDependency { task.remove_collectibles_dependencies(&CollectiblesRef {
target: CollectiblesRef { collectible_type,
collectible_type, task: dependent_task_id,
task: dependent_task_id,
},
}); });
} }
} }
......
...@@ -3,14 +3,14 @@ use turbo_tasks::{TaskExecutionReason, TaskId}; ...@@ -3,14 +3,14 @@ use turbo_tasks::{TaskExecutionReason, TaskId};
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, get_mut, TaskDataCategory,
operation::{ operation::{
ExecuteContext, Operation, ExecuteContext, Operation, TaskGuard,
aggregation_update::{AggregationUpdateJob, AggregationUpdateQueue}, aggregation_update::{AggregationUpdateJob, AggregationUpdateQueue},
}, },
storage_schema::TaskStorageAccessors, storage_schema::TaskStorageAccessors,
}, },
data::{CachedDataItem, CachedDataItemKey, InProgressState, InProgressStateInner}, data::{InProgressState, InProgressStateInner},
}; };
#[derive(Encode, Decode, Clone, Default)] #[derive(Encode, Decode, Clone, Default)]
...@@ -33,7 +33,7 @@ impl ConnectChildOperation { ...@@ -33,7 +33,7 @@ impl ConnectChildOperation {
let mut parent_task = ctx.task(parent_task_id, TaskDataCategory::All); let mut parent_task = ctx.task(parent_task_id, TaskDataCategory::All);
let Some(InProgressState::InProgress(box InProgressStateInner { let Some(InProgressState::InProgress(box InProgressStateInner {
new_children, .. new_children, ..
})) = get_mut!(parent_task, InProgress) })) = parent_task.get_in_progress_mut()
else { else {
panic!("Task is not in progress while calling another task: {parent_task:?}"); panic!("Task is not in progress while calling another task: {parent_task:?}");
}; };
...@@ -43,9 +43,7 @@ impl ConnectChildOperation { ...@@ -43,9 +43,7 @@ impl ConnectChildOperation {
return; return;
} }
if parent_task.has_key(&CachedDataItemKey::Child { if parent_task.children_contains(&child_task_id) {
task: child_task_id,
}) {
// It is already connected, we can skip the rest // It is already connected, we can skip the rest
return; return;
} }
...@@ -69,11 +67,10 @@ impl ConnectChildOperation { ...@@ -69,11 +67,10 @@ impl ConnectChildOperation {
} else { } else {
let mut child_task = ctx.task(child_task_id, TaskDataCategory::All); let mut child_task = ctx.task(child_task_id, TaskDataCategory::All);
if !child_task.has_key(&CachedDataItemKey::Output {}) if !child_task.has_output()
&& child_task.add(CachedDataItem::new_scheduled( && child_task.add_scheduled(TaskExecutionReason::Connect, || {
TaskExecutionReason::Connect, ctx.get_task_desc_fn(child_task_id)
|| ctx.get_task_desc_fn(child_task_id), })
))
{ {
ctx.schedule_task(child_task, ctx.get_current_task_priority()); ctx.schedule_task(child_task, ctx.get_current_task_priority());
} }
...@@ -87,7 +84,7 @@ impl ConnectChildOperation { ...@@ -87,7 +84,7 @@ impl ConnectChildOperation {
} }
impl Operation for ConnectChildOperation { impl Operation for ConnectChildOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) { fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop { loop {
ctx.operation_suspend_point(&self); ctx.operation_suspend_point(&self);
match self { match self {
......
...@@ -6,13 +6,10 @@ use turbo_tasks::{ ...@@ -6,13 +6,10 @@ use turbo_tasks::{
util::{good_chunk_size, into_chunks}, util::{good_chunk_size, into_chunks},
}; };
use crate::{ use crate::backend::operation::{
backend::operation::{ AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext, Operation,
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext, TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob, get_aggregation_number,
Operation, TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob, get_uppers, is_aggregating_node,
get_aggregation_number, get_uppers, is_aggregating_node,
},
data::{CachedDataItem, CachedDataItemType},
}; };
pub fn connect_children( pub fn connect_children(
...@@ -27,12 +24,13 @@ pub fn connect_children( ...@@ -27,12 +24,13 @@ pub fn connect_children(
let parent_aggregation = get_aggregation_number(&parent_task); let parent_aggregation = get_aggregation_number(&parent_task);
parent_task.extend_new( let old_children = parent_task.children_len();
CachedDataItemType::Child, parent_task.extend_children(new_children.iter().copied());
new_children.iter().map(|&new_child| CachedDataItem::Child { debug_assert!(
task: new_child, old_children + new_children.len() == parent_task.children_len(),
value: (), "Attempted to connect {len} new children, but some of them were already present in \
}), {parent_task_id}",
len = new_children.len()
); );
let new_follower_ids: SmallVec<_> = new_children.into_iter().collect(); let new_follower_ids: SmallVec<_> = new_children.into_iter().collect();
......
...@@ -11,9 +11,8 @@ use crate::{ ...@@ -11,9 +11,8 @@ use crate::{
AggregationUpdateJob, AggregationUpdateQueue, ComputeDirtyAndCleanUpdate, AggregationUpdateJob, AggregationUpdateQueue, ComputeDirtyAndCleanUpdate,
}, },
}, },
storage::{get, get_mut, remove},
}, },
data::{CachedDataItem, CachedDataItemKey, Dirtyness, InProgressState, InProgressStateInner}, data::{Dirtyness, InProgressState, InProgressStateInner},
}; };
#[derive(Encode, Decode, Clone, Default)] #[derive(Encode, Decode, Clone, Default)]
...@@ -35,7 +34,7 @@ impl InvalidateOperation { ...@@ -35,7 +34,7 @@ impl InvalidateOperation {
pub fn run( pub fn run(
task_ids: SmallVec<[TaskId; 4]>, task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause, #[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
mut ctx: impl ExecuteContext, mut ctx: impl ExecuteContext<'_>,
) { ) {
InvalidateOperation::MakeDirty { InvalidateOperation::MakeDirty {
task_ids, task_ids,
...@@ -47,7 +46,7 @@ impl InvalidateOperation { ...@@ -47,7 +46,7 @@ impl InvalidateOperation {
} }
impl Operation for InvalidateOperation { impl Operation for InvalidateOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) { fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop { loop {
ctx.operation_suspend_point(&self); ctx.operation_suspend_point(&self);
match self { match self {
...@@ -183,7 +182,7 @@ pub fn make_task_dirty( ...@@ -183,7 +182,7 @@ pub fn make_task_dirty(
task_id: TaskId, task_id: TaskId,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause, #[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue, queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext, ctx: &mut impl ExecuteContext<'_>,
) { ) {
if ctx.is_once_task(task_id) { if ctx.is_once_task(task_id) {
return; return;
...@@ -208,12 +207,12 @@ pub fn make_task_dirty_internal( ...@@ -208,12 +207,12 @@ pub fn make_task_dirty_internal(
make_stale: bool, make_stale: bool,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause, #[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue, queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext, ctx: &mut impl ExecuteContext<'_>,
) { ) {
// There must be no way to invalidate immutable tasks. If there would be a way the task is not // There must be no way to invalidate immutable tasks. If there would be a way the task is not
// immutable. // immutable.
#[cfg(any(debug_assertions, feature = "verify_immutable"))] #[cfg(any(debug_assertions, feature = "verify_immutable"))]
if task.is_immutable() { if task.immutable() {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
let extra_info = format!( let extra_info = format!(
" Invalidation cause: {}", " Invalidation cause: {}",
...@@ -231,7 +230,7 @@ pub fn make_task_dirty_internal( ...@@ -231,7 +230,7 @@ pub fn make_task_dirty_internal(
if make_stale if make_stale
&& let Some(InProgressState::InProgress(box InProgressStateInner { stale, .. })) = && let Some(InProgressState::InProgress(box InProgressStateInner { stale, .. })) =
get_mut!(task, InProgress) task.get_in_progress_mut()
&& !*stale && !*stale
{ {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
...@@ -244,7 +243,7 @@ pub fn make_task_dirty_internal( ...@@ -244,7 +243,7 @@ pub fn make_task_dirty_internal(
.entered(); .entered();
*stale = true; *stale = true;
} }
let current = get!(task, Dirty); let current = task.get_dirty();
let (old_self_dirty, old_current_session_self_clean, parent_priority) = match current { let (old_self_dirty, old_current_session_self_clean, parent_priority) = match current {
Some(Dirtyness::Dirty(current_priority)) => { Some(Dirtyness::Dirty(current_priority)) => {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
...@@ -257,22 +256,19 @@ pub fn make_task_dirty_internal( ...@@ -257,22 +256,19 @@ pub fn make_task_dirty_internal(
.entered(); .entered();
// already dirty // already dirty
let parent_priority = ctx.get_current_task_priority(); let parent_priority = ctx.get_current_task_priority();
if current_priority >= &parent_priority { if *current_priority >= parent_priority {
// Update the priority to be the lower one // Update the priority to be the lower one
task.insert(CachedDataItem::Dirty { task.set_dirty(Dirtyness::Dirty(parent_priority));
value: Dirtyness::Dirty(parent_priority),
});
} }
return; return;
} }
Some(Dirtyness::SessionDependent) => { Some(Dirtyness::SessionDependent) => {
let parent_priority = ctx.get_current_task_priority(); let parent_priority = ctx.get_current_task_priority();
task.insert(CachedDataItem::Dirty { task.set_dirty(Dirtyness::Dirty(parent_priority));
value: Dirtyness::Dirty(parent_priority),
});
// It was a session-dependent dirty before, so we need to remove that clean count // It was a session-dependent dirty before, so we need to remove that clean count
let was_current_session_clean = remove!(task, CurrentSessionClean).is_some(); let was_current_session_clean = task.current_session_clean();
if was_current_session_clean { if was_current_session_clean {
task.set_current_session_clean(false);
// There was a clean count for a session. If it was the current session, we need to // There was a clean count for a session. If it was the current session, we need to
// propagate that change. // propagate that change.
(true, true, parent_priority) (true, true, parent_priority)
...@@ -290,9 +286,7 @@ pub fn make_task_dirty_internal( ...@@ -290,9 +286,7 @@ pub fn make_task_dirty_internal(
} }
None => { None => {
let parent_priority = ctx.get_current_task_priority(); let parent_priority = ctx.get_current_task_priority();
task.insert(CachedDataItem::Dirty { task.set_dirty(Dirtyness::Dirty(parent_priority));
value: Dirtyness::Dirty(parent_priority),
});
// It was clean before, so we need to increase the dirty count // It was clean before, so we need to increase the dirty count
(false, false, parent_priority) (false, false, parent_priority)
} }
...@@ -301,13 +295,14 @@ pub fn make_task_dirty_internal( ...@@ -301,13 +295,14 @@ pub fn make_task_dirty_internal(
let new_self_dirty = true; let new_self_dirty = true;
let new_current_session_self_clean = false; let new_current_session_self_clean = false;
let dirty_container_count = get!(task, AggregatedDirtyContainerCount) let dirty_container_count = task
.get_aggregated_dirty_container_count()
.copied()
.unwrap_or_default();
let current_session_clean_container_count = task
.get_aggregated_current_session_clean_container_count()
.copied() .copied()
.unwrap_or_default(); .unwrap_or_default();
let current_session_clean_container_count =
get!(task, AggregatedCurrentSessionCleanContainerCount)
.copied()
.unwrap_or_default();
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
let _span = tracing::trace_span!( let _span = tracing::trace_span!(
...@@ -337,18 +332,15 @@ pub fn make_task_dirty_internal( ...@@ -337,18 +332,15 @@ pub fn make_task_dirty_internal(
)); ));
} }
let should_schedule = let should_schedule = !ctx.should_track_activeness() || task.has_activeness();
!ctx.should_track_activeness() || task.has_key(&CachedDataItemKey::Activeness {});
if should_schedule { if should_schedule
let description = || ctx.get_task_desc_fn(task_id); && task.add_scheduled(TaskExecutionReason::Invalidated, || {
if task.add(CachedDataItem::new_scheduled( ctx.get_task_desc_fn(task_id)
TaskExecutionReason::Invalidated, })
description, {
)) { drop(task);
drop(task); let task = ctx.task(task_id, TaskDataCategory::All);
let task = ctx.task(task_id, TaskDataCategory::All); ctx.schedule_task(task, parent_priority);
ctx.schedule_task(task, parent_priority);
}
} }
} }
...@@ -9,14 +9,10 @@ use rustc_hash::FxHashMap; ...@@ -9,14 +9,10 @@ use rustc_hash::FxHashMap;
use tracing::{span::Span, trace_span}; use tracing::{span::Span, trace_span};
use turbo_tasks::TaskId; use turbo_tasks::TaskId;
use crate::{ use crate::backend::{
backend::{ TaskDataCategory,
TaskDataCategory, operation::{ExecuteContext, Operation},
operation::{ExecuteContext, Operation}, storage_schema::TaskStorageAccessors,
storage::{get, iter_many},
storage_schema::TaskStorageAccessors,
},
data::CachedDataItem,
}; };
/// The maximum number of leaf distance updates to process before yielding back to the executor. /// The maximum number of leaf distance updates to process before yielding back to the executor.
...@@ -148,7 +144,7 @@ impl LeafDistanceUpdateQueue { ...@@ -148,7 +144,7 @@ impl LeafDistanceUpdateQueue {
TaskDataCategory::Data, TaskDataCategory::Data,
); );
debug_assert!(dependencies_max_distance_in_buffer < u32::MAX / 2); debug_assert!(dependencies_max_distance_in_buffer < u32::MAX / 2);
let mut leaf_distance = get!(task, LeafDistance).copied().unwrap_or_default(); let mut leaf_distance = task.get_leaf_distance().copied().unwrap_or_default();
if leaf_distance.distance > dependencies_distance { if leaf_distance.distance > dependencies_distance {
// It is strictly monotonic. No need to update. // It is strictly monotonic. No need to update.
return; return;
...@@ -165,22 +161,17 @@ impl LeafDistanceUpdateQueue { ...@@ -165,22 +161,17 @@ impl LeafDistanceUpdateQueue {
// We are within the buffer zone, keep the max as is // We are within the buffer zone, keep the max as is
leaf_distance.distance = dependencies_distance + 1; leaf_distance.distance = dependencies_distance + 1;
} }
let dependents = iter_many!(task, OutputDependent { task } => task) // TODO Technically CellDependent is also needed, but there are cycles in the CellDependent
// TODO Technically this is also needed, but there are cycles in the CellDependent graph // graph. So we need to handle that properly first. When enabling this, make sure to also
// So we need to handle that properly first // call the leaf update queue when adding CellDependents.
// When enabling this, make sure to also call the leaf update queue when adding CellDependents for dependent_id in task.iter_output_dependent() {
// .chain(iter_many!(task, CellDependent { task, .. } => task))
;
for dependent_id in dependents {
self.push( self.push(
dependent_id, dependent_id,
leaf_distance.distance, leaf_distance.distance,
leaf_distance.max_distance_in_buffer, leaf_distance.max_distance_in_buffer,
); );
} }
task.insert(CachedDataItem::LeafDistance { task.set_leaf_distance(leaf_distance);
value: leaf_distance,
});
} }
} }
......
...@@ -3,11 +3,8 @@ use std::{cmp::max, num::NonZeroU32}; ...@@ -3,11 +3,8 @@ use std::{cmp::max, num::NonZeroU32};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use turbo_tasks::TaskId; use turbo_tasks::TaskId;
use crate::backend::{ use crate::backend::operation::{
get, AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
operation::{
AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
},
}; };
const AGGREGATION_NUMBER_BUFFER_SPACE: u32 = 3; const AGGREGATION_NUMBER_BUFFER_SPACE: u32 = 3;
...@@ -22,7 +19,8 @@ pub fn prepare_new_children( ...@@ -22,7 +19,8 @@ pub fn prepare_new_children(
let children_count = new_children.len(); let children_count = new_children.len();
// Compute future parent aggregation number based on the number of children // Compute future parent aggregation number based on the number of children
let current_parent_aggregation = get!(parent_task, AggregationNumber) let current_parent_aggregation = parent_task
.get_aggregation_number()
.copied() .copied()
.unwrap_or_default(); .unwrap_or_default();
let future_parent_aggregation = if is_root_node(current_parent_aggregation.base) { let future_parent_aggregation = if is_root_node(current_parent_aggregation.base) {
......
...@@ -12,14 +12,14 @@ use turbo_tasks::{CellId, FxIndexMap, TaskId, TypedSharedReference, backend::Cel ...@@ -12,14 +12,14 @@ use turbo_tasks::{CellId, FxIndexMap, TaskId, TypedSharedReference, backend::Cel
use crate::backend::operation::invalidate::TaskDirtyCause; use crate::backend::operation::invalidate::TaskDirtyCause;
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, TaskStorageAccessors, TaskDataCategory,
operation::{ operation::{
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard, AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
invalidate::make_task_dirty_internal, invalidate::make_task_dirty_internal,
}, },
storage::{iter_many, remove}, storage_schema::TaskStorageAccessors,
}, },
data::{CachedDataItem, CachedDataItemKey, CellRef}, data::CellRef,
}; };
#[derive(Encode, Decode, Clone, Default)] #[derive(Encode, Decode, Clone, Default)]
...@@ -57,7 +57,7 @@ impl UpdateCellOperation { ...@@ -57,7 +57,7 @@ impl UpdateCellOperation {
updated_key_hashes: Option<SmallVec<[u64; 2]>>, updated_key_hashes: Option<SmallVec<[u64; 2]>>,
#[cfg(feature = "verify_determinism")] verification_mode: VerificationMode, #[cfg(feature = "verify_determinism")] verification_mode: VerificationMode,
#[cfg(not(feature = "verify_determinism"))] _verification_mode: VerificationMode, #[cfg(not(feature = "verify_determinism"))] _verification_mode: VerificationMode,
mut ctx: impl ExecuteContext, mut ctx: impl ExecuteContext<'_>,
) { ) {
let content = if let CellContent(Some(new_content)) = content { let content = if let CellContent(Some(new_content)) = content {
Some(new_content.into_typed(cell.type_id)) Some(new_content.into_typed(cell.type_id))
...@@ -70,8 +70,7 @@ impl UpdateCellOperation { ...@@ -70,8 +70,7 @@ impl UpdateCellOperation {
// We need to detect recomputation, because here the content has not actually changed (even // We need to detect recomputation, because here the content has not actually changed (even
// if it's not equal to the old content, as not all values implement Eq). We have to // if it's not equal to the old content, as not all values implement Eq). We have to
// assume that tasks are deterministic and pure. // assume that tasks are deterministic and pure.
let assume_unchanged = let assume_unchanged = !ctx.should_track_dependencies() || !task.has_dirty();
!ctx.should_track_dependencies() || !task.has_key(&CachedDataItemKey::Dirty {});
if assume_unchanged { if assume_unchanged {
let has_old_content = task.has_cell_data(is_serializable_cell_content, cell); let has_old_content = task.has_cell_data(is_serializable_cell_content, cell);
...@@ -108,20 +107,21 @@ impl UpdateCellOperation { ...@@ -108,20 +107,21 @@ impl UpdateCellOperation {
Lazy::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>()) Lazy::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
}); });
let tasks_with_keys = iter_many!( let tasks_with_keys = task
task, .iter_cell_dependents()
CellDependent { cell: dependent_cell, key, task } .filter(|&(dependent_cell, key, _)| {
if dependent_cell == cell && key.is_none_or(|key_hash| { dependent_cell == cell
updated_key_hashes_set.as_ref().is_none_or(|set| { && key.is_none_or(|key_hash| {
set.contains(&key_hash) updated_key_hashes_set
}) .as_ref()
.is_none_or(|set| set.contains(&key_hash))
})
}) })
=> (task, key) .map(|(_, key, task)| (task, key))
) .filter(|&(dependent_task_id, _)| {
.filter(|&(dependent_task_id, _)| { // once tasks are never invalidated
// once tasks are never invalidated !ctx.is_once_task(dependent_task_id)
!ctx.is_once_task(dependent_task_id) });
});
let mut dependent_tasks: FxIndexMap<TaskId, SmallVec<[Option<u64>; 2]>> = let mut dependent_tasks: FxIndexMap<TaskId, SmallVec<[Option<u64>; 2]>> =
FxIndexMap::default(); FxIndexMap::default();
for (task, key) in tasks_with_keys { for (task, key) in tasks_with_keys {
...@@ -142,10 +142,7 @@ impl UpdateCellOperation { ...@@ -142,10 +142,7 @@ impl UpdateCellOperation {
// tasks and after that set the new cell content. When the cell content is unset, // tasks and after that set the new cell content. When the cell content is unset,
// readers will wait for it to be set via InProgressCell. // readers will wait for it to be set via InProgressCell.
let old_content = task.remove(&CachedDataItemKey::cell_data( let old_content = task.remove_cell_data(is_serializable_cell_content, cell);
is_serializable_cell_content,
cell,
));
drop(task); drop(task);
drop(old_content); drop(old_content);
...@@ -177,19 +174,12 @@ impl UpdateCellOperation { ...@@ -177,19 +174,12 @@ impl UpdateCellOperation {
// So we can just update the cell content. // So we can just update the cell content.
let old_content = if let Some(new_content) = content { let old_content = if let Some(new_content) = content {
task.insert(CachedDataItem::cell_data( task.set_cell_data(is_serializable_cell_content, cell, new_content)
is_serializable_cell_content,
cell,
new_content,
))
} else { } else {
task.remove(&CachedDataItemKey::cell_data( task.remove_cell_data(is_serializable_cell_content, cell)
is_serializable_cell_content,
cell,
))
}; };
let in_progress_cell = remove!(task, InProgressCell { cell }); let in_progress_cell = task.remove_in_progress_cells(&cell);
drop(task); drop(task);
drop(old_content); drop(old_content);
...@@ -216,7 +206,7 @@ impl UpdateCellOperation { ...@@ -216,7 +206,7 @@ impl UpdateCellOperation {
} }
impl Operation for UpdateCellOperation { impl Operation for UpdateCellOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) { fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop { loop {
if self.is_serializable() { if self.is_serializable() {
ctx.operation_suspend_point(&self); ctx.operation_suspend_point(&self);
...@@ -235,20 +225,14 @@ impl Operation for UpdateCellOperation { ...@@ -235,20 +225,14 @@ impl Operation for UpdateCellOperation {
let mut make_stale = false; let mut make_stale = false;
let dependent = ctx.task(dependent_task_id, TaskDataCategory::All); let dependent = ctx.task(dependent_task_id, TaskDataCategory::All);
for key in keys.iter().copied() { for key in keys.iter().copied() {
if dependent.has_key(&CachedDataItemKey::OutdatedCellDependency { if dependent.outdated_cell_dependencies_contains(&(cell_ref, key)) {
target: cell_ref,
key,
}) {
// cell dependency is outdated, so it hasn't read the cell yet // cell dependency is outdated, so it hasn't read the cell yet
// and doesn't need to be invalidated. // and doesn't need to be invalidated.
// We do not need to make the task stale in this case. // We do not need to make the task stale in this case.
// But importantly we still need to make the task dirty as it should // But importantly we still need to make the task dirty as it should
// no longer be considered as // no longer be considered as
// "recomputation". // "recomputation".
} else if !dependent.has_key(&CachedDataItemKey::CellDependency { } else if !dependent.cell_dependencies_contains(&(cell_ref, key)) {
target: cell_ref,
key,
}) {
// cell dependency has been removed, so the task doesn't depend on // cell dependency has been removed, so the task doesn't depend on
// the cell anymore and doesn't need // the cell anymore and doesn't need
// to be invalidated // to be invalidated
...@@ -288,14 +272,10 @@ impl Operation for UpdateCellOperation { ...@@ -288,14 +272,10 @@ impl Operation for UpdateCellOperation {
let mut task = ctx.task(task, TaskDataCategory::Data); let mut task = ctx.task(task, TaskDataCategory::Data);
if let Some(content) = content { if let Some(content) = content {
task.add_new(CachedDataItem::cell_data( task.add_cell_data(is_serializable_cell_content, cell, content);
is_serializable_cell_content,
cell,
content,
));
} }
let in_progress_cell = remove!(task, InProgressCell { cell }); let in_progress_cell = task.remove_in_progress_cells(&cell);
drop(task); drop(task);
......
...@@ -5,12 +5,12 @@ use turbo_tasks::TaskId; ...@@ -5,12 +5,12 @@ use turbo_tasks::TaskId;
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, get_many, TaskDataCategory,
operation::{ operation::{
AggregatedDataUpdate, AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, AggregatedDataUpdate, AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext,
Operation, get_aggregation_number, is_root_node, Operation, get_aggregation_number, is_root_node,
}, },
storage::{get, update_count}, storage_schema::TaskStorageAccessors,
}, },
data::CollectibleRef, data::CollectibleRef,
}; };
...@@ -22,7 +22,7 @@ impl UpdateCollectibleOperation { ...@@ -22,7 +22,7 @@ impl UpdateCollectibleOperation {
task_id: TaskId, task_id: TaskId,
collectible: CollectibleRef, collectible: CollectibleRef,
mut count: i32, mut count: i32,
mut ctx: impl ExecuteContext, mut ctx: impl ExecuteContext<'_>,
) { ) {
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::All);
if count < 0 { if count < 0 {
...@@ -52,32 +52,27 @@ impl UpdateCollectibleOperation { ...@@ -52,32 +52,27 @@ impl UpdateCollectibleOperation {
} }
} }
let mut queue = AggregationUpdateQueue::new(); let mut queue = AggregationUpdateQueue::new();
let outdated = get!(task, OutdatedCollectible { collectible }).copied(); let outdated = task.get_outdated_collectibles(&collectible).copied();
if let Some(outdated) = outdated { if let Some(outdated) = outdated {
if count > 0 && outdated > 0 { if count > 0 && outdated > 0 {
let shared = min(count, outdated); let shared = min(count, outdated);
update_count!(task, OutdatedCollectible { collectible }, -shared); let _ = task.update_outdated_collectibles_positive_crossing(collectible, -shared);
count -= shared; count -= shared;
} else if count < 0 && outdated < 0 { } else if count < 0 && outdated < 0 {
let shared = min(-count, -outdated); let shared = min(-count, -outdated);
update_count!(task, OutdatedCollectible { collectible }, shared); let _ = task.update_outdated_collectibles_positive_crossing(collectible, shared);
count += shared; count += shared;
} else { } else {
// Not reduced from outdated // Not reduced from outdated
} }
} }
if count != 0 { if count != 0 {
if update_count!(task, Collectible { collectible }, count) { if task.update_collectibles_positive_crossing(collectible, count) {
let ty = collectible.collectible_type; let ty = collectible.collectible_type;
let dependent: SmallVec<[TaskId; 4]> = get_many!( let dependent: SmallVec<[TaskId; 4]> = task
task, .iter_collectibles_dependents()
CollectiblesDependent { .filter_map(|(collectible_type, task)| (collectible_type == ty).then_some(task))
collectible_type, .collect();
task,
} if collectible_type == ty => {
task
}
);
if !dependent.is_empty() { if !dependent.is_empty() {
queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange { queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids: dependent, task_ids: dependent,
......
...@@ -64,6 +64,8 @@ enum ModifiedState { ...@@ -64,6 +64,8 @@ enum ModifiedState {
/// Snapshot(Some): /// Snapshot(Some):
/// It was modified before snapshot mode was entered and it was accessed again during snapshot /// It was modified before snapshot mode was entered and it was accessed again during snapshot
/// mode. A copy of the version of the item when snapshot mode was entered is stored here. /// mode. A copy of the version of the item when snapshot mode was entered is stored here.
/// The `TaskStorage` contains only persistent fields (via `clone_snapshot()`), and has
/// `meta_modified`/`data_modified` flags set to indicate which categories need serializing.
/// Snapshot(None): /// Snapshot(None):
/// It was not modified before snapshot mode was entered, but it was accessed during snapshot /// It was not modified before snapshot mode was entered, but it was accessed during snapshot
/// mode. Or the snapshot was already taken out by the snapshot operation. /// mode. Or the snapshot was already taken out by the snapshot operation.
...@@ -342,258 +344,6 @@ impl DerefMut for StorageWriteGuard<'_> { ...@@ -342,258 +344,6 @@ impl DerefMut for StorageWriteGuard<'_> {
} }
} }
// TODO: this implementation is only needed to bootstrap new tasks
impl super::storage_schema::TaskStorageAccessors for StorageWriteGuard<'_> {
fn typed(&self) -> &super::storage_schema::TaskStorage {
&self.inner
}
fn typed_mut(&mut self) -> &mut super::storage_schema::TaskStorage {
&mut self.inner
}
fn track_modification(&mut self, category: SpecificTaskDataCategory) {
// Delegate to the existing track_modification method
StorageWriteGuard::track_modification(self, category)
}
fn check_access(&self, _category: super::TaskDataCategory) {
// StorageWriteGuard doesn't have category tracking - that's handled by TaskGuardImpl.
// This is a no-op for Stor
}
}
macro_rules! count {
($task:ident, $key:ident) => {{ $task.count($crate::data::CachedDataItemType::$key) }};
}
macro_rules! get {
($task:ident, $key:ident $input:tt) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
if let Some($crate::data::CachedDataItemValueRef::$key {
value,
}) = $task.get(&$crate::data::CachedDataItemKey::$key $input) {
Some(value)
} else {
None
}
}};
($task:ident, $key:ident) => {
$crate::backend::storage::get!($task, $key {})
};
}
macro_rules! get_mut {
($task:ident, $key:ident $input:tt) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
if let Some($crate::data::CachedDataItemValueRefMut::$key {
value,
}) = $task.get_mut(&$crate::data::CachedDataItemKey::$key $input) {
let () = $crate::data::allow_mut_access::$key;
Some(value)
} else {
None
}
}};
($task:ident, $key:ident) => {
$crate::backend::storage::get_mut!($task, $key {})
};
}
macro_rules! get_mut_or_insert_with {
($task:ident, $key:ident $input:tt, $f:expr) => {{
#[allow(unused_imports)]
use $crate::backend::operation::TaskGuard;
let () = $crate::data::allow_mut_access::$key;
let functor = $f;
let $crate::data::CachedDataItemValueRefMut::$key {
value,
} = $task.get_mut_or_insert_with($crate::data::CachedDataItemKey::$key $input, move || $crate::data::CachedDataItemValue::$key { value: functor() }) else {
unreachable!()
};
value
}};
($task:ident, $key:ident, $f:expr) => {
$crate::backend::storage::get_mut_or_insert_with!($task, $key {}, $f)
};
}
/// Creates an iterator over all [`CachedDataItemKey::$key`][crate::data::CachedDataItemKey]s in
/// `$task` matching the given `$key_pattern`, optional `$value_pattern`, and optional `if $cond`.
///
/// Each element in the iterator is determined by `$iter_item`, which may use fields extracted by
/// `$key_pattern` or `$value_pattern`.
macro_rules! iter_many {
($task:ident, $key:ident $key_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
$task
.iter($crate::data::CachedDataItemType::$key)
.filter_map(|(key, _)| match key {
$crate::data::CachedDataItemKey::$key $key_pattern $(if $cond)? => Some(
$iter_item
),
_ => None,
})
}};
($task:ident, $key:ident $input:tt $value_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
$task
.iter($crate::data::CachedDataItemType::$key)
.filter_map(|(key, value)| match (key, value) {
(
$crate::data::CachedDataItemKey::$key $input,
$crate::data::CachedDataItemValueRef::$key { value: $value_pattern }
) $(if $cond)? => Some($iter_item),
_ => None,
})
}};
}
/// A thin wrapper around [`iter_many`] that calls [`Iterator::collect`].
///
/// Note that the return type of [`Iterator::collect`] may be ambiguous in certain contexts, so
/// using this macro may require explicit type annotations on variables.
macro_rules! get_many {
($($args:tt)*) => {
$crate::backend::storage::iter_many!($($args)*).collect()
};
}
macro_rules! update {
($task:ident, $key:ident $input:tt, $update:expr) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
#[allow(unused_mut)]
let mut update = $update;
$task.update($crate::data::CachedDataItemKey::$key $input, |old| {
update(old.and_then(|old| {
if let $crate::data::CachedDataItemValue::$key { value } = old {
Some(value)
} else {
None
}
}))
.map(|new| $crate::data::CachedDataItemValue::$key { value: new })
})
}};
($task:ident, $key:ident, $update:expr) => {
$crate::backend::storage::update!($task, $key {}, $update)
};
}
macro_rules! update_count {
($task:ident, $key:ident $input:tt, -$update:expr) => {
match $update {
update => {
let mut state_change = false;
$crate::backend::storage::update!($task, $key $input, |old: Option<_>| {
#[allow(unused_comparisons, reason = "type of update might be unsigned, where update < 0 is always false")]
if let Some(old) = old {
let new = old - update;
state_change = old <= 0 && new > 0 || old > 0 && new <= 0;
(new != 0).then_some(new)
} else {
state_change = update < 0;
(update != 0).then_some(-update)
}
});
state_change
}
}
};
($task:ident, $key:ident $input:tt, $update:expr) => {
match $update {
update => {
let mut state_change = false;
$crate::backend::storage::update!($task, $key $input, |old: Option<_>| {
if let Some(old) = old {
let new = old + update;
state_change = old <= 0 && new > 0 || old > 0 && new <= 0;
(new != 0).then_some(new)
} else {
state_change = update > 0;
(update != 0).then_some(update)
}
});
state_change
}
}
};
($task:ident, $key:ident, -$update:expr) => {
$crate::backend::storage::update_count!($task, $key {}, -$update)
};
($task:ident, $key:ident, $update:expr) => {
$crate::backend::storage::update_count!($task, $key {}, $update)
};
}
macro_rules! update_count_and_get {
($task:ident, $key:ident $input:tt, -$update:expr) => {
match $update {
update => {
let mut new = 0;
$crate::backend::storage::update!($task, $key $input, |old: Option<_>| {
let old = old.unwrap_or(0);
new = old - update;
(new != 0).then_some(new)
});
new
}
}
};
($task:ident, $key:ident $input:tt, $update:expr) => {
match $update {
update => {
let mut new = 0;
$crate::backend::storage::update!($task, $key $input, |old: Option<_>| {
let old = old.unwrap_or(0);
new = old + update;
(new != 0).then_some(new)
});
new
}
}
};
($task:ident, $key:ident, -$update:expr) => {
$crate::backend::storage::update_count_and_get!($task, $key {}, -$update)
};
($task:ident, $key:ident, $update:expr) => {
$crate::backend::storage::update_count_and_get!($task, $key {}, $update)
};
}
macro_rules! remove {
($task:ident, $key:ident $input:tt) => {{
#[allow(unused_imports)]
use $crate::backend::storage_schema::TaskStorageAccessors;
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.remove(
&$crate::data::CachedDataItemKey::$key $input
) {
Some(value)
} else {
None
}
}};
($task:ident, $key:ident) => {
$crate::backend::storage::remove!($task, $key {})
};
}
pub(crate) use count;
pub(crate) use get;
pub(crate) use get_many;
pub(crate) use get_mut;
pub(crate) use get_mut_or_insert_with;
pub(crate) use iter_many;
pub(crate) use remove;
pub(crate) use update;
pub(crate) use update_count;
pub(crate) use update_count_and_get;
pub struct SnapshotGuard<'l> { pub struct SnapshotGuard<'l> {
storage: &'l Storage, storage: &'l Storage,
} }
......
...@@ -95,8 +95,8 @@ pub trait BackingStorageSealed: 'static + Send + Sync { ...@@ -95,8 +95,8 @@ pub trait BackingStorageSealed: 'static + Send + Sync {
storage: &mut TaskStorage, storage: &mut TaskStorage,
) -> Result<()>; ) -> Result<()>;
/// Batch lookup and decode data for multiple tasks directly into TaskStorage instances. /// Batch lookup and decode data for multiple tasks directly into TypedStorage instances.
/// Returns a vector of TaskStorage, one for each task_id in the input slice. /// Returns a vector of TypedStorage, one for each task_id in the input slice.
/// # Safety /// # Safety
/// ///
/// `tx` must be a transaction from this BackingStorage instance. /// `tx` must be a transaction from this BackingStorage instance.
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
use anyhow::Result; use anyhow::Result;
use turbo_tasks::{ResolvedVc, State, Vc}; use turbo_tasks::{ResolvedVc, State, Vc};
use turbo_tasks_testing::{Registration, register, run_once}; use turbo_tasks_testing::{Registration, register, run, run_once};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
...@@ -135,3 +135,103 @@ async fn compute2(input: Vc<ChangingInput>) -> Result<Vc<u32>> { ...@@ -135,3 +135,103 @@ async fn compute2(input: Vc<ChangingInput>) -> Result<Vc<u32>> {
let state_value = *input.await?.state.get(); let state_value = *input.await?.state.get();
Ok(Vc::cell(state_value)) Ok(Vc::cell(state_value))
} }
// ============================================================================
// recompute_dependency test - verifies dependent tasks re-execute correctly
// ============================================================================
/// Tests that when a task's dependency changes, both the inner and outer
/// tasks re-execute correctly and return updated values.
/// This tests the basic dependency propagation through a simple two-task chain.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn recompute_dependency() {
run(&REGISTRATION, || async {
let input = get_dependency_input().resolve().await?;
// Reset state to 1 at the start of each iteration (important for multi-run tests)
input.await?.state.set(1);
// Initial execution - establishes dependency chain:
// outer_compute -> inner_compute -> input.state
let output = outer_compute(input);
let read = output.strongly_consistent().await?;
println!(
"first read: value={}, inner_random={}, outer_random={}",
read.value, read.inner_random, read.outer_random
);
assert_eq!(read.value, 1);
let prev_inner_random = read.inner_random;
let prev_outer_random = read.outer_random;
// Change state - should invalidate inner_compute,
// which should then invalidate outer_compute
println!("changing input");
input.await?.state.set(2);
let read = output.strongly_consistent().await?;
println!(
"second read: value={}, inner_random={}, outer_random={}",
read.value, read.inner_random, read.outer_random
);
// Value should be updated
assert_eq!(read.value, 2);
// Inner task should have re-executed (different random)
assert_ne!(
read.inner_random, prev_inner_random,
"inner_compute should have re-executed"
);
// CRITICAL: Outer task should ALSO have re-executed
// This is what the bug broke - outer_compute wasn't being
// invalidated because its output_dependent edge was removed
assert_ne!(
read.outer_random, prev_outer_random,
"outer_compute should have re-executed due to dependency on inner_compute"
);
anyhow::Ok(())
})
.await
.unwrap();
}
#[turbo_tasks::function]
fn get_dependency_input() -> Vc<ChangingInput> {
ChangingInput {
state: State::new(1),
}
.cell()
}
#[turbo_tasks::value]
struct DependencyOutput {
value: u32,
inner_random: u32,
outer_random: u32,
}
/// Inner task - reads state directly, returns value with embedded random
#[turbo_tasks::function]
async fn inner_compute(input: Vc<ChangingInput>) -> Result<Vc<u32>> {
println!("inner_compute()");
let value = *input.await?.state.get();
// Combine value with random to detect re-execution
// Value in lower 16 bits, random in upper 16 bits
Ok(Vc::cell(value | (rand::random::<u32>() << 16)))
}
/// Outer task - depends on inner_compute
#[turbo_tasks::function]
async fn outer_compute(input: Vc<ChangingInput>) -> Result<Vc<DependencyOutput>> {
println!("outer_compute()");
let inner_result = *inner_compute(input).await?;
let value = inner_result & 0xFFFF;
let inner_random = inner_result >> 16;
Ok(DependencyOutput {
value,
inner_random,
outer_random: rand::random(),
}
.cell())
}
mod deterministic_hash_macro; mod deterministic_hash_macro;
mod key_value_pair_macro;
mod non_local_value_macro; mod non_local_value_macro;
mod operation_value_macro; mod operation_value_macro;
mod task_input_macro; mod task_input_macro;
...@@ -9,7 +8,6 @@ mod value_debug_format_macro; ...@@ -9,7 +8,6 @@ mod value_debug_format_macro;
mod value_debug_macro; mod value_debug_macro;
pub use deterministic_hash_macro::derive_deterministic_hash; pub use deterministic_hash_macro::derive_deterministic_hash;
pub use key_value_pair_macro::derive_key_value_pair;
pub use non_local_value_macro::derive_non_local_value; pub use non_local_value_macro::derive_non_local_value;
pub use operation_value_macro::derive_operation_value; pub use operation_value_macro::derive_operation_value;
use syn::{Attribute, Meta, Token, punctuated::Punctuated, spanned::Spanned}; use syn::{Attribute, Meta, Token, punctuated::Punctuated, spanned::Spanned};
......
...@@ -57,15 +57,6 @@ pub fn derive_task_input(input: TokenStream) -> TokenStream { ...@@ -57,15 +57,6 @@ pub fn derive_task_input(input: TokenStream) -> TokenStream {
derive::derive_task_input(input) derive::derive_task_input(input)
} }
/// <!--
/// Documentation for this macro is available on the re-export:
/// <https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks/derive.KeyValuePair.html>
/// -->
#[proc_macro_derive(KeyValuePair)]
pub fn derive_key_value_pair(input: TokenStream) -> TokenStream {
derive::derive_key_value_pair(input)
}
/// <!-- /// <!--
/// Documentation for this macro is available on the re-export: /// Documentation for this macro is available on the re-export:
/// <https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks/attr.task_storage.html> /// <https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks/attr.task_storage.html>
......
use std::fmt::Debug;
pub trait KeyValuePair {
type Type: Debug + Copy + Clone + PartialEq + Eq + std::hash::Hash;
type Key: Debug + Clone + PartialEq + Eq + std::hash::Hash;
type Value: Debug + Clone + Default + PartialEq + Eq;
type ValueRef<'l>: Debug + Copy + Clone + PartialEq + Eq + 'l
where
Self: 'l;
type ValueRefMut<'l>: Debug + PartialEq + Eq + 'l
where
Self: 'l;
fn ty(&self) -> Self::Type;
fn key(&self) -> Self::Key;
fn value(&self) -> Self::Value;
fn value_ref(&self) -> Self::ValueRef<'_>;
fn value_mut(&mut self) -> Self::ValueRefMut<'_>;
fn from_key_and_value(key: Self::Key, value: Self::Value) -> Self;
fn from_key_and_value_ref(key: Self::Key, value_ref: Self::ValueRef<'_>) -> Self;
fn into_key_and_value(self) -> (Self::Key, Self::Value);
}
/// Derives the [`KeyValuePair`][trait@KeyValuePair] trait for a enum. Each variant need to
/// have a `value` field which becomes part of the value enum and all remaining fields become
/// part of the key.
///
/// Assuming the enum is called `Abc` it exposes `AbcKey` and `AbcValue` types for it too. The
/// key enum will have [`Debug`], [`Clone`], [`PartialEq`], [`Eq`], and [`Hash`] derived. The
/// value enum will have [`Debug`] and [`Clone`] derived. It's expected that all fields
/// implement these traits.
pub use turbo_tasks_macros::KeyValuePair;
...@@ -54,7 +54,6 @@ mod id; ...@@ -54,7 +54,6 @@ mod id;
mod id_factory; mod id_factory;
mod invalidation; mod invalidation;
mod join_iter_ext; mod join_iter_ext;
mod key_value_pair;
pub mod keyed; pub mod keyed;
#[doc(hidden)] #[doc(hidden)]
pub mod macro_helpers; pub mod macro_helpers;
...@@ -110,7 +109,6 @@ pub use crate::{ ...@@ -110,7 +109,6 @@ pub use crate::{
get_invalidator, get_invalidator,
}, },
join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt}, join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt},
key_value_pair::KeyValuePair,
magic_any::MagicAny, magic_any::MagicAny,
manager::{ manager::{
CurrentCellRef, ReadCellTracking, ReadConsistency, ReadTracking, TaskPersistence, CurrentCellRef, ReadCellTracking, ReadConsistency, ReadTracking, TaskPersistence,
......
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