Unverified Commit b35713c4 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: bincode: Use bincode to store the contents of value cells (#86338)

This PR switches `turbo-tasks-backend` to use `bincode` instead of serde for cell contents. To keep PR size manageable, `TaskInput`s are still serialized using serde and a compatibility shim. The next PR fixes that.

See https://github.com/vercel/next.js/pull/86631 for benchmark numbers.
parent dfb0b8e6
......@@ -73,4 +73,4 @@ rustflags = [
linker = "arm-linux-gnueabihf-gcc"
[target.wasm32-unknown-unknown]
rustflags = ["-Zshare-generics=y", "--cfg", 'getrandom_backend="wasm_js"']
\ No newline at end of file
rustflags = ["-Zshare-generics=y", "--cfg", 'getrandom_backend="wasm_js"']
......@@ -9218,6 +9218,7 @@ dependencies = [
"once_cell",
"parking_lot",
"pin-project-lite",
"pot",
"rayon",
"regex",
"rustc-hash 2.1.1",
......@@ -9237,6 +9238,7 @@ dependencies = [
"turbo-tasks-macros",
"turbo-tasks-malloc",
"unsize",
"unty",
]
[[package]]
......@@ -9258,7 +9260,6 @@ dependencies = [
"lmdb-rkv",
"once_cell",
"parking_lot",
"pot",
"rand 0.9.0",
"regex",
"ringmap",
......@@ -9272,6 +9273,7 @@ dependencies = [
"thread_local",
"tokio",
"tracing",
"turbo-bincode",
"turbo-persistence",
"turbo-rcstr",
"turbo-tasks",
......
......@@ -360,7 +360,6 @@ preset_env_base = "6.0.0"
# General Deps
bincode = { version = "2.0.1", features = ["serde"] }
chromiumoxide = { version = "0.5.4", features = [
"tokio-runtime",
], default-features = false }
......@@ -374,6 +373,7 @@ allsorts = { version = "0.14.0", default-features = false, features = [
] }
anyhow = "1.0.100"
async-trait = "0.1.64"
bincode = { version = "2.0.1", features = ["serde"] }
bitfield = "0.18.0"
byteorder = "1.5.0"
bytes = "1.1.0"
......@@ -393,12 +393,13 @@ either = "1.9.0"
erased-serde = "0.4.5"
flate2 = "1.0.28"
futures = "0.3.31"
futures-util = "0.3.31"
futures-retry = "0.6.0"
futures-util = "0.3.31"
hashbrown = "0.14.5"
image = { version = "0.25.8", default-features = false }
indexmap = "2.7.1"
indoc = "2.0.0"
inventory = "0.3.21"
itertools = "0.10.5"
lightningcss = { version = "1.0.0-alpha.68", features = [
"serde",
......@@ -443,11 +444,10 @@ ringmap = "0.1.3"
roaring = "0.10.10"
rstest = "0.16.0"
rustc-hash = "2.1.1"
twox-hash = { version = "2.1.0", features = ["xxhash64", "xxhash3_128"] }
semver = "1.0.16"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
serde_bytes = "0.11.15"
serde_json = "1.0.138"
serde_path_to_error = "0.1.16"
serde_qs = "0.13.0"
serde_with = "3.12.0"
......@@ -458,18 +458,19 @@ smallvec = { version = "1.15.1", features = [
"const_new",
"impl_bincode",
] }
swc_sourcemap = "9.3.4"
strsim = "0.11.1"
shrink-to-fit = "0.2.10"
strsim = "0.11.1"
swc_sourcemap = "9.3.4"
syn = "2.0.100"
tempfile = "3.20.0"
thread_local = "1.1.8"
thiserror = "1.0.48"
thread_local = "1.1.8"
tokio = "1.43.0"
tokio-util = { version = "0.7.13", features = ["io", "rt"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
triomphe = { git = "https://github.com/sokra/triomphe", branch = "sokra/unstable" }
twox-hash = { version = "2.1.0", features = ["xxhash64", "xxhash3_128"] }
unsize = "1.1.0"
unty = "0.0.4"
url = "2.2.2"
......@@ -478,7 +479,6 @@ uuid = "1.18.1"
vergen = { version = "9.0.6", features = ["cargo"] }
vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
webbrowser = "1.0.6"
inventory = "0.3.21"
[patch.crates-io]
bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" }
......
......@@ -2204,9 +2204,10 @@ impl NextConfig {
/// A subset of ts/jsconfig that next.js implicitly
/// interops with.
#[turbo_tasks::value(serialization = "custom", eq = "manual")]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Encode, Decode)]
#[serde(rename_all = "camelCase")]
pub struct JsConfig {
#[bincode(with = "turbo_bincode::serde_json")]
compiler_options: Option<serde_json::Value>,
}
......
......@@ -42,7 +42,6 @@ indexmap = { workspace = true }
lmdb-rkv = { version = "0.14.0", optional = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
pot = "3.0.0"
rand = { workspace = true }
ringmap = { workspace = true, features = ["serde"] }
rustc-hash = { workspace = true }
......@@ -53,6 +52,7 @@ smallvec = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
thread_local = { workspace = true }
turbo-bincode = { workspace = true }
turbo-persistence = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
......
......@@ -1057,6 +1057,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
(meta, data)
};
let process = |task_id: TaskId, (meta, data): (Option<Vec<_>>, Option<Vec<_>>)| {
// TODO: perf: Instead of returning a `Vec` of individually allocated `SmallVec`s, it'd
// be better to append everything to a flat per-task or per-shard `Vec<u8>`, and have
// each `serialize` call return `(start_idx, end_idx)`.
(
task_id,
meta.map(|d| self.backing_storage.serialize(task_id, &d)),
......
......@@ -10,10 +10,10 @@ use std::{
};
use anyhow::Result;
use bincode::{Decode, Encode};
use indexmap::map::Entry;
use ringmap::RingSet;
use rustc_hash::{FxBuildHasher, FxHashMap};
use serde::{Deserialize, Serialize, Serializer, ser::SerializeSeq};
use smallvec::{SmallVec, smallvec};
#[cfg(any(
feature = "trace_aggregation_update",
......@@ -166,9 +166,11 @@ impl ComputeDirtyAndCleanUpdateResult {
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Encode, Decode, Clone, Debug)]
pub struct InnerOfUppersHasNewFollowersJob {
#[bincode(with = "turbo_bincode::smallvec")]
pub upper_ids: TaskIdVec,
#[bincode(with = "turbo_bincode::smallvec")]
pub new_follower_ids: TaskIdVec,
}
......@@ -178,9 +180,11 @@ impl From<InnerOfUppersHasNewFollowersJob> for AggregationUpdateJob {
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Encode, Decode, Clone, Debug)]
pub struct InnerOfUppersLostFollowersJob {
#[bincode(with = "turbo_bincode::smallvec")]
pub upper_ids: TaskIdVec,
#[bincode(with = "turbo_bincode::smallvec")]
pub lost_follower_ids: TaskIdVec,
}
......@@ -190,7 +194,7 @@ impl From<InnerOfUppersLostFollowersJob> for AggregationUpdateJob {
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Encode, Decode, Clone, Debug)]
pub struct AggregatedDataUpdateJob {
pub upper_ids: TaskIdVec,
pub update: AggregatedDataUpdate,
......@@ -203,7 +207,7 @@ impl From<AggregatedDataUpdateJob> for AggregationUpdateJob {
}
/// A job in the job queue for updating something in the aggregated graph.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Encode, Decode, Clone, Debug)]
pub enum AggregationUpdateJob {
/// Update the aggregation number of a task. This might result in balancing needed to update
/// "upper" and "follower" edges.
......@@ -252,17 +256,27 @@ pub enum AggregationUpdateJob {
collectible_type: turbo_tasks::TraitTypeId,
},
/// Increases the active counter of the task
#[serde(skip)]
IncreaseActiveCount { task: TaskId },
IncreaseActiveCount {
// TODO: bgw: Add a way to skip the entire enum variant in bincode (generating an error
// upon attempted serialization) similar to #[serde(skip)] on variants
#[bincode(skip, default = "unreachable_decode")]
task: TaskId,
},
/// Increases the active counters of the tasks
#[serde(skip)]
IncreaseActiveCounts { task_ids: TaskIdVec },
IncreaseActiveCounts {
#[bincode(skip, default = "unreachable_decode")]
task_ids: TaskIdVec,
},
/// Decreases the active counter of the task
#[serde(skip)]
DecreaseActiveCount { task: TaskId },
DecreaseActiveCount {
#[bincode(skip, default = "unreachable_decode")]
task: TaskId,
},
/// Decreases the active counters of the tasks
#[serde(skip)]
DecreaseActiveCounts { task_ids: TaskIdVec },
DecreaseActiveCounts {
#[bincode(skip, default = "unreachable_decode")]
task_ids: TaskIdVec,
},
/// Balances the edges of the graph. This checks if the graph invariant is still met for this
/// edge and coverts a upper edge to a follower edge or vice versa. Balancing might triggers
/// more changes to the structure.
......@@ -271,6 +285,10 @@ pub enum AggregationUpdateJob {
Noop,
}
fn unreachable_decode<T>() -> T {
unreachable!("AggregatedDataUpdateJob variant should not have been encoded, cannot decode")
}
impl AggregationUpdateJob {
pub fn data_update(
task: &mut impl TaskGuard,
......@@ -291,9 +309,10 @@ impl AggregationUpdateJob {
}
}
#[derive(Default, Serialize, Deserialize, Clone, Copy, Debug)]
#[derive(Default, Encode, Decode, Clone, Copy, Debug)]
#[bincode(decode_bounds = "T: Default", borrow_decode_bounds = "T: Default")]
pub struct SessionDependent<T> {
#[serde(skip, default)]
#[bincode(skip)]
pub value: T,
}
......@@ -312,7 +331,7 @@ impl<T> Deref for SessionDependent<T> {
}
/// Aggregated data update.
#[derive(Default, Serialize, Deserialize, Clone, Debug)]
#[derive(Default, Encode, Decode, Clone, Debug)]
pub struct AggregatedDataUpdate {
/// One of the inner tasks has changed its dirty state or aggregated dirty state.
/// (task id, dirty update, current session clean update)
......@@ -649,21 +668,21 @@ impl AggregatedDataUpdate {
}
/// An aggregation number update job that is enqueued.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
struct AggregationNumberUpdate {
base_aggregation_number: u32,
distance: Option<NonZeroU32>,
#[cfg(feature = "trace_aggregation_update")]
#[serde(skip, default)]
#[bincode(skip, default)]
span: Option<Span>,
}
/// An aggregated data update job that is enqueued. See `AggregatedDataUpdate`.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
struct AggregationUpdateJobItem {
job: AggregationUpdateJob,
#[cfg(feature = "trace_aggregation_update")]
#[serde(skip, default)]
#[bincode(skip, default)]
span: Option<Span>,
}
......@@ -692,12 +711,12 @@ struct AggregationUpdateJobGuard {
}
/// A balancing job that is enqueued. See `balance_edge`.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
struct BalanceJob {
upper_id: TaskId,
task_id: TaskId,
#[cfg(feature = "trace_aggregation_update")]
#[serde(skip, default)]
#[bincode(skip, default)]
span: Option<Span>,
}
......@@ -728,11 +747,11 @@ impl PartialEq for BalanceJob {
impl Eq for BalanceJob {}
/// An optimization job that is enqueued. See `optimize_task`.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
struct OptimizeJob {
task_id: TaskId,
#[cfg(feature = "trace_aggregation_update")]
#[serde(skip, default)]
#[bincode(skip, default)]
span: Option<Span>,
}
......@@ -761,11 +780,11 @@ impl PartialEq for OptimizeJob {
impl Eq for OptimizeJob {}
/// A job to find and schedule dirty tasks that is enqueued. See `find_and_schedule_dirty`.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
struct FindAndScheduleJob {
task_id: TaskId,
#[cfg(feature = "trace_find_and_schedule")]
#[serde(skip, default)]
#[bincode(skip, default)]
span: Option<Span>,
}
......@@ -793,42 +812,73 @@ impl PartialEq for FindAndScheduleJob {
impl Eq for FindAndScheduleJob {}
/// Serializes the jobs in the queue. This is used to filter out transient jobs during
/// serialization.
fn serialize_jobs<S: Serializer>(
jobs: &VecDeque<AggregationUpdateJobItem>,
serializer: S,
) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(jobs.len()))?;
for job in jobs {
match job.job {
AggregationUpdateJob::IncreaseActiveCount { .. }
| AggregationUpdateJob::IncreaseActiveCounts { .. }
| AggregationUpdateJob::DecreaseActiveCount { .. }
| AggregationUpdateJob::DecreaseActiveCounts { .. } => {
seq.serialize_element(&AggregationUpdateJobItem {
job: AggregationUpdateJob::Noop,
#[cfg(feature = "trace_aggregation_update")]
span: None,
})?;
}
_ => {
seq.serialize_element(job)?;
/// Encodes the jobs in the queue. This is used to filter out transient jobs during encoding.
mod encode_jobs {
use bincode::{
de::{BorrowDecoder, Decoder},
enc::Encoder,
error::{DecodeError, EncodeError},
};
use super::*;
pub fn encode<E: Encoder>(
jobs: &VecDeque<AggregationUpdateJobItem>,
encoder: &mut E,
) -> Result<(), EncodeError> {
usize::encode(&jobs.len(), encoder)?;
for job in jobs {
match job.job {
AggregationUpdateJob::IncreaseActiveCount { .. }
| AggregationUpdateJob::IncreaseActiveCounts { .. }
| AggregationUpdateJob::DecreaseActiveCount { .. }
| AggregationUpdateJob::DecreaseActiveCounts { .. } => {
AggregationUpdateJobItem {
job: AggregationUpdateJob::Noop,
#[cfg(feature = "trace_aggregation_update")]
span: None,
}
.encode(encoder)?;
}
_ => {
job.encode(encoder)?;
}
}
}
Ok(())
}
pub fn decode<Context, D: Decoder<Context = Context>>(
decoder: &mut D,
) -> Result<VecDeque<AggregationUpdateJobItem>, DecodeError> {
let len = usize::decode(decoder)?;
let mut jobs = VecDeque::with_capacity(len);
for _ in 0..len {
jobs.push_back(Decode::decode(decoder)?);
}
Ok(jobs)
}
pub fn borrow_decode<'de, Context, D: BorrowDecoder<'de, Context = Context>>(
decoder: &mut D,
) -> Result<VecDeque<AggregationUpdateJobItem>, DecodeError> {
decode(decoder)
}
seq.end()
}
/// A queue for aggregation update jobs.
#[derive(Default, Serialize, Deserialize, Clone)]
#[derive(Default, Encode, Decode, Clone)]
pub struct AggregationUpdateQueue {
#[serde(serialize_with = "serialize_jobs")]
#[bincode(with = "encode_jobs")]
jobs: VecDeque<AggregationUpdateJobItem>,
#[bincode(with = "turbo_bincode::indexmap")]
number_updates: FxIndexMap<TaskId, AggregationNumberUpdate>,
done_number_updates: FxHashMap<TaskId, AggregationNumberUpdate>,
#[bincode(with = "turbo_bincode::ringset")]
find_and_schedule: FxRingSet<FindAndScheduleJob>,
#[bincode(with = "turbo_bincode::ringset")]
balance_queue: FxRingSet<BalanceJob>,
#[bincode(with = "turbo_bincode::ringset")]
optimize_queue: FxRingSet<OptimizeJob>,
}
......
use std::mem::take;
use bincode::{Decode, Encode};
use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use turbo_tasks::TaskId;
......@@ -23,7 +23,7 @@ use crate::{
data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef},
};
#[derive(Serialize, Deserialize, Clone, Default)]
#[derive(Encode, Decode, Clone, Default)]
pub enum CleanupOldEdgesOperation {
RemoveEdges {
task_id: TaskId,
......@@ -38,7 +38,7 @@ pub enum CleanupOldEdgesOperation {
// TODO Add aggregated edge
}
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
pub enum OutdatedEdge {
Child(TaskId),
Collectible(CollectibleRef, i32),
......
use serde::{Deserialize, Serialize};
use bincode::{Decode, Encode};
use turbo_tasks::{TaskExecutionReason, TaskId};
use crate::{
......@@ -12,7 +12,7 @@ use crate::{
data::{CachedDataItem, CachedDataItemKey, InProgressState, InProgressStateInner},
};
#[derive(Serialize, Deserialize, Clone, Default)]
#[derive(Encode, Decode, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum ConnectChildOperation {
UpdateAggregation {
......
use serde::{Deserialize, Serialize};
use bincode::{Decode, Encode};
use smallvec::SmallVec;
use turbo_tasks::{TaskExecutionReason, TaskId};
......@@ -19,7 +19,7 @@ use crate::{
},
};
#[derive(Serialize, Deserialize, Clone, Default)]
#[derive(Encode, Decode, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum InvalidateOperation {
MakeDirty {
......
......@@ -13,7 +13,7 @@ use std::{
sync::{Arc, atomic::Ordering},
};
use serde::{Deserialize, Serialize};
use bincode::{Decode, Encode};
use turbo_tasks::{
CellId, FxIndexMap, KeyValuePair, TaskId, TurboTasksBackendApi, TypedSharedReference,
};
......@@ -32,11 +32,7 @@ use crate::{
};
pub trait Operation:
Serialize
+ for<'de> Deserialize<'de>
+ Default
+ TryFrom<AnyOperation, Error = ()>
+ Into<AnyOperation>
Encode + Decode<()> + Default + TryFrom<AnyOperation, Error = ()> + Into<AnyOperation>
{
fn execute(self, ctx: &mut impl ExecuteContext);
}
......@@ -786,7 +782,7 @@ macro_rules! impl_operation {
};
}
#[derive(Serialize, Deserialize, Clone)]
#[derive(Encode, Decode, Clone)]
pub enum AnyOperation {
ConnectChild(connect_child::ConnectChildOperation),
Invalidate(invalidate::InvalidateOperation),
......
use std::mem::take;
use serde::{Deserialize, Serialize};
use bincode::{Decode, Encode};
use smallvec::SmallVec;
#[cfg(not(feature = "verify_determinism"))]
use turbo_tasks::backend::VerificationMode;
......@@ -20,7 +20,7 @@ use crate::{
data::{CachedDataItem, CachedDataItemKey, CellRef},
};
#[derive(Serialize, Deserialize, Clone, Default)]
#[derive(Encode, Decode, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum UpdateCellOperation {
InvalidateWhenCellDependency {
......
use bincode::{Decode, Encode};
use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
......@@ -32,25 +33,25 @@ macro_rules! transient_traits {
};
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub struct CellRef {
pub task: TaskId,
pub cell: CellId,
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub struct CollectibleRef {
pub collectible_type: TraitTypeId,
pub cell: CellRef,
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encode, Decode)]
pub struct CollectiblesRef {
pub task: TaskId,
pub collectible_type: TraitTypeId,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub enum OutputValue {
Cell(CellRef),
Output(TaskId),
......@@ -140,7 +141,7 @@ transient_traits!(ActivenessState);
impl Eq for ActivenessState {}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)]
pub enum Dirtyness {
Dirty,
SessionDependent,
......@@ -205,14 +206,14 @@ impl InProgressCellState {
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct AggregationNumber {
pub base: u32,
pub distance: u32,
pub effective: u32,
}
#[derive(Debug, Clone, KeyValuePair, Serialize, Deserialize)]
#[derive(Debug, Clone, KeyValuePair, Encode, Decode)]
pub enum CachedDataItem {
// Output
Output {
......@@ -227,8 +228,10 @@ pub enum CachedDataItem {
Dirty {
value: Dirtyness,
},
#[serde(skip)]
CurrentSessionClean {
// TODO: bgw: Add a way to skip the entire enum variant in bincode (generating an error
// upon attempted serialization) similar to #[serde(skip)] on variants
#[bincode(skip, default = "unreachable_decode")]
value: (),
},
......@@ -243,9 +246,10 @@ pub enum CachedDataItem {
cell: CellId,
value: TypedSharedReference,
},
#[serde(skip)]
TransientCellData {
#[bincode(skip, default = "unreachable_decode")]
cell: CellId,
#[bincode(skip, default = "unreachable_decode")]
value: SharedReference,
},
CellTypeMaxIndex {
......@@ -301,9 +305,10 @@ pub enum CachedDataItem {
task: TaskId,
value: i32,
},
#[serde(skip)]
AggregatedCurrentSessionCleanContainer {
#[bincode(skip, default = "unreachable_decode")]
task: TaskId,
#[bincode(skip, default = "unreachable_decode")]
value: i32,
},
AggregatedCollectible {
......@@ -313,8 +318,8 @@ pub enum CachedDataItem {
AggregatedDirtyContainerCount {
value: i32,
},
#[serde(skip)]
AggregatedCurrentSessionCleanContainerCount {
#[bincode(skip, default = "unreachable_decode")]
value: i32,
},
......@@ -330,43 +335,52 @@ pub enum CachedDataItem {
},
// Transient Root Type
#[serde(skip)]
Activeness {
#[bincode(skip, default = "unreachable_decode")]
value: ActivenessState,
},
// Transient In Progress state
#[serde(skip)]
InProgress {
#[bincode(skip, default = "unreachable_decode")]
value: InProgressState,
},
#[serde(skip)]
InProgressCell {
#[bincode(skip, default = "unreachable_decode")]
cell: CellId,
#[bincode(skip, default = "unreachable_decode")]
value: InProgressCellState,
},
#[serde(skip)]
OutdatedCollectible {
#[bincode(skip, default = "unreachable_decode")]
collectible: CollectibleRef,
#[bincode(skip, default = "unreachable_decode")]
value: i32,
},
#[serde(skip)]
OutdatedOutputDependency {
#[bincode(skip, default = "unreachable_decode")]
target: TaskId,
#[bincode(skip, default = "unreachable_decode")]
value: (),
},
#[serde(skip)]
OutdatedCellDependency {
#[bincode(skip, default = "unreachable_decode")]
target: CellRef,
#[bincode(skip, default = "unreachable_decode")]
value: (),
},
#[serde(skip)]
OutdatedCollectiblesDependency {
#[bincode(skip, default = "unreachable_decode")]
target: CollectiblesRef,
#[bincode(skip, default = "unreachable_decode")]
value: (),
},
}
fn unreachable_decode<T>() -> T {
unreachable!("CachedDataItem variant should not have been encoded, cannot decode")
}
impl CachedDataItem {
pub fn cell_data(
is_serializable_cell_content: bool,
......
......@@ -591,9 +591,9 @@ fn field_declarations(fields: &[Vec<&syn::Field>]) -> Vec<proc_macro2::TokenStre
.map(|field| {
let ty = &field.ty;
let ident = field.ident.as_ref().unwrap();
let attrs = &field.attrs;
// we don't preserve attrs here because we don't copy over the derives, so the
// attributes are likely irrelevant to the generated type
quote! {
#(#attrs)*
#ident: #ty
}
})
......@@ -614,9 +614,8 @@ fn ref_field_declarations(fields: &[Vec<&syn::Field>]) -> Vec<proc_macro2::Token
.map(|field| {
let ty = &field.ty;
let ident = field.ident.as_ref().unwrap();
let attrs = &field.attrs;
// don't preserve attrs because we don't copy over the derives either
quote! {
#(#attrs)*
#ident: &'l #ty
}
})
......@@ -637,9 +636,8 @@ fn mut_ref_field_declarations(fields: &[Vec<&syn::Field>]) -> Vec<proc_macro2::T
.map(|field| {
let ty = &field.ty;
let ident = field.ident.as_ref().unwrap();
let attrs = &field.attrs;
// don't preserve attrs because we don't copy over the derives either
quote! {
#(#attrs)*
#ident: &'l mut #ty
}
})
......
......@@ -53,8 +53,6 @@ impl Parse for PrimitiveInput {
}
}
// TODO: wire this up in https://github.com/vercel/next.js/pull/86338
#[allow(dead_code)]
pub struct BincodeWrappers {
pub encode_ty: Type,
pub decode_ty: Type,
......
......@@ -3,14 +3,16 @@ use quote::quote;
use syn::parse_macro_input;
use crate::{
global_name::global_name, ident::get_type_ident, primitive_input::PrimitiveInput,
global_name::global_name,
ident::get_type_ident,
primitive_input::{BincodeWrappers, PrimitiveInput},
value_macro::value_type_and_register,
};
pub fn primitive(input: TokenStream) -> TokenStream {
let PrimitiveInput {
ty,
bincode_wrappers: _,
bincode_wrappers,
} = parse_macro_input!(input as PrimitiveInput);
let Some(ident) = get_type_ident(&ty) else {
......@@ -38,9 +40,18 @@ pub fn primitive(input: TokenStream) -> TokenStream {
};
let name = global_name(quote!(stringify!(#ty)));
// TODO: https://github.com/vercel/next.js/pull/86338 -- switch to bincode, use bincode wrapper
let new_value_type = quote! {
turbo_tasks::ValueType::new_with_any_serialization::<#ty>(#name);
let new_value_type = if let Some(bincode_wrappers) = bincode_wrappers {
let BincodeWrappers {
encode_ty,
decode_ty,
} = bincode_wrappers;
quote! {
turbo_tasks::ValueType::new_with_bincode_wrappers::<#ty, #encode_ty, #decode_ty>(#name)
}
} else {
quote! {
turbo_tasks::ValueType::new_with_bincode::<#ty>(#name)
}
};
let value_type_and_register = value_type_and_register(
......
......@@ -348,7 +348,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
},
SerializationMode::Auto | SerializationMode::Custom => {
quote! {
turbo_tasks::ValueType::new_with_any_serialization::<#ident>(#name)
turbo_tasks::ValueType::new_with_bincode::<#ident>(#name)
}
}
};
......
......@@ -33,6 +33,7 @@ erased-serde = { workspace = true }
event-listener = "5.4.0"
futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
inventory = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]}
pin-project-lite = { workspace = true }
......@@ -41,7 +42,7 @@ regex = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc", "derive"] }
serde_json = { workspace = true }
shrink-to-fit = { workspace=true,features = ["indexmap", "serde_json", "smallvec", "nightly"] }
shrink-to-fit = { workspace = true, features = ["indexmap", "serde_json", "smallvec", "nightly"] }
smallvec = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
......@@ -55,7 +56,8 @@ turbo-tasks-hash = { workspace = true }
turbo-tasks-macros = { workspace = true }
turbo-tasks-malloc = { workspace = true }
unsize = { workspace = true }
inventory = { workspace = true }
unty = { workspace = true }
pot = "3.0.0"
[dev-dependencies]
criterion = { workspace = true, features = ["async_tokio"] }
......
This diff is collapsed.
......@@ -8,8 +8,8 @@ use std::{
};
use anyhow::Result;
use bincode::{Decode, Encode};
use pin_project_lite::pin_project;
use serde::{Deserialize, Serialize};
use crate::{backend::TurboTasksExecutionErrorMessage, panic_hooks::LAST_ERROR_LOCATION};
......@@ -26,7 +26,7 @@ impl<T, F: Future<Output = T>> CaptureFuture<T, F> {
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct TurboTasksPanic {
pub message: TurboTasksExecutionErrorMessage,
pub location: Option<String>,
......
......@@ -73,6 +73,7 @@ macro_rules! define_id {
type Target = $primitive;
fn deref(&self) -> &Self::Target {
// SAFETY: `NonZero<T>` is guaranteed to have the same layout as `T`
unsafe { transmute_copy(&&self.id) }
}
}
......
......@@ -108,41 +108,3 @@ impl<'de> DeserializeSeed<'de> for MagicAnyDeserializeSeed {
(self.functor)(&mut deserializer).map_err(serde::de::Error::custom)
}
}
type AnyDeserializeSeedFunctor = fn(
&mut dyn erased_serde::Deserializer<'_>,
) -> Result<Box<dyn Any + Sync + Send>, erased_serde::Error>;
#[derive(Clone, Copy)]
pub struct AnyDeserializeSeed {
functor: AnyDeserializeSeedFunctor,
}
impl AnyDeserializeSeed {
pub fn new<T>() -> Self
where
T: for<'de> Deserialize<'de> + Any + Send + Sync + 'static,
{
fn deserialize<T: Any + for<'de> Deserialize<'de> + Send + Sync + 'static>(
deserializer: &mut dyn erased_serde::Deserializer<'_>,
) -> Result<Box<dyn Any + Sync + Send>, erased_serde::Error> {
let value: T = erased_serde::deserialize(deserializer)?;
Ok(Box::new(value))
}
Self {
functor: deserialize::<T>,
}
}
}
impl<'de> DeserializeSeed<'de> for AnyDeserializeSeed {
type Value = Box<dyn Any + Sync + Send>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut deserializer = <dyn erased_serde::Deserializer>::erase(deserializer);
(self.functor)(&mut deserializer).map_err(serde::de::Error::custom)
}
}
......@@ -44,8 +44,6 @@ __turbo_tasks_internal_primitive!(Duration);
__turbo_tasks_internal_primitive!(Vec<u8>);
__turbo_tasks_internal_primitive!(Vec<bool>);
// TODO: use this in https://github.com/vercel/next.js/pull/86338
#[allow(dead_code)]
struct JsonValueEncodeWrapper<'a>(&'a serde_json::Value);
impl ManualEncodeWrapper for JsonValueEncodeWrapper<'_> {
......@@ -62,8 +60,6 @@ impl Encode for JsonValueEncodeWrapper<'_> {
}
}
// TODO: use this in https://github.com/vercel/next.js/pull/86338
#[allow(dead_code)]
struct JsonValueDecodeWrapper(serde_json::Value);
impl ManualDecodeWrapper for JsonValueDecodeWrapper {
......
......@@ -211,7 +211,7 @@ impl RawVc {
task,
index,
ReadCellOptions {
is_serializable_cell_content: value_type.is_serializable(),
is_serializable_cell_content: value_type.bincode.is_some(),
final_read_hint: false,
tracking: ReadTracking::default(),
},
......@@ -467,7 +467,7 @@ impl Future for ReadRawVcFuture {
if this.is_serializable_cell_content_unknown {
let value_type = registry::get_value_type(index.type_id);
this.read_cell_options.is_serializable_cell_content =
value_type.is_serializable();
value_type.bincode.is_some();
}
let read_result =
tt.try_read_task_cell(task, index, this.read_cell_options);
......
......@@ -6,11 +6,20 @@ use std::{
};
use anyhow::Result;
use serde::{Deserialize, Serialize, ser::SerializeTuple};
use bincode::{
Decode, Encode,
de::Decoder,
enc::Encoder,
error::{DecodeError, EncodeError},
impl_borrow_decode,
};
use turbo_bincode::{
TurboBincodeDecoder, TurboBincodeEncoder, turbo_bincode_decode, turbo_bincode_encode,
};
use unsize::CoerceUnsize;
use crate::{
ValueTypeId, registry,
ValueType, ValueTypeId, registry,
triomphe_utils::{coerce_to_any_send_sync, downcast_triomphe_arc},
};
......@@ -55,8 +64,101 @@ impl TypedSharedReference {
pub fn into_untyped(self) -> SharedReference {
self.reference
}
fn encode(&self, enc: &mut TurboBincodeEncoder) -> Result<(), EncodeError> {
let Self { type_id, reference } = self;
let value_type = registry::get_value_type(*type_id);
if let Some(bincode) = value_type.bincode {
type_id.encode(enc)?;
bincode.0(&*reference.0, enc)?;
Ok(())
} else {
Err(EncodeError::OtherString(format!(
"{:?} is not serializable",
value_type.global_name
)))
}
}
fn decode(dec: &mut TurboBincodeDecoder) -> Result<Self, DecodeError> {
let type_id = ValueTypeId::decode(dec)?;
let value_type = registry::get_value_type(type_id);
if let Some(bincode) = value_type.bincode {
let reference = bincode.1(dec)?;
Ok(Self { type_id, reference })
} else {
#[cold]
fn not_deserializable(value_type: &ValueType) -> DecodeError {
DecodeError::OtherString(format!("{value_type} is not deserializable"))
}
Err(not_deserializable(value_type))
}
}
}
impl Encode for TypedSharedReference {
fn encode<'a, E: Encoder>(&self, encoder: &'a mut E) -> Result<(), EncodeError> {
let maybe_turbo_encoder = if unty::type_equal::<E, TurboBincodeEncoder>() {
// SAFETY: Transmute is safe because `&mut E` is `&mut TurboBincodeEncoder`:
// - `unty::type_equal::<E, TurboBincodeEncoder>()` does not check lifetimes, but does
// check the type and layout, so we know those are correct.
// - The transmuted encoder cannot escape this function, and we know that the lifetime
// of `'f` is at least as long as the function.
// - Lifetimes don't change layout. This is not guaranteed, but if this assumption is
// broken, we'd have a different type id, `type_equal` would return `false` and we'd
// fall back to a slower codepath, and wouldn't violate memory safety.
// - Two mutable references have the same layout and alignment when they reference
// exactly the same type.
// - The explicit lifetime ('a) avoids creating an implitly unbounded lifetime.
Ok(unsafe { std::mem::transmute::<&'a mut E, &'a mut TurboBincodeEncoder>(encoder) })
} else {
Err(encoder)
};
match maybe_turbo_encoder {
Ok(turbo_encoder) => TypedSharedReference::encode(self, turbo_encoder),
Err(generic_encoder) => {
// The underlying `SharedReference` can only be serialized using
// `TurboBincodeEncoder` because the encoder function pointer cannot take type
// parameters. This is okay, because we expect any hot codepaths to use
// `TurboBincodeEncoder`.
//
// Create a `TurboBincodeEncoder` and encode this as a nested byte array. We must
// redundantly store a size here, otherwise we won't be able to determine what size
// buffer to use for `TurboBincodeReader`.
let buffer = turbo_bincode_encode(self)?;
buffer.encode(generic_encoder)
}
}
}
}
impl<Context> Decode<Context> for TypedSharedReference {
fn decode<'a, D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
let maybe_turbo_decoder = if unty::type_equal::<D, TurboBincodeDecoder>() {
// SAFETY: See notes on the `Encode::encode` implementation above.
Ok(unsafe { std::mem::transmute::<&mut D, &mut TurboBincodeDecoder<'a>>(decoder) })
} else {
Err(decoder)
};
match maybe_turbo_decoder {
Ok(turbo_decoder) => TypedSharedReference::decode(turbo_decoder),
Err(generic_decoder) => {
// The underlying `SharedReference` can only be deserialized using
// `TurboBincodeDecoder` because the decoder function pointer cannot take type
// parameters. This is okay, because we expect any hot codepaths to use
// `TurboBincodeDecoder`.
//
// Decode the nested byte array that was created during encoding, then use a
// `TurboBincodeDecoder` to decode the contents.
let buffer: Vec<u8> = Decode::decode(generic_decoder)?;
turbo_bincode_decode(&buffer)
}
}
}
}
impl_borrow_decode!(TypedSharedReference);
impl Deref for TypedSharedReference {
type Target = SharedReference;
......@@ -98,30 +200,6 @@ impl Debug for SharedReference {
}
}
impl Serialize for TypedSharedReference {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let TypedSharedReference {
type_id: ty,
reference: SharedReference(arc),
} = self;
let value_type = registry::get_value_type(*ty);
if let Some(serializable) = value_type.any_as_serializable(arc) {
let mut t = serializer.serialize_tuple(2)?;
t.serialize_element(ty)?;
t.serialize_element(serializable)?;
t.end()
} else {
Err(serde::ser::Error::custom(format!(
"{:?} is not serializable",
registry::get_value_type(*ty).global_name
)))
}
}
}
impl Display for SharedReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "untyped value")
......@@ -137,54 +215,3 @@ impl Display for TypedSharedReference {
)
}
}
impl<'de> Deserialize<'de> for TypedSharedReference {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = TypedSharedReference;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a serializable shared reference")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
if let Some(type_id) = seq.next_element()? {
let value_type = registry::get_value_type(type_id);
if let Some(seed) = value_type.get_any_deserialize_seed() {
if let Some(value) = seq.next_element_seed(seed)? {
let arc = triomphe::Arc::<dyn Any + Send + Sync>::from(value);
Ok(TypedSharedReference {
type_id,
reference: SharedReference(arc),
})
} else {
Err(serde::de::Error::invalid_length(
1,
&"tuple with type and value",
))
}
} else {
Err(serde::de::Error::custom(format!(
"{value_type} is not deserializable"
)))
}
} else {
Err(serde::de::Error::invalid_length(
0,
&"tuple with type and value",
))
}
}
}
deserializer.deserialize_tuple(2, Visitor)
}
}
use std::{fmt::Debug, future::Future, marker::PhantomData};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use crate::{
Vc, VcValueTrait,
......@@ -57,21 +56,6 @@ impl<T> std::hash::Hash for TraitRef<T> {
}
}
impl<T> Serialize for TraitRef<T> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.shared_reference.serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for TraitRef<T> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self {
shared_reference: TypedSharedReference::deserialize(deserializer)?,
_t: PhantomData,
})
}
}
impl<U> std::ops::Deref for TraitRef<Box<U>>
where
Box<U>: VcValueTrait<ValueTrait = U>,
......
......@@ -5,19 +5,23 @@ use std::{
};
use auto_hash_map::{AutoMap, AutoSet};
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use bincode::{
Decode, Encode,
error::{DecodeError, EncodeError},
};
use tracing::Span;
use turbo_bincode::{TurboBincodeDecoder, TurboBincodeEncoder};
use crate::{
RawVc, VcValueType, id::TraitTypeId, macro_helpers::NativeFunction,
magic_any::AnyDeserializeSeed, registry, task::shared_reference::TypedSharedReference,
vc::VcCellMode,
RawVc, SharedReference, VcValueType, id::TraitTypeId, macro_helpers::NativeFunction, registry,
task::shared_reference::TypedSharedReference, vc::VcCellMode,
};
type AnySerializationFn = fn(&(dyn Any + Sync + Send)) -> &dyn erased_serde::Serialize;
type RawCellFactoryFn = fn(TypedSharedReference) -> RawVc;
type AnyEncodeFn = fn(&dyn Any, &mut TurboBincodeEncoder<'_>) -> Result<(), EncodeError>;
type AnyDecodeFn = fn(&mut TurboBincodeDecoder<'_>) -> Result<SharedReference, DecodeError>;
// TODO this type need some refactoring when multiple languages are added to
// turbo-task In this case a trait_method might be of a different function type.
// It probably need to be a Vc<Function>.
......@@ -37,14 +41,14 @@ pub struct ValueType {
/// List of trait methods available
trait_methods: AutoMap<&'static TraitMethod, &'static NativeFunction>,
/// Functors for serialization
any_serialization: Option<(AnySerializationFn, AnyDeserializeSeed)>,
/// Functions to convert to write the type to a buffer or read it from a buffer.
pub bincode: Option<(AnyEncodeFn, AnyDecodeFn)>,
/// An implementation of
/// [`VcCellMode::raw_cell`][crate::vc::cell_mode::VcCellMode::raw_cell].
/// [`VcCellMode::raw_cell`][crate::vc::VcCellMode::raw_cell].
///
/// Allows dynamically constructing a cell using the type id. Used inside of
/// [`RawVc`] where we have a type id, but not the concrete type `T` of
/// [`TraitRef`][crate::TraitRef] where we have a type id, but not the concrete type `T` of
/// `Vc<T>`.
///
/// Because we allow resolving `Vc<dyn Trait>`, it's otherwise not possible
......@@ -87,81 +91,98 @@ impl Display for ValueType {
}
}
pub fn any_as_serialize<T: Any + Serialize + Send + Sync + 'static>(
this: &(dyn Any + Send + Sync),
) -> &dyn erased_serde::Serialize {
if let Some(r) = this.downcast_ref::<T>() {
return r;
pub fn any_as_encode<T: Any>(this: &dyn Any) -> &T {
if let Some(enc) = this.downcast_ref::<T>() {
return enc;
}
panic!(
"any_as_serialize::<{}> called with invalid type",
unreachable!(
"any_as_encode::<{}> called with invalid type",
type_name::<T>()
);
}
// TODO: use this in https://github.com/vercel/next.js/pull/86338
#[allow(dead_code)]
pub trait ManualEncodeWrapper: Encode {
type Value;
// this uses RPIT to avoid some lifetime problems
fn new<'a>(value: &'a Self::Value) -> impl Encode + 'a;
}
// TODO: use this in https://github.com/vercel/next.js/pull/86338
#[allow(dead_code)]
pub trait ManualDecodeWrapper: Decode<()> {
type Value;
fn inner(self) -> Self::Value;
}
impl ValueType {
/// This is internally used by `#[turbo_tasks::value]`
/// This is internally used by [`#[turbo_tasks::value]`][crate::value].
pub fn new<T: VcValueType>(global_name: &'static str) -> Self {
Self {
name: std::any::type_name::<T>(),
Self::new_inner::<T>(global_name, None)
}
/// This is internally used by [`#[turbo_tasks::value]`][crate::value].
pub fn new_with_bincode<T: VcValueType + Encode + Decode<()>>(
global_name: &'static str,
) -> Self {
Self::new_inner::<T>(
global_name,
traits: AutoSet::new(),
trait_methods: AutoMap::new(),
any_serialization: None,
raw_cell: <T::CellMode as VcCellMode<T>>::raw_cell,
}
Some((
|this, enc| {
T::encode(any_as_encode::<T>(this), enc)?;
Ok(())
},
|dec| {
let val = T::decode(dec)?;
Ok(SharedReference::new(triomphe::Arc::new(val)))
},
)),
)
}
/// This is internally used by `#[turbo_tasks::value]`
pub fn new_with_any_serialization<
T: VcValueType + Any + Serialize + for<'de> Deserialize<'de>,
/// This is used internally by [`turbo_tasks_macros::primitive`] to encode/decode foreign types
/// that cannot implement the [`bincode`] traits due to the [orphan rules].
///
/// This is done by constructing wrapper types that implement the bincode traits on behalf of
/// the wrapped type.
///
/// [orphan rules]: https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
pub fn new_with_bincode_wrappers<
T: VcValueType,
E: ManualEncodeWrapper<Value = T>,
D: ManualDecodeWrapper<Value = T>,
>(
global_name: &'static str,
) -> Self {
Self::new_inner::<T>(
global_name,
Some((
|this, enc| {
E::new(any_as_encode::<T>(this)).encode(enc)?;
Ok(())
},
|dec| {
let val = D::inner(D::decode(dec)?);
Ok(SharedReference::new(triomphe::Arc::new(val)))
},
)),
)
}
// Helper for other constructor functions
fn new_inner<T: VcValueType>(
global_name: &'static str,
bincode: Option<(AnyEncodeFn, AnyDecodeFn)>,
) -> Self {
Self {
name: std::any::type_name::<T>(),
global_name,
traits: AutoSet::new(),
trait_methods: AutoMap::new(),
any_serialization: Some((any_as_serialize::<T>, AnyDeserializeSeed::new::<T>())),
bincode,
raw_cell: <T::CellMode as VcCellMode<T>>::raw_cell,
}
}
pub fn any_as_serializable<'a>(
&self,
arc: &'a triomphe::Arc<dyn Any + Sync + Send>,
) -> Option<&'a dyn erased_serde::Serialize> {
if let Some(s) = self.any_serialization {
Some((s.0)(&**arc))
} else {
None
}
}
pub fn is_serializable(&self) -> bool {
self.any_serialization.is_some()
}
pub fn get_any_deserialize_seed(&self) -> Option<AnyDeserializeSeed> {
self.any_serialization.map(|s| s.1)
}
pub(crate) fn register_trait_method(
&mut self,
trait_method: &'static TraitMethod,
......
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