Skip to content

Commit 955aebf

Browse files
committed
Don't serialize ExpnData for foreign crates
When we encode an ExpnId into the crate metadata, we write out the CrateNum of the crate that 'owns' the corresponding `ExpnData`, which is later used to decode the `ExpnData` from its owning crate. However, we current serialize the `ExpnData` for all `ExpnIds` that we serialize, even if the `ExpnData` was already serialized into a foreign crate. This commit skips encoding this kind of `ExpnData`, which should hopefully speed up metadata encoding and reduce the total metadata size.
1 parent 40857b9 commit 955aebf

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/librustc_span/hygiene.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1170,13 +1170,27 @@ pub fn raw_encode_expn_id<E: Encoder>(
11701170
mode: ExpnDataEncodeMode,
11711171
e: &mut E,
11721172
) -> Result<(), E::Error> {
1173-
if !context.serialized_expns.lock().contains(&expn) {
1174-
context.latest_expns.lock().insert(expn);
1175-
}
1173+
// Record the fact that we need to serialize the corresponding
1174+
// `ExpnData`
1175+
let needs_data = || {
1176+
if !context.serialized_expns.lock().contains(&expn) {
1177+
context.latest_expns.lock().insert(expn);
1178+
}
1179+
};
1180+
11761181
match mode {
1177-
ExpnDataEncodeMode::IncrComp => expn.0.encode(e),
1182+
ExpnDataEncodeMode::IncrComp => {
1183+
// Always serialize the `ExpnData` in incr comp mode
1184+
needs_data();
1185+
expn.0.encode(e)
1186+
}
11781187
ExpnDataEncodeMode::Metadata => {
11791188
let data = expn.expn_data();
1189+
// We only need to serialize the ExpnData
1190+
// if it comes from this crate.
1191+
if data.krate == LOCAL_CRATE {
1192+
needs_data();
1193+
}
11801194
data.orig_id.expect("Missing orig_id").encode(e)?;
11811195
data.krate.encode(e)
11821196
}

0 commit comments

Comments
 (0)