Skip to content

Commit 543a7ff

Browse files
committed
Use TransientCellData for non-serializable cells
1 parent bfc0d4f commit 543a7ff

File tree

20 files changed

+219
-66
lines changed

20 files changed

+219
-66
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
799799
}
800800
}
801801

802+
let ReadCellOptions {
803+
is_transient_cell,
804+
tracking,
805+
final_read_hint,
806+
} = options;
807+
802808
let mut ctx = self.execute_context(turbo_tasks);
803809
let (mut task, reader_task) = if self.should_track_dependencies()
804-
&& !matches!(options.tracking, ReadTracking::Untracked)
810+
&& !matches!(tracking, ReadTracking::Untracked)
805811
&& let Some(reader_id) = reader
806812
&& reader_id != task_id
807813
{
@@ -814,16 +820,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
814820
(ctx.task(task_id, TaskDataCategory::Data), None)
815821
};
816822

817-
let content = if options.final_read_hint {
818-
remove!(task, CellData { cell })
819-
} else if let Some(content) = get!(task, CellData { cell }) {
823+
let content = if final_read_hint {
824+
task.remove_cell_data(is_transient_cell, cell)
825+
} else if let Some(content) = task.get_cell_data(is_transient_cell, cell) {
820826
let content = content.clone();
821827
Some(content)
822828
} else {
823829
None
824830
};
825831
if let Some(content) = content {
826-
if options.tracking.should_track(false) {
832+
if tracking.should_track(false) {
827833
add_cell_dependency(task_id, task, reader, reader_task, cell);
828834
}
829835
return Ok(Ok(TypedCellContent(
@@ -850,7 +856,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
850856
)
851857
.copied();
852858
let Some(max_id) = max_id else {
853-
if options.tracking.should_track(true) {
859+
if tracking.should_track(true) {
854860
add_cell_dependency(task_id, task, reader, reader_task, cell);
855861
}
856862
bail!(
@@ -859,7 +865,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
859865
);
860866
};
861867
if cell.index >= max_id {
862-
if options.tracking.should_track(true) {
868+
if tracking.should_track(true) {
863869
add_cell_dependency(task_id, task, reader, reader_task, cell);
864870
}
865871
bail!(
@@ -1959,6 +1965,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
19591965
.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index))
19601966
}));
19611967
}
1968+
if task_id.is_transient() || iter_many!(task, TransientCellData { cell }
1969+
if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index) => cell
1970+
).count() > 0 {
1971+
removed_data.extend(task.extract_if(CachedDataItemType::TransientCellData, |key, _| {
1972+
matches!(key, CachedDataItemKey::TransientCellData { cell } if cell_counters
1973+
.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index))
1974+
}));
1975+
}
19621976

19631977
old_edges.extend(
19641978
task.iter(CachedDataItemType::OutdatedCollectible)
@@ -2388,6 +2402,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
23882402
fn task_execution_completed_cleanup(&self, ctx: &mut impl ExecuteContext<'_>, task_id: TaskId) {
23892403
let mut task = ctx.task(task_id, TaskDataCategory::All);
23902404
task.shrink_to_fit(CachedDataItemType::CellData);
2405+
task.shrink_to_fit(CachedDataItemType::TransientCellData);
23912406
task.shrink_to_fit(CachedDataItemType::CellTypeMaxIndex);
23922407
task.shrink_to_fit(CachedDataItemType::CellDependency);
23932408
task.shrink_to_fit(CachedDataItemType::OutputDependency);
@@ -2523,12 +2538,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
25232538
&self,
25242539
task_id: TaskId,
25252540
cell: CellId,
2526-
_options: ReadCellOptions,
2541+
options: ReadCellOptions,
25272542
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
25282543
) -> Result<TypedCellContent> {
25292544
let mut ctx = self.execute_context(turbo_tasks);
25302545
let task = ctx.task(task_id, TaskDataCategory::Data);
2531-
if let Some(content) = get!(task, CellData { cell }) {
2546+
if let Some(content) = task.get_cell_data(options.is_transient_cell, cell) {
25322547
Ok(CellContent(Some(content.reference.clone())).into_typed(cell.type_id))
25332548
} else {
25342549
Ok(CellContent(None).into_typed(cell.type_id))
@@ -2670,6 +2685,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
26702685
&self,
26712686
task_id: TaskId,
26722687
cell: CellId,
2688+
is_transient_cell: bool,
26732689
content: CellContent,
26742690
verification_mode: VerificationMode,
26752691
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
@@ -2678,6 +2694,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
26782694
task_id,
26792695
cell,
26802696
content,
2697+
is_transient_cell,
26812698
verification_mode,
26822699
self.execute_context(turbo_tasks),
26832700
);
@@ -3265,12 +3282,19 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
32653282
&self,
32663283
task_id: TaskId,
32673284
cell: CellId,
3285+
is_transient_cell: bool,
32683286
content: CellContent,
32693287
verification_mode: VerificationMode,
32703288
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
32713289
) {
3272-
self.0
3273-
.update_task_cell(task_id, cell, content, verification_mode, turbo_tasks);
3290+
self.0.update_task_cell(
3291+
task_id,
3292+
cell,
3293+
is_transient_cell,
3294+
content,
3295+
verification_mode,
3296+
turbo_tasks,
3297+
);
32743298
}
32753299

32763300
fn mark_own_task_as_finished(

turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ use std::{
1414
};
1515

1616
use serde::{Deserialize, Serialize};
17-
use turbo_tasks::{FxIndexMap, KeyValuePair, TaskId, TurboTasksBackendApi};
17+
use turbo_tasks::{
18+
CellId, FxIndexMap, KeyValuePair, TaskId, TurboTasksBackendApi, TypedSharedReference,
19+
};
1820

1921
use crate::{
2022
backend::{
2123
OperationGuard, TaskDataCategory, TransientTask, TurboTasksBackend, TurboTasksBackendInner,
2224
TurboTasksBackendJob,
23-
storage::{SpecificTaskDataCategory, StorageWriteGuard, get, iter_many},
25+
storage::{SpecificTaskDataCategory, StorageWriteGuard, get, iter_many, remove},
2426
},
2527
backing_storage::BackingStorage,
2628
data::{
@@ -467,6 +469,28 @@ pub trait TaskGuard: Debug {
467469
.unwrap_or_default();
468470
dirty_count < clean_count
469471
}
472+
fn remove_cell_data(
473+
&mut self,
474+
is_transient_cell: bool,
475+
cell: CellId,
476+
) -> Option<TypedSharedReference> {
477+
if is_transient_cell {
478+
remove!(self, TransientCellData { cell })
479+
} else {
480+
remove!(self, CellData { cell })
481+
}
482+
}
483+
fn get_cell_data(
484+
&self,
485+
is_transient_cell: bool,
486+
cell: CellId,
487+
) -> Option<&TypedSharedReference> {
488+
if is_transient_cell {
489+
get!(self, TransientCellData { cell })
490+
} else {
491+
get!(self, CellData { cell })
492+
}
493+
}
470494
}
471495

472496
pub struct TaskGuardImpl<'a, B: BackingStorage> {

turbopack/crates/turbo-tasks-backend/src/backend/operation/update_cell.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
1616
invalidate::make_task_dirty_internal,
1717
},
18-
storage::{get, get_many, remove},
18+
storage::{get_many, remove},
1919
},
2020
data::{CachedDataItem, CachedDataItemKey, CellRef},
2121
};
@@ -24,12 +24,14 @@ use crate::{
2424
#[allow(clippy::large_enum_variant)]
2525
pub enum UpdateCellOperation {
2626
InvalidateWhenCellDependency {
27+
is_transient_cell: bool,
2728
cell_ref: CellRef,
2829
dependent_tasks: SmallVec<[TaskId; 4]>,
2930
content: Option<TypedSharedReference>,
3031
queue: AggregationUpdateQueue,
3132
},
3233
FinalCellChange {
34+
is_transient_cell: bool,
3335
cell_ref: CellRef,
3436
content: Option<TypedSharedReference>,
3537
queue: AggregationUpdateQueue,
@@ -46,6 +48,7 @@ impl UpdateCellOperation {
4648
task_id: TaskId,
4749
cell: CellId,
4850
content: CellContent,
51+
is_transient_cell: bool,
4952
#[cfg(feature = "verify_determinism")] verification_mode: VerificationMode,
5053
#[cfg(not(feature = "verify_determinism"))] _verification_mode: VerificationMode,
5154
mut ctx: impl ExecuteContext,
@@ -64,7 +67,7 @@ impl UpdateCellOperation {
6467
let assume_unchanged =
6568
!ctx.should_track_dependencies() || !task.has_key(&CachedDataItemKey::Dirty {});
6669

67-
let old_content = get!(task, CellData { cell });
70+
let old_content = task.get_cell_data(is_transient_cell, cell);
6871

6972
if assume_unchanged {
7073
if old_content.is_some() {
@@ -115,12 +118,14 @@ impl UpdateCellOperation {
115118
// tasks and after that set the new cell content. When the cell content is unset,
116119
// readers will wait for it to be set via InProgressCell.
117120

118-
let old_content = task.remove(&CachedDataItemKey::CellData { cell });
121+
let old_content =
122+
task.remove(&CachedDataItemKey::cell_data(is_transient_cell, cell));
119123

120124
drop(task);
121125
drop(old_content);
122126

123127
UpdateCellOperation::InvalidateWhenCellDependency {
128+
is_transient_cell,
124129
cell_ref: CellRef {
125130
task: task_id,
126131
cell,
@@ -138,12 +143,13 @@ impl UpdateCellOperation {
138143
// So we can just update the cell content.
139144

140145
let old_content = if let Some(new_content) = content {
141-
task.insert(CachedDataItem::CellData {
146+
task.insert(CachedDataItem::cell_data(
147+
is_transient_cell,
142148
cell,
143-
value: new_content,
144-
})
149+
new_content,
150+
))
145151
} else {
146-
task.remove(&CachedDataItemKey::CellData { cell })
152+
task.remove(&CachedDataItemKey::cell_data(is_transient_cell, cell))
147153
};
148154

149155
let in_progress_cell = remove!(task, InProgressCell { cell });
@@ -163,6 +169,7 @@ impl Operation for UpdateCellOperation {
163169
ctx.operation_suspend_point(&self);
164170
match self {
165171
UpdateCellOperation::InvalidateWhenCellDependency {
172+
is_transient_cell,
166173
cell_ref,
167174
ref mut dependent_tasks,
168175
ref mut content,
@@ -203,24 +210,23 @@ impl Operation for UpdateCellOperation {
203210
}
204211
if dependent_tasks.is_empty() {
205212
self = UpdateCellOperation::FinalCellChange {
213+
is_transient_cell,
206214
cell_ref,
207215
content: take(content),
208216
queue: take(queue),
209217
};
210218
}
211219
}
212220
UpdateCellOperation::FinalCellChange {
221+
is_transient_cell,
213222
cell_ref: CellRef { task, cell },
214223
content,
215224
ref mut queue,
216225
} => {
217226
let mut task = ctx.task(task, TaskDataCategory::Data);
218227

219228
if let Some(content) = content {
220-
task.add_new(CachedDataItem::CellData {
221-
cell,
222-
value: content,
223-
})
229+
task.add_new(CachedDataItem::cell_data(is_transient_cell, cell, content));
224230
}
225231

226232
let in_progress_cell = remove!(task, InProgressCell { cell });

turbopack/crates/turbo-tasks-backend/src/data.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use turbo_tasks::{
55
ValueTypeId,
66
backend::TurboTasksExecutionError,
77
event::{Event, EventListener},
8-
registry,
98
};
109

1110
use crate::{
@@ -244,6 +243,10 @@ pub enum CachedDataItem {
244243
cell: CellId,
245244
value: TypedSharedReference,
246245
},
246+
TransientCellData {
247+
cell: CellId,
248+
value: TypedSharedReference,
249+
},
247250
CellTypeMaxIndex {
248251
cell_type: ValueTypeId,
249252
value: u32,
@@ -364,6 +367,14 @@ pub enum CachedDataItem {
364367
}
365368

366369
impl CachedDataItem {
370+
pub fn cell_data(is_transient: bool, cell: CellId, value: TypedSharedReference) -> Self {
371+
if is_transient {
372+
CachedDataItem::TransientCellData { cell, value }
373+
} else {
374+
CachedDataItem::CellData { cell, value }
375+
}
376+
}
377+
367378
pub fn is_persistent(&self) -> bool {
368379
match self {
369380
CachedDataItem::Output { value } => value.is_transient(),
@@ -374,6 +385,7 @@ impl CachedDataItem {
374385
CachedDataItem::CurrentSessionClean { .. } => false,
375386
CachedDataItem::Child { task, .. } => !task.is_transient(),
376387
CachedDataItem::CellData { .. } => true,
388+
CachedDataItem::TransientCellData { .. } => false,
377389
CachedDataItem::CellTypeMaxIndex { .. } => true,
378390
CachedDataItem::OutputDependency { target, .. } => !target.is_transient(),
379391
CachedDataItem::CellDependency { target, .. } => !target.task.is_transient(),
@@ -471,6 +483,7 @@ impl CachedDataItem {
471483
| Self::OutdatedOutputDependency { .. }
472484
| Self::OutdatedCellDependency { .. }
473485
| Self::OutdatedCollectiblesDependency { .. }
486+
| Self::TransientCellData { .. }
474487
| Self::CurrentSessionClean { .. }
475488
| Self::AggregatedCurrentSessionCleanContainer { .. }
476489
| Self::AggregatedCurrentSessionCleanContainerCount { .. }
@@ -486,6 +499,14 @@ impl CachedDataItem {
486499
}
487500

488501
impl CachedDataItemKey {
502+
pub fn cell_data(is_transient: bool, cell: CellId) -> Self {
503+
if is_transient {
504+
CachedDataItemKey::TransientCellData { cell }
505+
} else {
506+
CachedDataItemKey::CellData { cell }
507+
}
508+
}
509+
489510
pub fn is_persistent(&self) -> bool {
490511
match self {
491512
CachedDataItemKey::Output { .. } => true,
@@ -496,6 +517,7 @@ impl CachedDataItemKey {
496517
CachedDataItemKey::CurrentSessionClean { .. } => false,
497518
CachedDataItemKey::Child { task, .. } => !task.is_transient(),
498519
CachedDataItemKey::CellData { .. } => true,
520+
CachedDataItemKey::TransientCellData { .. } => false,
499521
CachedDataItemKey::CellTypeMaxIndex { .. } => true,
500522
CachedDataItemKey::OutputDependency { target, .. } => !target.is_transient(),
501523
CachedDataItemKey::CellDependency { target, .. } => !target.task.is_transient(),
@@ -561,6 +583,7 @@ impl CachedDataItemType {
561583
| Self::OutdatedOutputDependency { .. }
562584
| Self::OutdatedCellDependency { .. }
563585
| Self::OutdatedCollectiblesDependency { .. }
586+
| Self::TransientCellData { .. }
564587
| Self::CurrentSessionClean { .. }
565588
| Self::AggregatedCurrentSessionCleanContainer { .. }
566589
| Self::AggregatedCurrentSessionCleanContainerCount { .. }
@@ -600,6 +623,7 @@ impl CachedDataItemType {
600623
| Self::CurrentSessionClean
601624
| Self::AggregatedCurrentSessionCleanContainer
602625
| Self::AggregatedCurrentSessionCleanContainerCount
626+
| Self::TransientCellData
603627
| Self::OutdatedCollectible
604628
| Self::OutdatedOutputDependency
605629
| Self::OutdatedCellDependency
@@ -621,9 +645,6 @@ impl CachedDataItemValueRef<'_> {
621645
pub fn is_persistent(&self) -> bool {
622646
match self {
623647
CachedDataItemValueRef::Output { value } => !value.is_transient(),
624-
CachedDataItemValueRef::CellData { value } => {
625-
registry::get_value_type(value.type_id).is_serializable()
626-
}
627648
_ => true,
628649
}
629650
}

turbopack/crates/turbo-tasks-macros/src/generic_type_macro.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn generic_type(input: TokenStream) -> TokenStream {
8080
quote! {
8181
turbo_tasks::ValueType::new_with_any_serialization::<#repr>(#name)
8282
},
83+
quote! { true },
8384
);
8485

8586
quote! {

0 commit comments

Comments
 (0)