Skip to content

Commit 4b75e7e

Browse files
committed
Use the new self-describing format, remove serde_json wrapper
1 parent 13e6ab2 commit 4b75e7e

File tree

10 files changed

+53
-109
lines changed

10 files changed

+53
-109
lines changed

Cargo.lock

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

crates/next-core/src/next_config.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct NextConfig {
9292
cache_handler: Option<RcStr>,
9393
#[bincode(with_serde)]
9494
cache_handlers: Option<FxIndexMap<RcStr, RcStr>>,
95-
#[bincode(with = "turbo_bincode::serde_json")]
95+
#[bincode(with = "turbo_bincode::serde_self_describing")]
9696
env: FxIndexMap<String, JsonValue>,
9797
experimental: ExperimentalConfig,
9898
images: ImageConfig,
@@ -101,12 +101,12 @@ pub struct NextConfig {
101101
react_production_profiling: Option<bool>,
102102
react_strict_mode: Option<bool>,
103103
transpile_packages: Option<Vec<RcStr>>,
104-
#[bincode(with = "turbo_bincode::serde_json")]
104+
#[bincode(with = "turbo_bincode::serde_self_describing")]
105105
modularize_imports: Option<FxIndexMap<String, ModularizeImportPackageConfig>>,
106106
dist_dir: RcStr,
107107
dist_dir_root: RcStr,
108108
deployment_id: Option<RcStr>,
109-
#[bincode(with = "turbo_bincode::serde_json")]
109+
#[bincode(with = "turbo_bincode::serde_self_describing")]
110110
sass_options: Option<serde_json::Value>,
111111
trailing_slash: Option<bool>,
112112
asset_prefix: Option<RcStr>,
@@ -119,9 +119,9 @@ pub struct NextConfig {
119119
output: Option<OutputType>,
120120
turbopack: Option<TurbopackConfig>,
121121
production_browser_source_maps: bool,
122-
#[bincode(with = "turbo_bincode::serde_json")]
122+
#[bincode(with = "turbo_bincode::serde_self_describing")]
123123
output_file_tracing_includes: Option<serde_json::Value>,
124-
#[bincode(with = "turbo_bincode::serde_json")]
124+
#[bincode(with = "turbo_bincode::serde_self_describing")]
125125
output_file_tracing_excludes: Option<serde_json::Value>,
126126
// TODO: This option is not respected, it uses Turbopack's root instead.
127127
output_file_tracing_root: Option<RcStr>,
@@ -154,9 +154,9 @@ pub struct NextConfig {
154154
http_agent_options: HttpAgentConfig,
155155
on_demand_entries: OnDemandEntriesConfig,
156156
powered_by_header: bool,
157-
#[bincode(with = "turbo_bincode::serde_json")]
157+
#[bincode(with = "turbo_bincode::serde_self_describing")]
158158
public_runtime_config: FxIndexMap<String, serde_json::Value>,
159-
#[bincode(with = "turbo_bincode::serde_json")]
159+
#[bincode(with = "turbo_bincode::serde_self_describing")]
160160
server_runtime_config: FxIndexMap<String, serde_json::Value>,
161161
static_page_generation_timeout: f64,
162162
target: Option<String>,
@@ -686,9 +686,9 @@ pub enum RemotePatternProtocol {
686686
)]
687687
#[serde(rename_all = "camelCase")]
688688
pub struct TurbopackConfig {
689-
#[bincode(with = "turbo_bincode::serde_json")]
689+
#[bincode(with = "turbo_bincode::serde_self_describing")]
690690
pub rules: Option<FxIndexMap<RcStr, RuleConfigCollection>>,
691-
#[bincode(with = "turbo_bincode::serde_json")]
691+
#[bincode(with = "turbo_bincode::serde_self_describing")]
692692
pub resolve_alias: Option<FxIndexMap<RcStr, JsonValue>>,
693693
pub resolve_extensions: Option<Vec<RcStr>>,
694694
pub debug_ids: Option<bool>,
@@ -955,7 +955,7 @@ pub struct ExperimentalConfig {
955955
/// @see [api reference](https://nextjs.org/docs/app/api-reference/next-config-js/mdxRs)
956956
mdx_rs: Option<MdxRsOptions>,
957957
strict_next_head: Option<bool>,
958-
#[bincode(with = "turbo_bincode::serde_json")]
958+
#[bincode(with = "turbo_bincode::serde_self_describing")]
959959
swc_plugins: Option<Vec<(RcStr, serde_json::Value)>>,
960960
external_middleware_rewrites_resolve: Option<bool>,
961961
scroll_restoration: Option<bool>,
@@ -964,7 +964,7 @@ pub struct ExperimentalConfig {
964964
middleware_prefetch: Option<MiddlewarePrefetchType>,
965965
/// optimizeCss can be boolean or critters' option object
966966
/// Use Record<string, unknown> as critters doesn't export its Option type ([link](https://github.com/GoogleChromeLabs/critters/blob/a590c05f9197b656d2aeaae9369df2483c26b072/packages/critters/src/index.d.ts))
967-
#[bincode(with = "turbo_bincode::serde_json")]
967+
#[bincode(with = "turbo_bincode::serde_self_describing")]
968968
optimize_css: Option<serde_json::Value>,
969969
next_script_workers: Option<bool>,
970970
web_vitals_attribution: Option<Vec<RcStr>>,
@@ -982,15 +982,15 @@ pub struct ExperimentalConfig {
982982
adjust_font_fallbacks_with_size_adjust: Option<bool>,
983983
after: Option<bool>,
984984
app_document_preloading: Option<bool>,
985-
#[bincode(with = "turbo_bincode::serde_json")]
985+
#[bincode(with = "turbo_bincode::serde_self_describing")]
986986
cache_life: Option<FxIndexMap<String, CacheLifeProfile>>,
987987
case_sensitive_routes: Option<bool>,
988988
cpus: Option<f64>,
989989
cra_compat: Option<bool>,
990990
disable_optimized_loading: Option<bool>,
991991
disable_postcss_preset_env: Option<bool>,
992992
esm_externals: Option<EsmExternals>,
993-
#[bincode(with = "turbo_bincode::serde_json")]
993+
#[bincode(with = "turbo_bincode::serde_self_describing")]
994994
extension_alias: Option<serde_json::Value>,
995995
external_dir: Option<bool>,
996996
/// If set to `false`, webpack won't fall back to polyfill Node.js modules
@@ -1005,7 +1005,7 @@ pub struct ExperimentalConfig {
10051005
instrumentation_hook: Option<bool>,
10061006
client_trace_metadata: Option<Vec<String>>,
10071007
large_page_data_bytes: Option<f64>,
1008-
#[bincode(with = "turbo_bincode::serde_json")]
1008+
#[bincode(with = "turbo_bincode::serde_self_describing")]
10091009
logging: Option<serde_json::Value>,
10101010
memory_based_workers_count: Option<bool>,
10111011
/// Optimize React APIs for server builds.
@@ -1024,7 +1024,7 @@ pub struct ExperimentalConfig {
10241024
/// @internal Used by the Next.js internals only.
10251025
trust_host_header: Option<bool>,
10261026

1027-
#[bincode(with = "turbo_bincode::serde_json")]
1027+
#[bincode(with = "turbo_bincode::serde_self_describing")]
10281028
url_imports: Option<serde_json::Value>,
10291029
/// This option is to enable running the Webpack build in a worker thread
10301030
/// (doesn't apply to Turbopack).
@@ -1395,7 +1395,7 @@ pub struct ResolveExtensions(Option<Vec<RcStr>>);
13951395

13961396
#[turbo_tasks::value(transparent)]
13971397
pub struct SwcPlugins(
1398-
#[bincode(with = "turbo_bincode::serde_json")] Vec<(RcStr, serde_json::Value)>,
1398+
#[bincode(with = "turbo_bincode::serde_self_describing")] Vec<(RcStr, serde_json::Value)>,
13991399
);
14001400

14011401
#[turbo_tasks::value(transparent)]
@@ -1413,7 +1413,7 @@ pub struct OptionServerActions(Option<ServerActions>);
14131413

14141414
#[turbo_tasks::value(transparent)]
14151415
pub struct OptionJsonValue(
1416-
#[bincode(with = "turbo_bincode::serde_json")] pub Option<serde_json::Value>,
1416+
#[bincode(with = "turbo_bincode::serde_self_describing")] pub Option<serde_json::Value>,
14171417
);
14181418

14191419
fn turbopack_config_documentation_link() -> RcStr {
@@ -1466,7 +1466,7 @@ impl Issue for InvalidLoaderRuleRenameAsIssue {
14661466

14671467
#[turbo_tasks::value(shared)]
14681468
struct InvalidLoaderRuleConditionIssue {
1469-
#[bincode(with = "turbo_bincode::serde_json")]
1469+
#[bincode(with = "turbo_bincode::serde_self_describing")]
14701470
condition: ConfigConditionItem,
14711471
config_file_path: FileSystemPath,
14721472
}
@@ -2207,7 +2207,7 @@ impl NextConfig {
22072207
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Encode, Decode)]
22082208
#[serde(rename_all = "camelCase")]
22092209
pub struct JsConfig {
2210-
#[bincode(with = "turbo_bincode::serde_json")]
2210+
#[bincode(with = "turbo_bincode::serde_self_describing")]
22112211
compiler_options: Option<serde_json::Value>,
22122212
}
22132213

turbopack/crates/turbo-bincode/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ indexmap = { workspace = true }
1717
mime = { workspace = true }
1818
ringmap = { workspace = true }
1919
serde = { workspace = true }
20-
serde_json = { workspace = true }
2120
smallvec = { workspace = true }
2221
unty = { workspace = true }

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

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -473,76 +473,6 @@ pub mod mime_option {
473473
}
474474
}
475475

476-
/// Encode/decode as a serialized string encoded using `serde_json`.
477-
///
478-
/// This encodes less efficiently than `#[bincode(with_serde)]` would, but avoids [bincode's known
479-
/// compatibility issues][serde-issues]. Use this for infrequently-serialized types and when you're
480-
/// unsure if the underlying type may trigger a serde compatibility issue.
481-
///
482-
/// In the future this could be replaced with a more efficient serde-compatible self-describing
483-
/// format with a compact binary representation (e.g. pot or MessagePack), but `serde_json` is
484-
/// convenient because it avoids introducing additional dependencies.
485-
///
486-
/// [serde-issues]: https://docs.rs/bincode/latest/bincode/serde/index.html#known-issues
487-
pub mod serde_json {
488-
use super::*;
489-
490-
pub fn encode<E: Encoder, T: serde::Serialize>(
491-
value: &T,
492-
encoder: &mut E,
493-
) -> Result<(), EncodeError> {
494-
let json_str =
495-
::serde_json::to_string(value).map_err(|e| EncodeError::OtherString(e.to_string()))?;
496-
Encode::encode(&json_str, encoder)
497-
}
498-
499-
pub fn decode<Context, D: Decoder<Context = Context>, T: serde::de::DeserializeOwned>(
500-
decoder: &mut D,
501-
) -> Result<T, DecodeError> {
502-
let json_str: String = Decode::decode(decoder)?;
503-
::serde_json::from_str(&json_str).map_err(|e| DecodeError::OtherString(e.to_string()))
504-
}
505-
506-
pub fn borrow_decode<
507-
'de,
508-
Context,
509-
D: BorrowDecoder<'de, Context = Context>,
510-
T: serde::de::Deserialize<'de>,
511-
>(
512-
decoder: &mut D,
513-
) -> Result<T, DecodeError> {
514-
let json_str: &str = BorrowDecode::borrow_decode(decoder)?;
515-
::serde_json::from_str(json_str).map_err(|e| DecodeError::OtherString(e.to_string()))
516-
}
517-
518-
#[cfg(test)]
519-
mod tests {
520-
use ::serde_json::{Value, json};
521-
use bincode::{decode_from_slice, encode_to_vec};
522-
523-
use super::*;
524-
525-
#[test]
526-
fn test_roundtrip() {
527-
let cfg = bincode::config::standard();
528-
529-
#[derive(Encode, Decode)]
530-
struct Wrapper(#[bincode(with = "crate::serde_json")] Value);
531-
532-
let value1 = Wrapper(json!({
533-
"key1": [1, 2, 3],
534-
"key2": [4, 5, 6]
535-
}));
536-
537-
let value2: Wrapper = decode_from_slice(&encode_to_vec(&value1, cfg).unwrap(), cfg)
538-
.unwrap()
539-
.0;
540-
541-
assert_eq!(value1.0, value2.0);
542-
}
543-
}
544-
}
545-
546476
pub mod either {
547477
use ::either::Either;
548478

turbopack/crates/turbo-persistence/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ verbose_log = []
1414

1515
[dependencies]
1616
anyhow = { workspace = true }
17-
either = { workspace = true }
18-
pot = "3.0.0"
17+
bincode = { workspace = true }
1918
byteorder = { workspace = true }
19+
either = { workspace = true }
2020
jiff = "0.2.10"
2121
lzzzz = "1.1.0"
2222
memmap2 = "0.9.5"
@@ -27,6 +27,7 @@ rustc-hash = { workspace = true }
2727
smallvec = { workspace = true }
2828
thread_local = { workspace = true }
2929
tracing = { workspace = true }
30+
turbo-bincode= { workspace = true }
3031
twox-hash = { workspace = true }
3132
zstd = { version = "0.13.2", features = ["zdict_builder"] }
3233

turbopack/crates/turbo-persistence/src/meta_file.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ use std::{
88
};
99

1010
use anyhow::{Context, Result, bail};
11+
use bincode::{Decode, Encode};
1112
use byteorder::{BE, ReadBytesExt};
1213
use either::Either;
1314
use memmap2::{Mmap, MmapOptions};
1415
use quick_cache::sync::GuardResult;
1516
use rustc_hash::FxHasher;
17+
use turbo_bincode::turbo_bincode_decode;
1618

1719
use crate::{
1820
QueryKey,
@@ -31,6 +33,14 @@ impl quick_cache::Weighter<u32, Arc<qfilter::Filter>> for AmqfWeighter {
3133
pub type AmqfCache =
3234
quick_cache::sync::Cache<u32, Arc<qfilter::Filter>, AmqfWeighter, BuildHasherDefault<FxHasher>>;
3335

36+
/// A wrapper around [`qfilter::Filter`] that implements [`Encode`] and [`Decode`].
37+
#[derive(Encode, Decode)]
38+
pub struct AmqfBincodeWrapper(
39+
// this annotation can be replaced with `#[bincode(serde)]` once
40+
// <https://github.com/arthurprs/qfilter/issues/13> is resolved
41+
#[bincode(with = "turbo_bincode::serde_self_describing")] pub qfilter::Filter,
42+
);
43+
3444
pub struct MetaEntry {
3545
/// The metadata for the static sorted file.
3646
sst_data: StaticSortedFileMetaData,
@@ -76,13 +86,15 @@ impl MetaEntry {
7686

7787
pub fn deserialize_amqf(&self, meta: &MetaFile) -> Result<qfilter::Filter> {
7888
let amqf = self.raw_amqf(meta.amqf_data());
79-
pot::from_slice(amqf).with_context(|| {
80-
format!(
81-
"Failed to deserialize AMQF from {:08}.meta for {:08}.sst",
82-
meta.sequence_number,
83-
self.sequence_number()
84-
)
85-
})
89+
Ok(turbo_bincode_decode::<AmqfBincodeWrapper>(amqf)
90+
.with_context(|| {
91+
format!(
92+
"Failed to deserialize AMQF from {:08}.meta for {:08}.sst",
93+
meta.sequence_number,
94+
self.sequence_number()
95+
)
96+
})?
97+
.0)
8698
}
8799

88100
pub fn amqf(

turbopack/crates/turbo-persistence/src/static_sorted_file_builder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use std::{
88

99
use anyhow::{Context, Result};
1010
use byteorder::{BE, ByteOrder, WriteBytesExt};
11+
use turbo_bincode::{TurboBincodeBuffer, turbo_bincode_encode};
1112

1213
use crate::{
1314
compression::compress_into_buffer,
15+
meta_file::AmqfBincodeWrapper,
1416
static_sorted_file::{
1517
BLOCK_TYPE_INDEX, BLOCK_TYPE_KEY, KEY_BLOCK_ENTRY_TYPE_BLOB, KEY_BLOCK_ENTRY_TYPE_DELETED,
1618
KEY_BLOCK_ENTRY_TYPE_MEDIUM, KEY_BLOCK_ENTRY_TYPE_SMALL,
@@ -137,7 +139,7 @@ pub fn write_static_stored_file<E: Entry>(
137139
let meta = StaticSortedFileBuilderMeta {
138140
min_hash,
139141
max_hash,
140-
amqf: Cow::Owned(amqf),
142+
amqf: Cow::Owned(amqf.into_vec()),
141143
key_compression_dictionary_length: key_dict.len().try_into().unwrap(),
142144
block_count,
143145
size: file.stream_position()?,
@@ -390,7 +392,7 @@ fn write_key_blocks_and_compute_amqf(
390392
key_compression_dictionary: &[u8],
391393
writer: &mut BlockWriter<'_>,
392394
buffer: &mut Vec<u8>,
393-
) -> Result<Vec<u8>> {
395+
) -> Result<TurboBincodeBuffer> {
394396
let mut filter = qfilter::Filter::new(entries.len() as u64, AMQF_FALSE_POSITIVE_RATE)
395397
// This won't fail as we limit the number of entries per SST file
396398
.expect("Filter can't be constructed");
@@ -497,7 +499,7 @@ fn write_key_blocks_and_compute_amqf(
497499
writer.write_index_block(buffer, key_compression_dictionary)?;
498500
buffer.clear();
499501

500-
Ok(pot::to_vec(&filter).expect("AMQF serialization failed"))
502+
Ok(turbo_bincode_encode(&AmqfBincodeWrapper(filter)).expect("AMQF serialization failed"))
501503
}
502504

503505
/// Builder for a single key block

turbopack/crates/turbo-tasks/src/primitives.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl ManualEncodeWrapper for JsonValueEncodeWrapper<'_> {
5656

5757
impl Encode for JsonValueEncodeWrapper<'_> {
5858
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
59-
turbo_bincode::serde_json::encode(self.0, encoder)
59+
turbo_bincode::serde_self_describing::encode(self.0, encoder)
6060
}
6161
}
6262

@@ -72,6 +72,6 @@ impl ManualDecodeWrapper for JsonValueDecodeWrapper {
7272

7373
impl<Context> Decode<Context> for JsonValueDecodeWrapper {
7474
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
75-
Ok(Self(turbo_bincode::serde_json::decode(decoder)?))
75+
Ok(Self(turbo_bincode::serde_self_describing::decode(decoder)?))
7676
}
7777
}

0 commit comments

Comments
 (0)