Skip to content

Commit 1757d4c

Browse files
committed
WIP: Turbopack: Use bincode to store the contents of value cells
1 parent 24e2ebb commit 1757d4c

File tree

24 files changed

+562
-728
lines changed

24 files changed

+562
-728
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ preset_env_base = "5.0.0"
360360

361361

362362
# General Deps
363-
bincode = { version = "2.0.1", features = ["serde"] }
364363
chromiumoxide = { version = "0.5.4", features = [
365364
"tokio-runtime",
366365
], default-features = false }
@@ -378,6 +377,7 @@ async-compression = { version = "0.3.13", default-features = false, features = [
378377
"tokio",
379378
] }
380379
async-trait = "0.1.64"
380+
bincode = { version = "2.0.1", features = ["serde"] }
381381
bitfield = "0.18.0"
382382
byteorder = "1.5.0"
383383
bytes = "1.1.0"
@@ -397,12 +397,13 @@ either = "1.9.0"
397397
erased-serde = "0.4.5"
398398
flate2 = "1.0.28"
399399
futures = "0.3.31"
400-
futures-util = "0.3.31"
401400
futures-retry = "0.6.0"
401+
futures-util = "0.3.31"
402402
hashbrown = "0.14.5"
403403
image = { version = "0.25.8", default-features = false }
404404
indexmap = "2.7.1"
405405
indoc = "2.0.0"
406+
inventory = "0.3.21"
406407
itertools = "0.10.5"
407408
lightningcss = { version = "1.0.0-alpha.68", features = [
408409
"serde",
@@ -447,11 +448,10 @@ ringmap = "0.1.3"
447448
roaring = "0.10.10"
448449
rstest = "0.16.0"
449450
rustc-hash = "2.1.1"
450-
twox-hash = { version = "2.1.0", features = ["xxhash64", "xxhash3_128"] }
451451
semver = "1.0.16"
452452
serde = { version = "1.0.217", features = ["derive"] }
453-
serde_json = "1.0.138"
454453
serde_bytes = "0.11.15"
454+
serde_json = "1.0.138"
455455
serde_path_to_error = "0.1.16"
456456
serde_qs = "0.13.0"
457457
serde_with = "3.12.0"
@@ -462,26 +462,27 @@ smallvec = { version = "1.15.1", features = [
462462
"const_new",
463463
"impl_bincode",
464464
] }
465-
swc_sourcemap = "9.3.4"
466-
strsim = "0.11.1"
467465
shrink-to-fit = "0.2.10"
466+
strsim = "0.11.1"
467+
swc_sourcemap = "9.3.4"
468468
syn = "2.0.100"
469469
tempfile = "3.20.0"
470-
thread_local = "1.1.8"
471470
thiserror = "1.0.48"
471+
thread_local = "1.1.8"
472472
tokio = "1.43.0"
473473
tokio-util = { version = "0.7.13", features = ["io", "rt"] }
474474
tracing = "0.1.37"
475475
tracing-subscriber = "0.3.16"
476476
triomphe = { git = "https://github.com/sokra/triomphe", branch = "sokra/unstable" }
477+
twox-hash = { version = "2.1.0", features = ["xxhash64", "xxhash3_128"] }
477478
unsize = "1.1.0"
479+
unty = "0.0.4"
478480
url = "2.2.2"
479481
urlencoding = "2.1.2"
480482
uuid = "1.18.1"
481483
vergen = { version = "9.0.6", features = ["cargo"] }
482484
vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
483485
webbrowser = "1.0.6"
484-
inventory = "0.3.21"
485486

486487
[patch.crates-io]
487488
bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" }

turbopack/crates/turbo-bincode/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ indexmap = { workspace = true }
1717
mime = { workspace = true }
1818
serde = { workspace = true }
1919
serde_json = { workspace = true }
20+
smallvec = { workspace = true }

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

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
1+
use std::ptr::copy_nonoverlapping;
2+
3+
use ::smallvec::SmallVec;
14
use bincode::{
25
BorrowDecode, Decode, Encode,
3-
de::{BorrowDecoder, Decoder},
4-
enc::Encoder,
6+
de::{BorrowDecoder, Decoder, DecoderImpl, read::Reader},
7+
enc::{Encoder, EncoderImpl, write::Writer},
58
error::{DecodeError, EncodeError},
69
};
710

11+
pub const TURBO_BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard();
12+
pub type TurboBincodeBuffer = SmallVec<[u8; 16]>;
13+
pub type TurboBincodeEncoder = EncoderImpl<TurboBincodeWriter, bincode::config::Configuration>;
14+
pub type TurboBincodeDecoder<'a> =
15+
DecoderImpl<TurboBincodeReader<'a>, bincode::config::Configuration, ()>;
16+
17+
pub fn new_turbo_bincode_encoder() -> TurboBincodeEncoder {
18+
EncoderImpl::new(TurboBincodeWriter::new(), TURBO_BINCODE_CONFIG)
19+
}
20+
21+
pub fn new_turbo_bincode_decoder<'a>(buffer: &'a [u8]) -> TurboBincodeDecoder<'a> {
22+
DecoderImpl::new(TurboBincodeReader::new(buffer), TURBO_BINCODE_CONFIG, ())
23+
}
24+
25+
pub struct TurboBincodeWriter {
26+
pub buffer: TurboBincodeBuffer,
27+
}
28+
29+
impl TurboBincodeWriter {
30+
pub fn new() -> Self {
31+
Self {
32+
buffer: SmallVec::new(),
33+
}
34+
}
35+
}
36+
37+
impl Writer for TurboBincodeWriter {
38+
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
39+
self.buffer.extend_from_slice(bytes);
40+
Ok(())
41+
}
42+
}
43+
44+
pub struct TurboBincodeReader<'a> {
45+
pub buffer: &'a [u8],
46+
}
47+
48+
impl<'a> TurboBincodeReader<'a> {
49+
pub fn new(buffer: &'a [u8]) -> Self {
50+
Self { buffer }
51+
}
52+
}
53+
54+
impl Reader for TurboBincodeReader<'_> {
55+
fn read(&mut self, target_buffer: &mut [u8]) -> Result<(), DecodeError> {
56+
let len = target_buffer.len();
57+
let (head, rest) = self
58+
.buffer
59+
.split_at_checked(len)
60+
.ok_or(DecodeError::UnexpectedEnd {
61+
additional: target_buffer.len() - self.buffer.len(),
62+
})?;
63+
// SAFETY:
64+
// - We already checked the bounds.
65+
// - These memory ranges can't overlap because it would violate rust aliasing rules.
66+
// - `u8` is `Copy`.
67+
unsafe {
68+
copy_nonoverlapping(head.as_ptr(), target_buffer.as_mut_ptr(), len);
69+
}
70+
self.buffer = rest;
71+
Ok(())
72+
}
73+
74+
fn peek_read(&mut self, n: usize) -> Option<&[u8]> {
75+
Some(&self.buffer[..n])
76+
}
77+
78+
fn consume(&mut self, n: usize) {
79+
self.buffer = &self.buffer[n..];
80+
}
81+
}
82+
883
pub mod indexmap {
984
use std::hash::{BuildHasher, Hash};
1085

@@ -381,3 +456,70 @@ pub mod either {
381456
}
382457
}
383458
}
459+
460+
pub mod smallvec {
461+
use ::smallvec::Array;
462+
463+
use super::*;
464+
465+
pub fn encode<E: Encoder, A: Array<Item = impl Encode>>(
466+
vec: &SmallVec<A>,
467+
encoder: &mut E,
468+
) -> Result<(), EncodeError> {
469+
usize::encode(&vec.len(), encoder)?;
470+
for item in vec {
471+
Encode::encode(item, encoder)?;
472+
}
473+
Ok(())
474+
}
475+
476+
pub fn decode<Context, D: Decoder<Context = Context>, A: Array<Item = impl Decode<Context>>>(
477+
decoder: &mut D,
478+
) -> Result<SmallVec<A>, DecodeError> {
479+
let len = usize::decode(decoder)?;
480+
let mut vec = SmallVec::with_capacity(len);
481+
for _ in 0..len {
482+
vec.push(Decode::decode(decoder)?);
483+
}
484+
Ok(vec)
485+
}
486+
487+
pub fn borrow_decode<
488+
'de,
489+
Context,
490+
D: BorrowDecoder<'de, Context = Context>,
491+
A: Array<Item = impl BorrowDecode<'de, Context>>,
492+
>(
493+
decoder: &mut D,
494+
) -> Result<SmallVec<A>, DecodeError> {
495+
let len = usize::decode(decoder)?;
496+
let mut vec = SmallVec::with_capacity(len);
497+
for _ in 0..len {
498+
vec.push(BorrowDecode::borrow_decode(decoder)?);
499+
}
500+
Ok(vec)
501+
}
502+
503+
#[cfg(test)]
504+
mod tests {
505+
use bincode::{decode_from_slice, encode_to_vec};
506+
507+
use super::*;
508+
509+
#[test]
510+
fn test_roundtrip() {
511+
let cfg = bincode::config::standard();
512+
513+
#[derive(Encode, Decode)]
514+
struct Wrapper(#[bincode(with = "crate::smallvec")] SmallVec<[u32; 4]>);
515+
516+
let vec1 = Wrapper(SmallVec::from_slice(&[1u32, 2, 3, 4, 5]));
517+
518+
let vec2: Wrapper = decode_from_slice(&encode_to_vec(&vec1, cfg).unwrap(), cfg)
519+
.unwrap()
520+
.0;
521+
522+
assert_eq!(vec1.0, vec2.0);
523+
}
524+
}
525+
}

turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
};
1111

