Expand description
§Turbo Tasks
An incremental computation system that uses macros and types to automate the caching process.
For a high-level overview, start by reading Inside Turbopack: Building Faster by Building Less.
Turbo Tasks defines 4 primitives:
- Functions: Units of execution, invalidation, and reexecution.
- Values: Data created, stored, and returned by functions.
- Traits: Traits that define a set of functions on values.
- Collectibles: Values emitted in functions that bubble up the call graph and can be collected in parent functions. Collectibles are deduplicated by cell id equality.
It defines some derived elements from that:
- Tasks: An instance of a function together with its arguments.
Vcs (“Value Cells”): References to locations associated with tasks where values are stored. The contents of a cell can change after the reexecution of a function due to invalidation. AVccan be read to get a read-only reference to the stored data, representing a snapshot of that cell at that point in time.
There are a few design patterns that are commonly used with Turbo Tasks:
- Singleton Pattern: Use a private constructor function to ensure a 1:1 mapping between values and value cells.
§Functions and Tasks
#[turbo_tasks::function]s are memoized functions within the build process. An instance of a function with arguments is called a task. They are responsible for:
- Tracking Dependencies: Each task keeps track of its dependencies to determine when a recomputation is necessary. Dependencies are tracked when a
Vc<T>(Value Cell) is awaited. - Recomputing Changes: When a dependency changes, the affected tasks are automatically recomputed.
- Parallel Execution: Every task is spawned as a Tokio task, which uses Tokio’s multithreaded work-stealing executor.
§Task Graph
All tasks and their dependencies form a task graph.
This graph is crucial for invalidation propagation. When a task is invalidated, the changes propagate through the graph, triggering rebuilds where necessary.
§Incremental Builds
Upon execution of functions, turbo-tasks will track which Vcs are read. Once any of these change, turbo-tasks will invalidate the task created from the function’s execution and it will eventually be scheduled and reexecuted.
After initial execution, turbo-tasks employs a bottom-up approach for incremental rebuilds.
By rebuilding invalidated tasks, only the parts of the graph affected by changes are rebuilt, leaving untouched parts intact. No work is done for unchanged parts.
Re-exports§
pub use crate::display::ValueToString;pub use crate::display::ValueToStringRef;pub use crate::mapped_read_ref::MappedReadRef;
Modules§
- _singleton_
pattern - Singleton Pattern
- backend
- debug
- display
- Async Formatting
- duration_
span - event
- graph
- keyed
- mapped_
read_ ref - message_
queue - panic_
hooks - Provides a central registry for safe runtime registration and de-registration of panic hooks.
- parallel
- Parallel for each and map using tokio tasks.
- primitives
- registry
- scope
- A scoped tokio spawn implementation that allow a non-’static lifetime for tasks.
- small_
duration - task
- task_
statistics - test_
helpers - trace
- util
Macros§
- duration_
span - Creates a event-based span that traces a certain duration (lifetime of the guard). It’s not a real span, which means it can be used across threads.
- fxindexmap
- fxindexset
- stringify_
path - turbobail
- Async bail macro. Resolves arguments then calls
anyhow::bail!(). - turbofmt
- Async format macro. Returns
impl Future<Output = Result<RcStr>>. - vdbg
- This macro supports the same syntax as
dbg!, but also supports pretty-printingVctypes.
Structs§
- Apply
Effects Context - CellId
- Completion
- Just an empty type, but it’s never equal to itself.
- Completions
- This is a transparent value type wrapping
Vec<ResolvedVc<Completion>>. - Current
Cell Ref - A reference to a task’s cell with methods that allow updating the contents of the cell.
- Effects
- Captured effects from an operation. This struct can be used to return Effects from a turbo-tasks function and apply them later.
- Either
Task Input - A thin wrapper around
Eitherthat implements the traits required byTaskInput, notablyEncodeandDecode. - Error
- The
Errortype, a wrapper around a dynamic error type. - Execution
Id - An identifier for a specific task execution. Used to assert that local
Vcs don’t leak. This value may overflow and re-use old values. - Invalidation
Reason Set - A set of InvalidationReasons. They are automatically deduplicated and merged by kind during insertion. It implements Display to get a readable representation.
- Invalidator
- A lightweight handle to invalidate a task. Only stores the task ID.
The caller must provide the
TurboTasksApiwhen calling invalidation methods. - Join
Handle - Local
Task Id - Represents the nth
localfunction call inside a task. - Operation
Vc - A “subtype” (can be converted via
.connect()) ofVcthat represents a specific call (with arguments) to a task. - Pretty
Print Error - Implements Display to print the error message in a friendly way. Puts a summary first and details after that.
- Read
Cell Options - Read
Output Options - Read
RawVc Future - ReadRef
- The read value of a value cell. The read value is immutable, while the cell itself might change over time. It’s basically a snapshot of a value at a certain point in time.
- Read
VcFuture - Resolved
Vc - A “subtype” (via
Deref) ofVcthat represents a specificVc::cell/.cell()orResolvedVc::cell/.resolved_cell()constructor call within a task. - Serialization
Invalidator - State
- This API violates core assumption of turbo-tasks, is believed to be unsound, and there’s no plan fix it. You should prefer to use collectibles instead of state where at all possible. This API may be removed in the future.
- TaskId
- Trait
Method - Trait
Ref - Similar to a
ReadRef<T>, but contains a value trait object instead. - Trait
Type - Trait
Type Id - Transient
Instance - Pass a reference to an instance to a turbo-tasks function.
- Transient
State - Transient
Value - Pass a value by value (
Value<Xxx>) instead of by reference (Vc<Xxx>). - Turbo
Tasks - Turbo
Tasks Panic - Unused
- A wrapper around a value that is unused.
- Update
Info - Value
Type - A definition of a type of data.
- Value
Type Id - Vc
- Value cells represent the pending result of a computation, similar to a cell in a spreadsheet. When a
Vc’s contents change, the change is propagated by invalidating dependent tasks. - VcCell
Compare Mode - Mode that compares the cell’s content with the new value and only updates if the new value is different.
- VcCell
Keyed Compare Mode - Mode that compares the cell’s content with the new value key by key and only updates individual keys if the new value is different.
- VcCell
NewMode - Mode that always updates the cell’s content.
- VcDefault
Read - Representation for standard
#[turbo_tasks::value], where a read return a reference to the value type[] - VcTransparent
Read - Representation for
#[turbo_tasks::value(transparent)]types, where reads return a reference to the target type. - VcValue
Trait Cast - Casts an arbitrary cell content into a
TraitRef<T>. - VcValue
Type Cast - Casts an arbitrary cell content into a
ReadRef<T>.
Enums§
- Output
Content - A helper type representing the output of a resolved task.
- RawVc
- A type-erased representation of
Vc. - Read
Cell Tracking - Read
Consistency - Read
Tracking - Task
Execution Reason - Task
Persistence - Task
Priority
Constants§
Traits§
- Collectibles
Source - Implemented on
OperationVcandRawVc. - Dynamic
- Marker trait that indicates that a
Vc<Self>can accept all methods declared on aVc<T>. - Invalidation
Reason - A user-facing reason why a task was invalidated. This should only be used for invalidation that were triggered by the user.
- Invalidation
Reason Kind - Invalidation reason kind. This is used to merge multiple reasons of the same kind into a combined description.
- Join
Iter Ext - Magic
Any - NonLocal
Value - Marker trait indicating that a type does not contain any instances of
Vcor references toVc. It may containResolvedVcorOperationVc. - Operation
Value - Indicates that a type does not contain any instances of
VcorResolvedVc. It may containOperationVc. - Option
VcExt - Shrink
ToFit - Recursively calls
shrink_to_fiton all elements of the container. - Task
Input - Trait to implement in order for a type to be accepted as a
#[turbo_tasks::function]argument. - TryFlat
Join Iter Ext - TryJoin
Iter Ext - Turbo
Tasks Api - A type-erased subset of
TurboTasksstored inside a thread local when we’re in a turbo task context. Returned by theturbo_taskshelper function. - Turbo
Tasks Backend Api - A subset of the
TurboTasksAPI that’s exposed toBackendimplementations. - Turbo
Tasks Call Api - Common base trait for
TurboTasksApiandTurboTasksBackendApi. Provides APIs for creating tasks from function calls. - Upcast
- Marker trait that indicates that a
Vc<Self>can be upcasted to aVc<T>. - Upcast
Strict - A specialization of
Upcastthat ensures that the upcast is strict meaning thatT !== Self. - Value
Default Vc<T>analog to theDefaulttrait.- VcCast
- Trait defined to share behavior between values and traits within
ReadRawVcFuture. SeeVcValueTypeCastandVcValueTraitCast. - VcRead
- Trait that controls
crate::Vc’s read representation. - VcValue
Trait - A trait implemented on all values trait object references that can be used with a value cell
(
Vc<Box<dyn Trait>>). - VcValue
Type - A trait implemented on all values types that can be put into a value cell (
Vc).
Functions§
- apply_
effects - Applies all effects that have been emitted by an operations.
- block_
for_ future - Blocking waits for a future to complete. This blocks the current thread potentially staling other concurrent futures (but not other concurrent tasks). Try to avoid this method infavor of awaiting the future instead.
- block_
in_ place - Tells the scheduler about blocking work happening in the current thread. It will make sure to allocate extra threads for the pool.
- dynamic_
call - Calls
TurboTasks::dynamic_callfor the current turbo tasks instance. - effect
- Schedules an effect to be applied. The passed future is executed once
apply_effectsis called. - emit
- get_
effects - Capture effects from an turbo-tasks operation. Since this captures collectibles it might invalidate the current task when effects are changing or even temporary change.
- get_
invalidator - Get an
Invalidatorthat can be used to invalidate the current task based on external events. ReturnsNoneif called outside of a task context. - get_
serialization_ invalidator - Returns a
SerializationInvalidatorthat can be used to invalidate the serialization of the current task cells. - mark_
finished - Marks the current task as finished. This excludes it from waiting for strongly consistency.
- mark_
session_ dependent - Marks the current task as dirty when restored from filesystem cache.
- mark_
stateful - Marks the current task as stateful. This is used to indicate that the task has interior mutability (e.g., via State or TransientState), which means the task may produce different outputs even with the same inputs.
- mark_
top_ level_ task - Marks the current task context as being in a top-level task. When in a top-level task, eventually consistent reads will panic. It is almost always a mistake to perform an eventually consistent read at the top-level of the application.
- prevent_
gc - run
- run_
once - run_
once_ with_ reason - spawn
- Spawns a future as separate task and returns a JoinHandle which can be used to await the result. The future has access to the current TurboTasks context and runs in the same tracing span. Allocations and cpu time is accounted to the current turbo-tasks function.
- spawn_
blocking - Spawns a blocking function in a separate task using the blocking pool and returns a JoinHandle which can be used to await the result. The function has access to the current TurboTasks context and runs in the same tracing span. Allocations and cpu time is accounted to the current turbo-tasks function.
- spawn_
thread - Spawns a thread which runs in background. It has access to the current TurboTasks context, but is not accounted towards the current turbo-tasks function.
- trait_
call - Calls
TurboTasks::trait_callfor the current turbo tasks instance. - turbo_
tasks - turbo_
tasks_ scope - turbo_
tasks_ weak - unmark_
top_ level_ task_ may_ leak_ eventually_ consistent_ state - Unmarks the current task context as being in a top-level task. The opposite of
mark_top_level_task. - with_
turbo_ tasks
Type Aliases§
- FxDash
Map - FxIndex
Map - FxIndex
Set - Result
Result<T, Error>- Task
IdSet
Attribute Macros§
- function
- Tasks are created by defining a Rust function annotated with the
#[turbo_tasks::function]macro and calling it with arguments. Each unique combination of function and arguments create a new task at runtime. Tasks are the fundamental units of work within the build system. - task_
storage - Derives the TaskStorage struct and generates optimized storage structures.
- value
- Implements
VcValueTypefor the givenstructorenum. These value types can be used inside of a “value cell” asVc<...>. - value_
impl - A macro used on any
implblock for aVcValueType. This can either be an inherent implementation or a trait implementation (seeturbo_tasks::value_traitandVcValueTrait). - value_
trait - Allows this trait to be used as part of a trait object inside of a value cell, in the form of
Vc<Box<dyn MyTrait>>. The annotated trait is made into a subtrait ofVcValueTrait.
Derive Macros§
- NonLocal
Value - Implements
NonLocalValuefor a struct or enum by adding static (compile-time) assertions that every field implementsNonLocalValue. - Operation
Value - Shrink
ToFit - Task
Input - Refer to the trait documentation for usage.
- Value
ToString - Derive macro for
ValueToString. Also generatesValueToStringify for &T.