Skip to content

Commit 94554c0

Browse files
authored
Turbopack: Use RawValue for mappings inside SourceMapJson (#84208)
Normally `serde_json` has to handle JSON string escaping, which has some overhead. With `Box<RawValue>` it can just memcpy the raw string instead. More ideally we'd use `&RawValue`, but we'd need a `str` instead of a `Rope` to do that. https://docs.rs/serde_json/latest/serde_json/value/struct.RawValue.html This works because we don't care about the value of `mappings` here, we just want to make sure it's passed through when the struct is re-serialized. We use `RawValue` a lot in the `swc-sourcemap` crate: https://github.com/swc-project/swc-sourcemap/blob/main/src/lazy/mod.rs
1 parent 446a671 commit 94554c0

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

turbopack/crates/turbopack-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ rustc-hash = { workspace = true }
3030
regex = { workspace = true }
3131
serde = { workspace = true, features = ["rc"] }
3232
serde_bytes = { workspace = true }
33-
serde_json = { workspace = true, features = ["preserve_order"] }
33+
serde_json = { workspace = true, features = ["preserve_order", "raw_value"] }
3434
smallvec = { workspace = true }
3535
swc_sourcemap = { workspace = true }
3636
swc_core = { workspace = true, features = ["ecma_preset_env", "common"] }

turbopack/crates/turbopack-core/src/source_map/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ struct SourceMapJson {
6060
sources_content: Option<Vec<Option<String>>>,
6161
#[serde(skip_serializing_if = "Option::is_none")]
6262
names: Option<Vec<String>>,
63-
mappings: String,
63+
// We just need to hold onto `mappings` for serialization/deserialization, so there's no point
64+
// in decoding/encoding the string. Store it as a `RawValue`. Ideally this would be a reference
65+
// to the RawValue, but we deserialize using `from_reader`, which does not support that.
66+
mappings: Box<serde_json::value::RawValue>,
6467
#[serde(skip_serializing_if = "Option::is_none")]
6568
ignore_list: Option<Vec<u32>>,
6669

0 commit comments

Comments
 (0)