1212
use anyhow::Result;
13+
use bincode::{Decode, Encode};
1314
use indexmap::map::Entry;
1415
use ringmap::RingSet;
1516
use rustc_hash::{FxBuildHasher, FxHashMap};
@@ -91,9 +92,11 @@ pub fn get_aggregation_number(task: &impl TaskGuard) -> u32 {
9192
.unwrap_or_default()
9293
}
9394

94-
#[derive(Serialize, Deserialize, Clone, Debug)]
95+
#[derive(Encode, Decode, Clone, Debug)]
9596
pub struct InnerOfUppersHasNewFollowersJob {
97+
#[bincode(with = "turbo_bincode::smallvec")]
9698
pub upper_ids: TaskIdVec,
99+
#[bincode(with = "turbo_bincode::smallvec")]
97100
pub new_follower_ids: TaskIdVec,
98101
}
99102

@@ -103,9 +106,11 @@ impl From<InnerOfUppersHasNewFollowersJob> for AggregationUpdateJob {
103106
}
104107
}
105108

106-
#[derive(Serialize, Deserialize, Clone, Debug)]
109+
#[derive(Encode, Decode, Clone, Debug)]
107110
pub struct InnerOfUppersLostFollowersJob {
111+
#[bincode(with = "turbo_bincode::smallvec")]
108112
pub upper_ids: TaskIdVec,
113+
#[bincode(with = "turbo_bincode::smallvec")]
109114
pub lost_follower_ids: TaskIdVec,
110115
}
111116

@@ -115,7 +120,7 @@ impl From<InnerOfUppersLostFollowersJob> for AggregationUpdateJob {
115120
}
116121
}
117122

118-
#[derive(Serialize, Deserialize, Clone, Debug)]
123+
#[derive(Encode, Decode, Clone, Debug)]
119124
pub struct AggregatedDataUpdateJob {
120125
pub upper_ids: TaskIdVec,
121126
pub update: AggregatedDataUpdate,

turbopack/crates/turbo-tasks-backend/src/backend/operation/update_cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::mem::take;
22

3-
use serde::{Deserialize, Serialize};
3+
use bincode::{Decode, Encode};
44
use smallvec::SmallVec;
55
#[cfg(not(feature = "verify_determinism"))]
66
use turbo_tasks::backend::VerificationMode;
@@ -20,7 +20,7 @@ use crate::{
2020
data::{CachedDataItem, CachedDataItemKey, CellRef},
2121
};
2222

23-
#[derive(Serialize, Deserialize, Clone, Default)]
23+
#[derive(Encode, Decode, Clone, Default)]
2424
#[allow(clippy::large_enum_variant)]
2525
pub enum UpdateCellOperation {
2626
InvalidateWhenCellDependency {

0 commit comments

Comments
 (0)