Unverified Commit 75a12436 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks: Drop output_dependent on immutable task completion (#92683)

### What?

Extend the `cleanup_after_execution` macro codegen to support
`drop_on_completion_if_immutable` on **inline** collection fields
(previously only lazy fields were supported), and apply it to
`output_dependent` in the task storage schema.

### Why?

`output_dependent` tracks which tasks depend on a given task's output,
used for invalidation and leaf distance propagation. For immutable
tasks:

- **No invalidation needed** — the output never changes, so dependents
never need to be dirtied via this edge.
- **No new readers added** — `try_read_task_output` already guards with
`!task.immutable()` at `mod.rs:742`, so the set is frozen after
completion.
- **Leaf distance propagation** — immutable tasks can be treated as
non-participants in the leaf distance graph; propagation simply stops at
immutable nodes.

This matches the existing treatment of `cell_dependents`, which already
has `drop_on_completion_if_immutable` for the same reasons (immutable
tasks' cells don't change).

### How?

**Macro change**
(`turbo-tasks-macros/src/derive/task_storage_macro.rs`):

The `generate_cleanup_after_execution` function previously only handled
`shrink_on_completion` for inline fields. Now it also handles
`drop_on_completion_if_immutable`: when the task is immutable, it emits
`typed.field_name = Default::default()` which replaces the `AutoSet`
with an empty one, releasing any heap allocation (`Box<HashMap>` or
spilled `SmallVec` buffer). When mutable, it falls back to
`shrink_to_fit()`.

Note: unlike lazy fields which are physically removed from
`Vec<LazyField>`, inline fields always occupy their struct slot — but
`Default::default()` ensures zero heap overhead (the `SmallVec<[_; 1]>`
reverts to inline storage).

**Schema change** (`turbo-tasks-backend/src/backend/storage_schema.rs`):

Added `drop_on_completion_if_immutable` to the `#[field(...)]` attribute
on `output_dependent`.

<!-- NEXT_JS_LLM_PR -->

---------
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
parent db0fdc30
...@@ -74,7 +74,13 @@ struct TaskStorageSchema { ...@@ -74,7 +74,13 @@ struct TaskStorageSchema {
/// Tasks that depend on this task's output. /// Tasks that depend on this task's output.
#[field(storage = "auto_set", category = "data", inline, filter_transient)] #[field(
storage = "auto_set",
category = "data",
inline,
filter_transient,
drop_on_completion_if_immutable
)]
output_dependent: AutoSet<TaskId>, output_dependent: AutoSet<TaskId>,
/// The task's output value. /// The task's output value.
......
...@@ -537,6 +537,38 @@ fn parse_field_storage_attributes(field: &syn::Field) -> FieldInfo { ...@@ -537,6 +537,38 @@ fn parse_field_storage_attributes(field: &syn::Field) -> FieldInfo {
} }
}; };
// Validate that shrink_on_completion and drop_on_completion_if_immutable are only used on
// collection types (auto_set, auto_map, counter_map) for inline fields.
// Lazy non-collection fields can still use drop_on_completion_if_immutable (removes from the
// lazy vec), but shrink_on_completion is meaningless for non-collection types.
let is_collection = matches!(
storage_type,
StorageType::AutoSet | StorageType::AutoMap | StorageType::CounterMap
);
if !is_collection {
if shrink_on_completion {
field_name
.span()
.unwrap()
.error(format!(
"`shrink_on_completion` on field `{field_name}` has no effect: only \
collection types (auto_set, auto_map, counter_map) support shrinking"
))
.emit();
}
if inline && drop_on_completion_if_immutable {
field_name
.span()
.unwrap()
.error(format!(
"`drop_on_completion_if_immutable` on inline field `{field_name}` has no \
effect: only inline collection types (auto_set, auto_map, counter_map) \
support dropping"
))
.emit();
}
}
FieldInfo { FieldInfo {
is_pub, is_pub,
field_name, field_name,
...@@ -2442,23 +2474,25 @@ fn generate_automap_ops(field: &FieldInfo) -> TokenStream { ...@@ -2442,23 +2474,25 @@ fn generate_automap_ops(field: &FieldInfo) -> TokenStream {
/// 4. For fields with `shrink_on_completion`: shrink or remove if empty /// 4. For fields with `shrink_on_completion`: shrink or remove if empty
/// 5. For fields with `drop_on_completion_if_immutable` when task is immutable: remove /// 5. For fields with `drop_on_completion_if_immutable` when task is immutable: remove
fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStream { fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStream {
// Generate shrink calls for inline collection fields with shrink_on_completion // Generate cleanup calls for inline collection fields.
let mut inline_shrinks = Vec::new(); // Invalid attribute combinations (e.g. shrink/drop on non-collection fields) are rejected
// during parsing, so we can generate code unconditionally here.
let mut inline_cleanups = Vec::new();
for field in grouped_fields.all_inline() { for field in grouped_fields.all_inline() {
if field.is_flag() { if field.is_flag() {
continue; continue;
} }
if !field.shrink_on_completion { let field_name = &field.field_name;
continue; if field.drop_on_completion_if_immutable {
} inline_cleanups.push(quote! {
// Only collection types can be shrunk if is_immutable {
let is_collection = matches!( typed.#field_name = Default::default();
field.storage_type, } else {
StorageType::AutoSet | StorageType::AutoMap | StorageType::CounterMap typed.#field_name.shrink_to_fit();
); }
if is_collection { });
let field_name = &field.field_name; } else if field.shrink_on_completion {
inline_shrinks.push(quote! { inline_cleanups.push(quote! {
typed.#field_name.shrink_to_fit(); typed.#field_name.shrink_to_fit();
}); });
} }
...@@ -2467,8 +2501,9 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre ...@@ -2467,8 +2501,9 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre
// Generate match arms for lazy fields that have cleanup attributes // Generate match arms for lazy fields that have cleanup attributes
let mut match_arms = Vec::new(); let mut match_arms = Vec::new();
// Invalid attribute combinations (e.g. shrink on non-collection fields) are rejected
// during parsing, so we can simplify the match here.
for field in grouped_fields.all_lazy() { for field in grouped_fields.all_lazy() {
// Skip flags - they're in the bitfield, not the lazy vec
if field.is_flag() { if field.is_flag() {
continue; continue;
} }
...@@ -2477,57 +2512,45 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre ...@@ -2477,57 +2512,45 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre
let shrink = field.shrink_on_completion; let shrink = field.shrink_on_completion;
let drop_if_immutable = field.drop_on_completion_if_immutable; let drop_if_immutable = field.drop_on_completion_if_immutable;
// Skip fields with no cleanup attributes
if !shrink && !drop_if_immutable { if !shrink && !drop_if_immutable {
continue; continue;
} }
// Determine whether this is a collection type that can be shrunk
let is_collection = matches!( let is_collection = matches!(
field.storage_type, field.storage_type,
StorageType::AutoSet | StorageType::AutoMap | StorageType::CounterMap StorageType::AutoSet | StorageType::AutoMap | StorageType::CounterMap
); );
// Each arm returns bool: true = keep, false = remove // Each arm returns bool: true = keep, false = remove
let arm_body = match (shrink, drop_if_immutable, is_collection) { let arm_body = if drop_if_immutable && shrink && is_collection {
// shrink_on_completion + drop_on_completion_if_immutable + collection // Drop for immutable, shrink or remove-if-empty for mutable
(true, true, true) => quote! { quote! {
if is_immutable { if is_immutable {
false // drop for immutable tasks false
} else if c.is_empty() { } else if c.is_empty() {
false // remove empty false
} else { } else {
c.shrink_to_fit(); c.shrink_to_fit();
true // keep true
} }
}, }
// shrink_on_completion only + collection } else if shrink && is_collection {
(true, false, true) => quote! { // Shrink or remove-if-empty
quote! {
if c.is_empty() { if c.is_empty() {
false // remove empty false
} else { } else {
c.shrink_to_fit(); c.shrink_to_fit();
true // keep true
} }
}, }
// drop_on_completion_if_immutable only + collection } else if drop_if_immutable {
(false, true, true) => quote! { // Drop for immutable (works for both collection and direct values)
!is_immutable // keep if mutable, drop if immutable quote! {
}, !is_immutable
// shrink_on_completion + drop_on_completion_if_immutable + direct value }
(true, true, false) => quote! { } else {
!is_immutable // keep if mutable, drop if immutable continue;
},
// shrink_on_completion only + direct value (unusual but handle it)
(true, false, false) => quote! {
true // keep (direct values don't need shrinking)
},
// drop_on_completion_if_immutable only + direct value
(false, true, false) => quote! {
!is_immutable // keep if mutable, drop if immutable
},
// No attributes (shouldn't reach here due to continue above)
(false, false, _) => unreachable!(),
}; };
match_arms.push(quote! { match_arms.push(quote! {
...@@ -2551,8 +2574,8 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre ...@@ -2551,8 +2574,8 @@ fn generate_cleanup_after_execution(grouped_fields: &GroupedFields) -> TokenStre
let typed = self.typed_mut(); let typed = self.typed_mut();
let is_immutable = typed.flags.immutable(); let is_immutable = typed.flags.immutable();
// Shrink inline collection fields (always present, not in lazy vec) // Clean up inline collection fields (always present, not in lazy vec)
#(#inline_shrinks)* #(#inline_cleanups)*
// swap_retain pattern: iterate with manual index, swap_remove to delete // swap_retain pattern: iterate with manual index, swap_remove to delete
let mut i = 0; let mut i = 0;
......
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