Unverified Commit 2a40e648 authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Avoid `serde_json::RawValue` conversions in a few places (#97713)

[`RawValue::from_string`](https://docs.rs/serde_json/latest/serde_json/value/struct.RawValue.html#method.from_string)
attempts to deserialize values to ensure they're valid JSON.

The original version of this PR used
[`RawValue::from_string_unchecked`](https://docs.rs/serde_json/latest/serde_json/value/struct.RawValue.html#method.from_string_unchecked),
which introduced some `unsafe` usage to avoid the parse.

Luke's comments revealed some better approaches. For
`pages_chunk_group_bootstrap_params`, we're also able to avoid a string
copy by using `RcJsonRawValue` and serializing a `&RawValue` instead of
a `Box<RawValue>`.
parent ebc1c6a5
......@@ -6494,9 +6494,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.149"
version = "1.0.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
dependencies = [
"indexmap 2.14.0",
"itoa",
......
......@@ -338,7 +338,7 @@ rstest = "0.16.0"
rustc-hash = "2.1.1"
scattered-collect = "0.10.0"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
serde_json = "1.0.151"
serde_path_to_error = "0.1.20"
serde_qs = "1.1.1"
serde_with = "3.18.0"
......
......@@ -53,6 +53,7 @@ use turbopack_core::{
context::AssetContext,
file_source::FileSource,
ident::{AssetIdent, Layer},
json::RcJsonRawValue,
module::Module,
module_graph::{
GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
......@@ -1384,7 +1385,7 @@ impl PageEndpoint {
client_chunks: ResolvedVc<OutputAssets>,
// Inline bootstrap params, present when the bootstrap is inlined rather
// than emitted as a per-route chunk.
chunk_group_bootstrap_params: Option<RcStr>,
chunk_group_bootstrap_params: Option<RcJsonRawValue>,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let node_root = self.pages_project.project().node_root().owned().await?;
let client_relative_path = self
......
......@@ -12,6 +12,7 @@ use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIt
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
json::RcJsonRawValue,
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
};
......@@ -42,7 +43,7 @@ pub struct BuildManifest {
/// Per-page inline chunk group bootstrap params, as JSON. Empty when the
/// bootstrap is emitted as a per-route chunk instead (e.g. dev).
#[bincode(with = "turbo_bincode::indexmap")]
pub pages_chunk_group_bootstrap_params: FxIndexMap<RcStr, RcStr>,
pub pages_chunk_group_bootstrap_params: FxIndexMap<RcStr, RcJsonRawValue>,
/// The `globalThis[...]` chunk-loading global the runtime drains.
pub chunk_loading_global: RcStr,
}
......@@ -97,7 +98,7 @@ impl Asset for BuildManifest {
#[derive(Serialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SerializedBuildManifest {
pub struct SerializedBuildManifest<'a> {
pub dev_files: Vec<RcStr>,
pub amp_dev_files: Vec<RcStr>,
pub polyfill_files: Vec<RcStr>,
......@@ -107,7 +108,7 @@ impl Asset for BuildManifest {
pub amp_first_pages: Vec<RcStr>,
pub root_main_files_tree: FxIndexMap<RcStr, Vec<RcStr>>,
// The values are already JSON; store them as `RawValue` so they are emitted verbatim.
pub pages_chunk_group_bootstrap_params: FxIndexMap<RcStr, Box<RawValue>>,
pub pages_chunk_group_bootstrap_params: FxIndexMap<RcStr, &'a RawValue>,
pub chunk_loading_global: RcStr,
}
......@@ -206,8 +207,8 @@ impl Asset for BuildManifest {
pages_chunk_group_bootstrap_params: self
.pages_chunk_group_bootstrap_params
.iter()
.map(|(k, v)| Ok((k.clone(), RawValue::from_string(v.to_string())?)))
.collect::<Result<FxIndexMap<_, _>>>()?,
.map(|(k, v)| (k.clone(), &**v))
.collect(),
chunk_loading_global: self.chunk_loading_global.clone(),
..Default::default()
};
......
......@@ -4,7 +4,7 @@ use anyhow::Result;
use either::Either;
use indoc::writedoc;
use serde::Serialize;
use turbo_rcstr::{RcStr, rcstr};
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
......@@ -15,6 +15,7 @@ use turbopack_core::{
},
code_builder::{Code, CodeBuilder},
ident::AssetIdent,
json::RcJsonRawValue,
module::Module,
module_graph::ModuleGraph,
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
......@@ -83,7 +84,7 @@ impl EcmascriptBrowserEvaluateChunk {
/// The emitted evaluate-chunk file ([`Self::code`]) reuses these same params,
/// wrapping them as `push([selfPath, params])`. Next.js inlines them in production.
#[turbo_tasks::function]
pub(crate) async fn chunk_group_bootstrap_params(self: Vc<Self>) -> Result<Vc<RcStr>> {
pub(crate) async fn chunk_group_bootstrap_params(self: Vc<Self>) -> Result<Vc<RcJsonRawValue>> {
let this = self.await?;
let other_chunks_data = self.chunks_data().await?;
......@@ -124,7 +125,7 @@ impl EcmascriptBrowserEvaluateChunk {
runtime_module_ids,
};
Ok(Vc::cell(serde_json::to_string(&params)?.into()))
Ok(RcJsonRawValue::from_serialize(&params)?.cell())
}
#[turbo_tasks::function]
......@@ -174,7 +175,7 @@ impl EcmascriptBrowserEvaluateChunk {
"#,
chunk_loading_global = StringifyJs(&chunk_loading_global),
script_or_path = script_or_path,
params = &**params,
params = params.get(),
}?;
// When the runtime is not shared across routes, inline the full browser runtime into this
......
......@@ -16,6 +16,7 @@ use crate::{
},
environment::{ChunkLoading, Environment},
ident::AssetIdent,
json::RcJsonRawValue,
module::Module,
module_graph::{
ModuleGraph, binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroup,
......@@ -119,7 +120,7 @@ pub struct ChunkGroupResult {
pub referenced_assets: ResolvedVc<OutputAssets>,
pub references: ResolvedVc<OutputAssetsReferences>,
pub availability_info: AvailabilityInfo,
pub chunk_group_bootstrap_params: Option<RcStr>,
pub chunk_group_bootstrap_params: Option<RcJsonRawValue>,
}
impl ChunkGroupResult {
......
use std::{
hash::{Hash, Hasher},
ops::Deref,
sync::Arc,
};
use bincode::{
Decode, Encode,
de::Decoder,
enc::Encoder,
error::{DecodeError, EncodeError},
impl_borrow_decode,
};
use serde::Serialize;
use serde_json::value::RawValue;
use turbo_tasks::TaskInput;
/// A reference-counted [`RawValue`]: a string that is guaranteed to contain valid JSON.
///
/// This lets JSON produced in one task be passed through other tasks and re-emitted verbatim
/// without re-validating it.
///
/// Values restored from the persistent cache are trusted to be valid JSON, like all other cached
/// data.
#[turbo_tasks::value(shared, serialization = "custom", eq = "manual")]
#[derive(Clone, Debug)]
pub struct RcJsonRawValue(#[turbo_tasks(debug_ignore, unsafe_ignore)] Arc<RawValue>);
impl RcJsonRawValue {
pub fn from_serialize<T: Serialize + ?Sized>(value: &T) -> serde_json::Result<Self> {
Ok(Self(serde_json::value::to_raw_value(value)?.into()))
}
}
impl Deref for RcJsonRawValue {
type Target = RawValue;
fn deref(&self) -> &RawValue {
&self.0
}
}
impl PartialEq for RcJsonRawValue {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0) || self.0.get() == other.0.get()
}
}
impl Eq for RcJsonRawValue {}
impl Hash for RcJsonRawValue {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get().hash(state);
}
}
impl TaskInput for RcJsonRawValue {
fn is_transient(&self) -> bool {
false
}
}
impl Encode for RcJsonRawValue {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.0.get().encode(encoder)
}
}
impl<Context> Decode<Context> for RcJsonRawValue {
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
let json = String::decode(decoder)?;
// SAFETY: This was encoded from an `RcJsonRawValue`, which always holds valid JSON. Like
// all other persistent cache data, the encoded bytes are trusted.
Ok(Self(
unsafe { RawValue::from_string_unchecked(json) }.into(),
))
}
}
impl_borrow_decode!(RcJsonRawValue);
......@@ -23,6 +23,7 @@ pub mod generated_code_source;
pub mod ident;
pub mod introspect;
pub mod issue;
pub mod json;
pub mod loader;
pub mod module;
pub mod module_graph;
......
......@@ -218,11 +218,8 @@ pub async fn resolve_source_map_sources(
}
fn unencoded_str_to_raw_value(unencoded: &str) -> Box<RawValue> {
RawValue::from_string(
serde_json::to_string(unencoded)
.expect("serialization of a utf-8 string should always succeed"),
)
.expect("serde_json::to_string should produce valid JSON")
serde_json::value::to_raw_value(unencoded)
.expect("serialization of a utf-8 string should always succeed")
}
fn uri_encode_path(path: &str) -> String {
......
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