Skip to content

Commit 77a6a79

Browse files
committed
WIP: Turbopack: Implement bincode Encode/Decode traits on all turbo task values
1 parent 7f3d072 commit 77a6a79

File tree

33 files changed

+673
-91
lines changed

33 files changed

+673
-91
lines changed

Cargo.lock

Lines changed: 39 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ preset_env_base = "5.0.0"
359359

360360

361361
# General Deps
362+
bincode = { version = "2.0.1", features = ["serde"] }
362363
chromiumoxide = { version = "0.5.4", features = [
363364
"tokio-runtime",
364365
], default-features = false }
@@ -480,4 +481,6 @@ webbrowser = "1.0.6"
480481
inventory = "0.3.21"
481482

482483
[patch.crates-io]
484+
bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" }
485+
virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-default-parsing" }
483486
mdxjs = { git = "https://github.com/mischnic/mdxjs-rs.git", branch = "swc-core-32" }

turbopack/crates/turbo-rcstr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ atom_size_128 = []
1010
napi = ["dep:napi"]
1111

1212
[dependencies]
13+
bincode = { workspace = true }
1314
triomphe = { workspace = true }
1415
turbo-tasks-hash = { workspace = true }
1516
serde = { workspace = true }

turbopack/crates/turbo-rcstr/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
path::{Path, PathBuf},
1010
};
1111

12+
use bincode::{Decode, Encode, de::Decoder, enc::Encoder, error::EncodeError, impl_borrow_decode};
1213
use bytes_str::BytesStr;
1314
use debug_unreachable::debug_unreachable;
1415
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -369,6 +370,22 @@ impl<'de> Deserialize<'de> for RcStr {
369370
}
370371
}
371372

