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> {
{
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)> {
self.0.iter()
}
......
......@@ -7,7 +7,7 @@ use turbo_tasks::TaskId;
use crate::{
backend::{
TaskDataCategory, get, get_many,
TaskDataCategory,
operation::{
AggregatedDataUpdate, ExecuteContext, Operation,
aggregation_update::{
......@@ -15,10 +15,9 @@ use crate::{
get_aggregation_number, get_uppers, is_aggregating_node,
},
},
storage::update_count,
storage_schema::TaskStorageAccessors,
},
data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef},
data::{CellRef, CollectibleRef, CollectiblesRef},
};
#[derive(Encode, Decode, Clone, Default)]
......@@ -50,7 +49,7 @@ impl CleanupOldEdgesOperation {
task_id: TaskId,
outdated: Vec<OutdatedEdge>,
queue: AggregationUpdateQueue,
ctx: &mut impl ExecuteContext,
ctx: &mut impl ExecuteContext<'_>,
) {
CleanupOldEdgesOperation::RemoveEdges {
task_id,
......@@ -62,7 +61,7 @@ impl CleanupOldEdgesOperation {
}
impl Operation for CleanupOldEdgesOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) {
fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop {
ctx.operation_suspend_point(&self);
match self {
......@@ -84,8 +83,8 @@ impl Operation for CleanupOldEdgesOperation {
_ => true,
});
let mut task = ctx.task(task_id, TaskDataCategory::All);
for &child_id in children.iter() {
task.remove(&CachedDataItemKey::Child { task: child_id });
for task_id in children.iter() {
task.remove_children(task_id);
}
if is_aggregating_node(get_aggregation_number(&task)) {
queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers {
......@@ -96,7 +95,8 @@ impl Operation for CleanupOldEdgesOperation {
} else {
let upper_ids = get_uppers(&task);
let has_active_count = ctx.should_track_activeness()
&& get!(task, Activeness)
&& task
.get_activeness()
.is_some_and(|a| a.active_counter > 0);
drop(task);
if has_active_count {
......@@ -127,19 +127,20 @@ impl Operation for CleanupOldEdgesOperation {
let mut task = ctx.task(task_id, TaskDataCategory::All);
let mut emptied_collectables = FxHashSet::default();
for (collectible, count) in collectibles.iter_mut() {
if update_count!(
task,
Collectible {
collectible: *collectible
},
*count
) {
if task
.update_collectibles_positive_crossing(*collectible, *count)
{
emptied_collectables.insert(collectible.collectible_type);
}
}
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(
AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids,
......@@ -162,21 +163,17 @@ impl Operation for CleanupOldEdgesOperation {
) => {
{
let mut task = ctx.task(cell_task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CellDependent {
cell,
key,
task: task_id,
});
task.remove_cell_dependents(&(cell, key, task_id));
}
{
let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CellDependency {
target: CellRef {
task.remove_cell_dependencies(&(
CellRef {
task: cell_task_id,
cell,
},
key,
});
));
}
}
OutdatedEdge::OutputDependency(output_task_id) => {
......@@ -189,15 +186,11 @@ impl Operation for CleanupOldEdgesOperation {
.entered();
{
let mut task = ctx.task(output_task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::OutputDependent {
task: task_id,
});
task.remove_output_dependent(&task_id);
}
{
let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::OutputDependency {
target: output_task_id,
});
task.remove_output_dependencies(&output_task_id);
}
}
OutdatedEdge::CollectiblesDependency(CollectiblesRef {
......@@ -207,18 +200,16 @@ impl Operation for CleanupOldEdgesOperation {
{
let mut task =
ctx.task(dependent_task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CollectiblesDependent {
task.remove_collectibles_dependents(&(
collectible_type,
task: task_id,
});
task_id,
));
}
{
let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove(&CachedDataItemKey::CollectiblesDependency {
target: CollectiblesRef {
collectible_type,
task: dependent_task_id,
},
task.remove_collectibles_dependencies(&CollectiblesRef {
collectible_type,
task: dependent_task_id,
});
}
}
......
......@@ -3,14 +3,14 @@ use turbo_tasks::{TaskExecutionReason, TaskId};
use crate::{
backend::{
TaskDataCategory, get_mut,
TaskDataCategory,
operation::{
ExecuteContext, Operation,
ExecuteContext, Operation, TaskGuard,
aggregation_update::{AggregationUpdateJob, AggregationUpdateQueue},
},
storage_schema::TaskStorageAccessors,
},
data::{CachedDataItem, CachedDataItemKey, InProgressState, InProgressStateInner},
data::{InProgressState, InProgressStateInner},
};
#[derive(Encode, Decode, Clone, Default)]
......@@ -33,7 +33,7 @@ impl ConnectChildOperation {
let mut parent_task = ctx.task(parent_task_id, TaskDataCategory::All);
let Some(InProgressState::InProgress(box InProgressStateInner {
new_children, ..
})) = get_mut!(parent_task, InProgress)
})) = parent_task.get_in_progress_mut()
else {
panic!("Task is not in progress while calling another task: {parent_task:?}");
};
......@@ -43,9 +43,7 @@ impl ConnectChildOperation {
return;
}
if parent_task.has_key(&CachedDataItemKey::Child {
task: child_task_id,
}) {
if parent_task.children_contains(&child_task_id) {
// It is already connected, we can skip the rest
return;
}
......@@ -69,11 +67,10 @@ impl ConnectChildOperation {
} else {
let mut child_task = ctx.task(child_task_id, TaskDataCategory::All);
if !child_task.has_key(&CachedDataItemKey::Output {})
&& child_task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::Connect,
|| ctx.get_task_desc_fn(child_task_id),
))
if !child_task.has_output()
&& child_task.add_scheduled(TaskExecutionReason::Connect, || {
ctx.get_task_desc_fn(child_task_id)
})
{
ctx.schedule_task(child_task, ctx.get_current_task_priority());
}
......@@ -87,7 +84,7 @@ impl ConnectChildOperation {
}
impl Operation for ConnectChildOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) {
fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop {
ctx.operation_suspend_point(&self);
match self {
......
......@@ -6,13 +6,10 @@ use turbo_tasks::{
util::{good_chunk_size, into_chunks},
};
use crate::{
backend::operation::{
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext,
Operation, TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob,
get_aggregation_number, get_uppers, is_aggregating_node,
},
data::{CachedDataItem, CachedDataItemType},
use crate::backend::operation::{
AggregationUpdateJob, AggregationUpdateQueue, ChildExecuteContext, ExecuteContext, Operation,
TaskGuard, aggregation_update::InnerOfUppersHasNewFollowersJob, get_aggregation_number,
get_uppers, is_aggregating_node,
};
pub fn connect_children(
......@@ -27,12 +24,13 @@ pub fn connect_children(
let parent_aggregation = get_aggregation_number(&parent_task);
parent_task.extend_new(
CachedDataItemType::Child,
new_children.iter().map(|&new_child| CachedDataItem::Child {
task: new_child,
value: (),
}),
let old_children = parent_task.children_len();
parent_task.extend_children(new_children.iter().copied());
debug_assert!(
old_children + new_children.len() == parent_task.children_len(),
"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();
......
......@@ -11,9 +11,8 @@ use crate::{
AggregationUpdateJob, AggregationUpdateQueue, ComputeDirtyAndCleanUpdate,
},
},
storage::{get, get_mut, remove},
},
data::{CachedDataItem, CachedDataItemKey, Dirtyness, InProgressState, InProgressStateInner},
data::{Dirtyness, InProgressState, InProgressStateInner},
};
#[derive(Encode, Decode, Clone, Default)]
......@@ -35,7 +34,7 @@ impl InvalidateOperation {
pub fn run(
task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
mut ctx: impl ExecuteContext,
mut ctx: impl ExecuteContext<'_>,
) {
InvalidateOperation::MakeDirty {
task_ids,
......@@ -47,7 +46,7 @@ impl InvalidateOperation {
}
impl Operation for InvalidateOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) {
fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop {
ctx.operation_suspend_point(&self);
match self {
......@@ -183,7 +182,7 @@ pub fn make_task_dirty(
task_id: TaskId,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext,
ctx: &mut impl ExecuteContext<'_>,
) {
if ctx.is_once_task(task_id) {
return;
......@@ -208,12 +207,12 @@ pub fn make_task_dirty_internal(
make_stale: bool,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
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
// immutable.
#[cfg(any(debug_assertions, feature = "verify_immutable"))]
if task.is_immutable() {
if task.immutable() {
#[cfg(feature = "trace_task_dirty")]
let extra_info = format!(
" Invalidation cause: {}",
......@@ -231,7 +230,7 @@ pub fn make_task_dirty_internal(
if make_stale
&& let Some(InProgressState::InProgress(box InProgressStateInner { stale, .. })) =
get_mut!(task, InProgress)
task.get_in_progress_mut()
&& !*stale
{
#[cfg(feature = "trace_task_dirty")]
......@@ -244,7 +243,7 @@ pub fn make_task_dirty_internal(
.entered();
*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 {
Some(Dirtyness::Dirty(current_priority)) => {
#[cfg(feature = "trace_task_dirty")]
......@@ -257,22 +256,19 @@ pub fn make_task_dirty_internal(
.entered();
// already dirty
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
task.insert(CachedDataItem::Dirty {
value: Dirtyness::Dirty(parent_priority),
});
task.set_dirty(Dirtyness::Dirty(parent_priority));
}
return;
}
Some(Dirtyness::SessionDependent) => {
let parent_priority = ctx.get_current_task_priority();
task.insert(CachedDataItem::Dirty {
value: Dirtyness::Dirty(parent_priority),
});
task.set_dirty(Dirtyness::Dirty(parent_priority));
// 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 {
task.set_current_session_clean(false);
// There was a clean count for a session. If it was the current session, we need to
// propagate that change.
(true, true, parent_priority)
......@@ -290,9 +286,7 @@ pub fn make_task_dirty_internal(
}
None => {
let parent_priority = ctx.get_current_task_priority();
task.insert(CachedDataItem::Dirty {
value: Dirtyness::Dirty(parent_priority),
});
task.set_dirty(Dirtyness::Dirty(parent_priority));
// It was clean before, so we need to increase the dirty count
(false, false, parent_priority)
}
......@@ -301,13 +295,14 @@ pub fn make_task_dirty_internal(
let new_self_dirty = true;
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()
.unwrap_or_default();
let current_session_clean_container_count =
get!(task, AggregatedCurrentSessionCleanContainerCount)
.copied()
.unwrap_or_default();
#[cfg(feature = "trace_task_dirty")]
let _span = tracing::trace_span!(
......@@ -337,18 +332,15 @@ pub fn make_task_dirty_internal(
));
}
let should_schedule =
!ctx.should_track_activeness() || task.has_key(&CachedDataItemKey::Activeness {});
let should_schedule = !ctx.should_track_activeness() || task.has_activeness();
if should_schedule {
let description = || ctx.get_task_desc_fn(task_id);
if task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::Invalidated,
description,
)) {
drop(task);
let task = ctx.task(task_id, TaskDataCategory::All);
ctx.schedule_task(task, parent_priority);
}
if should_schedule
&& task.add_scheduled(TaskExecutionReason::Invalidated, || {
ctx.get_task_desc_fn(task_id)
})
{
drop(task);
let task = ctx.task(task_id, TaskDataCategory::All);
ctx.schedule_task(task, parent_priority);
}
}
......@@ -9,14 +9,10 @@ use rustc_hash::FxHashMap;
use tracing::{span::Span, trace_span};
use turbo_tasks::TaskId;
use crate::{
backend::{
TaskDataCategory,
operation::{ExecuteContext, Operation},
storage::{get, iter_many},
storage_schema::TaskStorageAccessors,
},
data::CachedDataItem,
use crate::backend::{
TaskDataCategory,
operation::{ExecuteContext, Operation},
storage_schema::TaskStorageAccessors,
};
/// The maximum number of leaf distance updates to process before yielding back to the executor.
......@@ -148,7 +144,7 @@ impl LeafDistanceUpdateQueue {
TaskDataCategory::Data,
);
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 {
// It is strictly monotonic. No need to update.
return;
......@@ -165,22 +161,17 @@ impl LeafDistanceUpdateQueue {
// We are within the buffer zone, keep the max as is
leaf_distance.distance = dependencies_distance + 1;
}
let dependents = iter_many!(task, OutputDependent { task } => task)
// TODO Technically this is also needed, but there are cycles in the CellDependent graph
// So we need to handle that properly first
// When enabling this, make sure to also call the leaf update queue when adding CellDependents
// .chain(iter_many!(task, CellDependent { task, .. } => task))
;
for dependent_id in dependents {
// TODO Technically CellDependent is also needed, but there are cycles in the CellDependent
// graph. So we need to handle that properly first. When enabling this, make sure to also
// call the leaf update queue when adding CellDependents.
for dependent_id in task.iter_output_dependent() {
self.push(
dependent_id,
leaf_distance.distance,
leaf_distance.max_distance_in_buffer,
);
}
task.insert(CachedDataItem::LeafDistance {
value: leaf_distance,
});
task.set_leaf_distance(leaf_distance);
}
}
......
......@@ -3,11 +3,8 @@ use std::{cmp::max, num::NonZeroU32};
use rustc_hash::FxHashSet;
use turbo_tasks::TaskId;
use crate::backend::{
get,
operation::{
AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
},
use crate::backend::operation::{
AggregationUpdateJob, AggregationUpdateQueue, TaskGuard, is_aggregating_node, is_root_node,
};
const AGGREGATION_NUMBER_BUFFER_SPACE: u32 = 3;
......@@ -22,7 +19,8 @@ pub fn prepare_new_children(
let children_count = new_children.len();
// 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()
.unwrap_or_default();
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
use crate::backend::operation::invalidate::TaskDirtyCause;
use crate::{
backend::{
TaskDataCategory, TaskStorageAccessors,
TaskDataCategory,
operation::{
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
invalidate::make_task_dirty_internal,
},
storage::{iter_many, remove},
storage_schema::TaskStorageAccessors,
},
data::{CachedDataItem, CachedDataItemKey, CellRef},
data::CellRef,
};
#[derive(Encode, Decode, Clone, Default)]
......@@ -57,7 +57,7 @@ impl UpdateCellOperation {
updated_key_hashes: Option<SmallVec<[u64; 2]>>,
#[cfg(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 {
Some(new_content.into_typed(cell.type_id))
......@@ -70,8 +70,7 @@ impl UpdateCellOperation {
// 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
// assume that tasks are deterministic and pure.
let assume_unchanged =
!ctx.should_track_dependencies() || !task.has_key(&CachedDataItemKey::Dirty {});
let assume_unchanged = !ctx.should_track_dependencies() || !task.has_dirty();
if assume_unchanged {
let has_old_content = task.has_cell_data(is_serializable_cell_content, cell);
......@@ -108,20 +107,21 @@ impl UpdateCellOperation {
Lazy::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
});
let tasks_with_keys = iter_many!(
task,
CellDependent { cell: dependent_cell, key, task }
if dependent_cell == cell && key.is_none_or(|key_hash| {
updated_key_hashes_set.as_ref().is_none_or(|set| {
set.contains(&key_hash)
})
let tasks_with_keys = task
.iter_cell_dependents()
.filter(|&(dependent_cell, key, _)| {
dependent_cell == cell
&& key.is_none_or(|key_hash| {
updated_key_hashes_set
.as_ref()
.is_none_or(|set| set.contains(&key_hash))
})
})
=> (task, key)
)
.filter(|&(dependent_task_id, _)| {
// once tasks are never invalidated
!ctx.is_once_task(dependent_task_id)
});
.map(|(_, key, task)| (task, key))
.filter(|&(dependent_task_id, _)| {
// once tasks are never invalidated
!ctx.is_once_task(dependent_task_id)
});
let mut dependent_tasks: FxIndexMap<TaskId, SmallVec<[Option<u64>; 2]>> =
FxIndexMap::default();
for (task, key) in tasks_with_keys {
......@@ -142,10 +142,7 @@ impl UpdateCellOperation {
// 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.
let old_content = task.remove(&CachedDataItemKey::cell_data(
is_serializable_cell_content,
cell,
));
let old_content = task.remove_cell_data(is_serializable_cell_content, cell);
drop(task);
drop(old_content);
......@@ -177,19 +174,12 @@ impl UpdateCellOperation {
// So we can just update the cell content.
let old_content = if let Some(new_content) = content {
task.insert(CachedDataItem::cell_data(
is_serializable_cell_content,
cell,
new_content,
))
task.set_cell_data(is_serializable_cell_content, cell, new_content)
} else {
task.remove(&CachedDataItemKey::cell_data(
is_serializable_cell_content,
cell,
))
task.remove_cell_data(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(old_content);
......@@ -216,7 +206,7 @@ impl UpdateCellOperation {
}
impl Operation for UpdateCellOperation {
fn execute(mut self, ctx: &mut impl ExecuteContext) {
fn execute(mut self, ctx: &mut impl ExecuteContext<'_>) {
loop {
if self.is_serializable() {
ctx.operation_suspend_point(&self);
......@@ -235,20 +225,14 @@ impl Operation for UpdateCellOperation {
let mut make_stale = false;
let dependent = ctx.task(dependent_task_id, TaskDataCategory::All);
for key in keys.iter().copied() {
if dependent.has_key(&CachedDataItemKey::OutdatedCellDependency {
target: cell_ref,
key,
}) {
if dependent.outdated_cell_dependencies_contains(&(cell_ref, key)) {
// cell dependency is outdated, so it hasn't read the cell yet
// and doesn't need to be invalidated.
// 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
// no longer be considered as
// "recomputation".
} else if !dependent.has_key(&CachedDataItemKey::CellDependency {
target: cell_ref,
key,
}) {
} else if !dependent.cell_dependencies_contains(&(cell_ref, key)) {
// cell dependency has been removed, so the task doesn't depend on
// the cell anymore and doesn't need
// to be invalidated
......@@ -288,14 +272,10 @@ impl Operation for UpdateCellOperation {
let mut task = ctx.task(task, TaskDataCategory::Data);
if let Some(content) = content {
task.add_new(CachedDataItem::cell_data(
is_serializable_cell_content,
cell,
content,
));
task.add_cell_data(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);
......
......@@ -5,12 +5,12 @@ use turbo_tasks::TaskId;
use crate::{
backend::{
TaskDataCategory, get_many,
TaskDataCategory,
operation::{
AggregatedDataUpdate, AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext,
Operation, get_aggregation_number, is_root_node,
},
storage::{get, update_count},
storage_schema::TaskStorageAccessors,
},
data::CollectibleRef,
};
......@@ -22,7 +22,7 @@ impl UpdateCollectibleOperation {
task_id: TaskId,
collectible: CollectibleRef,
mut count: i32,
mut ctx: impl ExecuteContext,
mut ctx: impl ExecuteContext<'_>,
) {
let mut task = ctx.task(task_id, TaskDataCategory::All);
if count < 0 {
......@@ -52,32 +52,27 @@ impl UpdateCollectibleOperation {
}
}
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 count > 0 && outdated > 0 {
let shared = min(count, outdated);
update_count!(task, OutdatedCollectible { collectible }, -shared);
let _ = task.update_outdated_collectibles_positive_crossing(collectible, -shared);
count -= shared;
} else if count < 0 && outdated < 0 {
let shared = min(-count, -outdated);
update_count!(task, OutdatedCollectible { collectible }, shared);
let _ = task.update_outdated_collectibles_positive_crossing(collectible, shared);
count += shared;
} else {
// Not reduced from outdated
}
}
if count != 0 {
if update_count!(task, Collectible { collectible }, count) {
if task.update_collectibles_positive_crossing(collectible, count) {
let ty = collectible.collectible_type;
let dependent: SmallVec<[TaskId; 4]> = get_many!(
task,
CollectiblesDependent {
collectible_type,
task,
} if collectible_type == ty => {
task
}
);
let dependent: SmallVec<[TaskId; 4]> = task
.iter_collectibles_dependents()
.filter_map(|(collectible_type, task)| (collectible_type == ty).then_some(task))
.collect();
if !dependent.is_empty() {
queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids: dependent,
......
......@@ -64,6 +64,8 @@ enum ModifiedState {
/// Snapshot(Some):
/// 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.
/// 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):
/// 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.
......@@ -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> {
storage: &'l Storage,
}
......
......@@ -95,8 +95,8 @@ pub trait BackingStorageSealed: 'static + Send + Sync {
storage: &mut TaskStorage,
) -> Result<()>;
/// Batch lookup and decode data for multiple tasks directly into TaskStorage instances.
/// Returns a vector of TaskStorage, one for each task_id in the input slice.
/// Batch lookup and decode data for multiple tasks directly into TypedStorage instances.
/// Returns a vector of TypedStorage, one for each task_id in the input slice.
/// # Safety
///
/// `tx` must be a transaction from this BackingStorage instance.
......
......@@ -4,7 +4,7 @@
use anyhow::Result;
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!();
......@@ -135,3 +135,103 @@ async fn compute2(input: Vc<ChangingInput>) -> Result<Vc<u32>> {
let state_value = *input.await?.state.get();
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 key_value_pair_macro;
mod non_local_value_macro;
mod operation_value_macro;
mod task_input_macro;
......@@ -9,7 +8,6 @@ mod value_debug_format_macro;
mod value_debug_macro;
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 operation_value_macro::derive_operation_value;
use syn::{Attribute, Meta, Token, punctuated::Punctuated, spanned::Spanned};
......
......@@ -57,15 +57,6 @@ pub fn derive_task_input(input: TokenStream) -> TokenStream {
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:
/// <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;
mod id_factory;
mod invalidation;
mod join_iter_ext;
mod key_value_pair;
pub mod keyed;
#[doc(hidden)]
pub mod macro_helpers;
......@@ -110,7 +109,6 @@ pub use crate::{
get_invalidator,
},
join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt},
key_value_pair::KeyValuePair,
magic_any::MagicAny,
manager::{
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