Skip to content

Commit 2931550

Browse files
authored
Turbopack: bincode: Implement bincode Encode/Decode traits on all turbo task values (#85580)
It looks like bincode should reduce the filesystem cache size by ~10% versus pot, because its not a self-describing format. This attempts to add the `bincode::Encode`/`bincode::Decode` traits everywhere, so that we can use those instead of serde. ## Why not use bincode's serde compatibility feature? - Unfortunately, serde has some bugs/incompatibilities/footguns with non-self-describing formats. `bincode`'s traits avoid this by not exposing features that would be incompatible with a self-describing format. https://docs.rs/bincode/latest/bincode/serde/index.html#known-issues - `bincode::Encode`/`Decode` are much simpler traits than serde's equivalents, so we'll probably get some rustc build performance and turbopack binary size benefits from using the bincode versions. In cases where we do have to implement these traits by hand, it's much easier than implementing the serde equivalents. - I'd like to kill `erased-serde` anyways because we're probably paying some performance cost from the large amounts of dynamic dispatch it performs, and we're not getting any benefits from it if we only ever serialize to a single format. ## Forks of Upstream Crates - I've forked `bincode` here: https://github.com/bgw/bincode/commits/bgw/patches/ - I submitted a patch via email to the maintainer via sourcehut - Upstream: https://git.sr.ht/~stygianentity/bincode - Fork: https://git.sr.ht/~bgw/bincode - This adds a few extra derive features and fixes type bounds on `PhantomData`. - The type bounds on `PhantomData` aren't very important, and we could work around it in our code. - We could just fork the derive crate if upstream doesn't want to accept the patches. - And `virtue` here: bgw/virtue@e386f35 - I've submitted a PR to `virtue` here: bincode-org/virtue#94 - This fixes a bug in the parsing of const enums and generics with defaults. - We don't hit this very often as most of our types are not generics, so we could work around it in our code if upstream doesn't want to accept the patch. ## Other Notes - Depends on a patched version of the `bincode` and `virtue` crates to fix a few bugs and add a few new features. I'll work on upstreaming these changes. - This implements `BorrowDecode` everywhere because the `bincode::Decode` derive macro requires it. I plan to add an attribute in my bincode fork to allow disabling this, and then we can get rid of all of the `BorrowDecode` impls. We don't want/need `BorrowDecode` for turbo task values because everything needs to be owned anyways.
1 parent 5118c41 commit 2931550

File tree

181 files changed

+2636
-476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+2636
-476
lines changed

Cargo.lock

Lines changed: 39 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,12 @@ serde_bytes = "0.11.15"
451451
serde_path_to_error = "0.1.16"
452452
serde_qs = "0.13.0"
453453
serde_with = "3.12.0"
454-
smallvec = { version = "1.13.1", features = [
454+
smallvec = { version = "1.15.1", features = [
455455
"serde",
456456
"const_generics",
457457
"union",
458458
"const_new",
459+
"impl_bincode",
459460
] }
460461
swc_sourcemap = "9.3.4"
461462
strsim = "0.11.1"
@@ -481,4 +482,5 @@ inventory = "0.3.21"
481482

482483
[patch.crates-io]
483484
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" }
484486
mdxjs = { git = "https://github.com/mischnic/mdxjs-rs.git", branch = "swc-core-32" }

crates/napi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ignored = [
5656

5757
[dependencies]
5858
anyhow = { workspace = true }
59+
bincode = { workspace = true }
5960
console-subscriber = { workspace = true, optional = true }
6061
dhat = { workspace = true, optional = true }
6162
either = { workspace = true }

crates/napi/src/next_api/project.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{borrow::Cow, io::Write, path::PathBuf, sync::Arc, thread, time::Duration};
22

33
use anyhow::{Context, Result, anyhow, bail};
4+
use bincode::{Decode, Encode};
45
use flate2::write::GzEncoder;
56
use futures_util::TryFutureExt;
67
use napi::{
@@ -1528,6 +1529,8 @@ pub fn project_compilation_events_subscribe(
15281529
Serialize,
15291530
TaskInput,
15301531
TraceRawVcs,
1532+
Encode,
1533+
Decode,
15311534
)]
15321535
pub struct StackFrame {
15331536
pub is_server: bool,

crates/next-api/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ workspace = true
1414

1515
[dependencies]
1616
anyhow = { workspace = true }
17+
bincode = { workspace = true }
1718
byteorder = { workspace = true }
1819
either = { workspace = true }
1920
futures = { workspace = true }
@@ -26,6 +27,7 @@ serde = { workspace = true }
2627
serde_json = { workspace = true }
2728
swc_core = { workspace = true }
2829
tracing = { workspace = true }
30+
turbo-bincode = { workspace = true }
2931
turbo-rcstr = { workspace = true }
3032
turbo-tasks = { workspace = true }
3133
turbo-tasks-hash = { workspace = true }

crates/next-api/src/app.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::{Context, Result, bail};
2+
use bincode::{Decode, Encode};
23
use next_core::{
34
app_structure::{
45
AppPageLoaderTree, CollectedRootParams, Entrypoint as AppEntrypoint,
@@ -1068,13 +1069,27 @@ pub fn app_entry_point_to_route(
10681069
#[turbo_tasks::value(transparent)]
10691070
struct OutputAssetsWithAvailability((ResolvedVc<OutputAssets>, AvailabilityInfo));
10701071

1071-
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Debug, TraceRawVcs, NonLocalValue)]
1072+
#[derive(
1073+
Copy,
1074+
Clone,
1075+
Serialize,
1076+
Deserialize,
1077+
PartialEq,
1078+
Eq,
1079+
Debug,
1080+
TraceRawVcs,
1081+
NonLocalValue,
1082+
Encode,
1083+
Decode,
1084+
)]
10721085
enum AppPageEndpointType {
10731086
Html,
10741087
Rsc,
10751088
}
10761089

1077-
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Debug, TraceRawVcs, NonLocalValue)]
1090+
#[derive(
1091+
Clone, Serialize, Deserialize, PartialEq, Eq, Debug, TraceRawVcs, NonLocalValue, Encode, Decode,
1092+
)]
10781093
enum AppEndpointType {
10791094
Page {
10801095
ty: AppPageEndpointType,

0 commit comments

Comments
 (0)