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