Unverified Commit 02f8ba61 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

feat: add TaskStorage derive macro and schema infrastructure (#88338)

Add a new derive macro for `TaskStorage` a replacement for `InnerStorage` and `CachedDataItem`, currently it is unused outside of tests.

## What

* add a new derive macro that generates code from the 'schema' in `storage_schema.rs`
* `storage_schema.rs` defines the layout of our new struct, for each field we specify
   * type and name
   * category ('data' 'meta' or 'transient')
   * location (inline or lazy or flags for bitfields)
   * whether we should `filter_transient` when encoding (not everything can contain transient data)
   * Logically this is a replacement for `CachedDataItem`
* From that we generate a few things
   * `TaskFlags` a bitfield to hold some data items as well as 'meta' information like the 'snapshot' and 'restore' bits replacing `InnerStorageState`
   * `LazyField`  enum, defines tall the fields that we dynamically allocate
   * `TaskStorage` the main storage struct, along with basic accessors for all the datatypes (which helps encapsualte storage strategy)
   * `TaskStorageAccessors` a trait with default implementations of every accessor which also handle
      * access checking, via `check_access`
      * mutation tracking via `track_mutation`
       * putting these behaviors in a trait allows some of the business logic around snapshotting to remain in normal rust code that implements the trait, while all the field updating logic can be autogenerated

  * Serialization and encoding infrastructure
      * routines to encode and decode the 'meta' or 'data' subsets
      * routines to create partial 'snapshots' of `meta` or `data` subsets
      * routines to restore data from deserialized snapshots

See https://gist.github.com/lukesandberg/ea35f0e92c1cdeddf80d0498f2978144 for a fully expanded version of the macro

### Why

There are a few goals from this redesign of `InnerStorage`

* (speculative) a single large `derive` macro will be easier to maintain than the current mix of `macro_rules` and derive macros we use for `CacheDataItem`
    * There is one macro to expand when inspecting and the macro generation code is pretty straightforward if voluminous
* We eliminate many `match` operations on `CachedDataItem`
    * Today to lookup a piece of data we need to execute several matches to get the the data category and find the storage location.  With this new approach those are handled by just calling the correct accessor which statically addresses the storage location
* serialization performance
    * we don't need to 'explode' the storage into `Vec<CachedDataItem>` anymore and instead can either directly snapshot the storage or encode directly to a buffer.  This should eliminate some temporary allocations and should enable a smaller serialization format
* more flexibility
    * mostly this is the same approach as before with a few small improvements, notably `invalidator` and `immutable` are now stored as single inline bits reducing lookupcosts and memory for tasks that have these.

### How

Most of the code was written by Claude with obviously a ton of feedback, so please review carefully.  For now all this code is unused but future PRs will integrate it into the backend
parent b186a869
mod counter_map;
mod dynamic_storage;
mod operation;
mod storage;
mod storage_schema;
use std::{
borrow::Cow,
......
......@@ -38,18 +38,39 @@ pub struct CellRef {
pub cell: CellId,
}
impl CellRef {
/// Returns true if this cell reference points to a transient task.
pub fn is_transient(&self) -> bool {
self.task.is_transient()
}
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub struct CollectibleRef {
pub collectible_type: TraitTypeId,
pub cell: CellRef,
}
impl CollectibleRef {
/// Returns true if this collectible reference points to a transient task.
pub fn is_transient(&self) -> bool {
self.cell.is_transient()
}
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub struct CollectiblesRef {
pub task: TaskId,
pub collectible_type: TraitTypeId,
}
impl CollectiblesRef {
/// Returns true if this collectibles reference points to a transient task.
pub fn is_transient(&self) -> bool {
self.task.is_transient()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub enum OutputValue {
Cell(CellRef),
......@@ -57,7 +78,11 @@ pub enum OutputValue {
Error(TurboTasksExecutionError),
}
impl OutputValue {
fn is_transient(&self) -> bool {
/// Returns true if this output value references a transient task.
///
/// Transient values should not be persisted to disk since they reference
/// tasks that will not exist after restart.
pub fn is_transient(&self) -> bool {
match self {
OutputValue::Cell(cell) => cell.task.is_transient(),
OutputValue::Output(task) => task.is_transient(),
......
......@@ -3,6 +3,7 @@ mod key_value_pair_macro;
mod non_local_value_macro;
mod operation_value_macro;
mod task_input_macro;
mod task_storage_macro;
mod trace_raw_vcs_macro;
mod value_debug_format_macro;
mod value_debug_macro;
......@@ -13,6 +14,7 @@ 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};
pub use task_input_macro::derive_task_input;
pub use task_storage_macro::task_storage;
pub use trace_raw_vcs_macro::derive_trace_raw_vcs;
pub use value_debug_format_macro::derive_value_debug_format;
pub use value_debug_macro::derive_value_debug;
......
......@@ -66,6 +66,15 @@ 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>
/// -->
#[proc_macro_attribute]
pub fn task_storage(_args: TokenStream, input: TokenStream) -> TokenStream {
derive::task_storage(input)
}
/// <!--
/// Documentation for this macro is available on the re-export:
/// <https://turbopack-rust-docs.vercel.sh/rustdoc/turbo_tasks/attr.value.html>
......
......@@ -282,6 +282,45 @@ pub use turbo_tasks_macros::value;
#[rustfmt::skip]
pub use turbo_tasks_macros::value_trait;
/// Derives the TaskStorage struct and generates optimized storage structures.
///
/// This macro analyzes `field` annotations and generates:
/// 1. A unified TaskStorage struct
/// 2. LazyField enum for lazy_vec fields
/// 3. Typed accessor methods on TaskStorage
/// 4. TaskStorageAccessors trait with accessor methods
/// 5. TaskFlags bitfield for boolean flags
///
/// # Field Attributes
///
/// All fields require two attributes:
///
/// ## `storage = "..."` (required)
///
/// Specifies how the field is stored:
/// - `direct` - Direct field access (e.g., `Option<OutputValue>`)
/// - `auto_set` - Uses AutoSet for small collections
/// - `auto_map` - Uses AutoMap for key-value pairs
/// - `counter_map` - Uses CounterMap for reference counting
/// - `flag` - Boolean flag stored in a compact TaskFlags bitfield (field type must be `bool`)
///
/// ## `category = "..."` (required)
///
/// Specifies the data category for persistence and access:
/// - `data` - Frequently changed, bulk I/O
/// - `meta` - Rarely changed, small I/O
/// - `transient` - Field is not serialized (in-memory only)
///
/// ## Optional Modifiers
///
/// - `inline` - Field is stored inline on TaskStorage (default is lazy). Only use for hot-path
/// fields that are frequently accessed.
/// - `default` - Use `Default::default()` semantics instead of `Option` for inline direct fields.
/// - `filter_transient` - Filter out transient values during serialization.
/// - Serialization methods
#[rustfmt::skip]
pub use turbo_tasks_macros::task_storage;
pub type TaskIdSet = AutoSet<TaskId, BuildHasherDefault<FxHasher>, 2>;
pub mod test_helpers {
......
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