373+
impl Encode for RcStr {
374+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
375+
self.as_str().encode(encoder)
376+
}
377+
}
378+
379+
impl<Context> Decode<Context> for RcStr {
380+
fn decode<D: Decoder<Context = Context>>(
381+
decoder: &mut D,
382+
) -> Result<Self, bincode::error::DecodeError> {
383+
Ok(RcStr::from(String::decode(decoder)?))
384+
}
385+
}
386+
387+
impl_borrow_decode!(RcStr);
388+
372389
impl Drop for RcStr {
373390
fn drop(&mut self) {
374391
match self.tag() {

turbopack/crates/turbo-tasks-auto-hash-map/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2024"
99
workspace = true
1010

1111
[dependencies]
12+
bincode = { workspace = true }
1213
hashbrown = { workspace = true, features = ["serde"]}
1314
rustc-hash = { workspace = true }
1415
serde = { workspace = true, features = ["derive"] }

turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ use std::{
55
marker::PhantomData,
66
};
77

8+
use bincode::{
9+
Decode, Encode,
10+
de::Decoder,
11+
enc::Encoder,
12+
error::{DecodeError, EncodeError},
13+
impl_borrow_decode,
14+
};
815
use hashbrown::hash_map::HashMap;
916
use rustc_hash::FxHasher;
1017
use serde::{
@@ -54,7 +61,7 @@ impl<K, V> AutoMap<K, V, BuildHasherDefault<FxHasher>, 0> {
5461
}
5562
}
5663

57-
impl<K, V, H: BuildHasher, const I: usize> AutoMap<K, V, H, I> {
64+
impl<K, V, H, const I: usize> AutoMap<K, V, H, I> {
5865
/// see [HashMap::with_hasher](https://doc.rust-lang.org/std/collections/hash_map/struct.HashMap.html#method.with_hasher)
5966
pub const fn with_hasher() -> Self {
6067
AutoMap::List(SmallVec::new_const())
@@ -813,6 +820,56 @@ where
813820
}
814821
}
815822

823+
impl<K, V, H, const I: usize> Encode for AutoMap<K, V, H, I>
824+
where
825+
K: Encode,
826+
V: Encode,
827+
{
828+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
829+
// Encode the length first
830+
self.len().encode(encoder)?;
831+
// Then encode each key-value tuple
832+
for entry in self.iter() {
833+
entry.encode(encoder)?;
834+
}
835+
Ok(())
836+
}
837+
}
838+
839+
impl<Context, K, V, H, const I: usize> Decode<Context> for AutoMap<K, V, H, I>
840+
where
841+
K: Decode<Context> + Eq + Hash,
842+
V: Decode<Context>,
843+
H: BuildHasher + Default,
844+
{
845+
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
846+
let len = usize::decode(decoder)?;
847+
if len <= MAX_LIST_SIZE {
848+
let mut list = SmallVec::with_capacity(len);
849+
for _ in 0..len {
850+
let entry = <(K, V)>::decode(decoder)?;
851+
list.push(entry);
852+
}
853+
Ok(AutoMap::List(list))
854+
} else {
855+
let mut map = HashMap::with_capacity_and_hasher(len, H::default());
856+
for _ in 0..len {
857+
let (key, value) = <(K, V)>::decode(decoder)?;
858+
map.insert(key, value);
859+
}
860+
Ok(AutoMap::Map(Box::new(map)))
861+
}
862+
}
863+
}
864+
865+
impl_borrow_decode!(
866+
AutoMap<K, V, H, I>,
867+
K: Decode<__Context> + Eq + Hash,
868+
V: Decode<__Context>,
869+
H: BuildHasher + Default,
870+
const I: usize,
871+
);
872+
816873
impl<K: Eq + Hash, V: Eq, H: BuildHasher, const I: usize> PartialEq for AutoMap<K, V, H, I> {
817874
fn eq(&self, other: &Self) -> bool {
818875
match (self, other) {

turbopack/crates/turbo-tasks-auto-hash-map/src/set.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ use std::{
44
marker::PhantomData,
55
};
66

7+
use bincode::{Decode, Encode};
78
use rustc_hash::FxHasher;
89
use serde::{Deserialize, Serialize};
910
use shrink_to_fit::ShrinkToFit;
1011

1112
use crate::AutoMap;
1213

13-
#[derive(Clone)]
14+
#[derive(Clone, Encode, Decode)]
15+
#[bincode(
16+
encode_bounds = "K: Encode + Hash + Eq, H: BuildHasher + Default",
17+
decode_bounds = "K: Decode<__Context> + Hash + Eq, H: BuildHasher + Default",
18+
borrow_decode_bounds = "K: Decode<__Context> + Hash + Eq, H: BuildHasher + Default"
19+
)]
1420
pub struct AutoSet<K, H = BuildHasherDefault<FxHasher>, const I: usize = 0> {
1521
map: AutoMap<K, (), H, I>,
1622
}

turbopack/crates/turbo-tasks-env/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub use self::{
1818
};
1919

2020
#[turbo_tasks::value(transparent)]
21-
pub struct EnvMap(#[turbo_tasks(trace_ignore)] FxIndexMap<RcStr, RcStr>);
21+
pub struct EnvMap(
22+
#[turbo_tasks(trace_ignore)]
23+
#[bincode(with = "turbo_tasks::bincode::indexmap")]
24+
FxIndexMap<RcStr, RcStr>,
25+
);
2226

2327
#[turbo_tasks::value_impl]
2428
impl EnvMap {

turbopack/crates/turbo-tasks-fs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ workspace = true
2525
[dependencies]
2626
anyhow = { workspace = true }
2727
auto-hash-map = { workspace = true }
28+
bincode = { workspace = true }
2829
bitflags = "1.3.2"
2930
bytes = { workspace = true }
3031
concurrent-queue = { workspace = true }

turbopack/crates/turbo-tasks-fs/src/glob.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use std::fmt::Display;
22

33
use anyhow::{Result, bail};
4+
use bincode::{
5+
Decode, Encode,
6+
de::Decoder,
7+
enc::Encoder,
8+
error::{DecodeError, EncodeError},
9+
};
410
use regex::bytes::{Regex, RegexBuilder};
511
use serde::{Deserialize, Serialize};
612
use turbo_rcstr::{RcStr, rcstr};
@@ -20,8 +26,8 @@ use crate::globset::parse;
2026
// Note: a/**/b does match a/b, so we need some special logic about path
2127
// separators
2228

23-
#[turbo_tasks::value(eq = "manual")]
24-
#[derive(Debug, Clone)]
29+
#[turbo_tasks::value(eq = "manual", serialization = "custom")]
30+
#[derive(Debug, Clone, Serialize, Deserialize)]
2531
#[serde(into = "GlobForm", try_from = "GlobForm")]
2632
pub struct Glob {
2733
glob: RcStr,
@@ -46,8 +52,37 @@ impl Display for Glob {
4652
write!(f, "Glob({})", self.glob)
4753
}
4854
}
55+
56+
impl Encode for Glob {
57+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
58+
self.glob.encode(encoder)?;
59+
self.opts.encode(encoder)?;
60+
Ok(())
61+
}
62+
}
63+
64+
impl<Context> Decode<Context> for Glob {
65+
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
66+
let glob = RcStr::decode(decoder)?;
67+
let opts = GlobOptions::decode(decoder)?;
68+
Glob::parse(glob, opts).map_err(|err| DecodeError::OtherString(err.to_string()))
69+
}
70+
}
71+
4972
#[derive(
50-
Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Hash, Default, TaskInput, TraceRawVcs, Debug,
73+
Serialize,
74+
Deserialize,
75+
Copy,
76+
Clone,
77+
PartialEq,
78+
Eq,
79+
Hash,
80+
Default,
81+
TaskInput,
82+
TraceRawVcs,
83+
Debug,
84+
Encode,
85+
Decode,
5186
)]
5287

5388
pub struct GlobOptions {

0 commit comments

Comments
 (0)