Skip to content

Commit a32f9c5

Browse files
committed
Auto merge of rust-lang#119302 - Mark-Simulacrum:relative-spans, r=<try>
Encode spans with relative offsets The relative offset is always smaller than the absolute offset, and with the LEB128 encoding, this ends up cutting the overall metadata size considerably (~1.5 megabytes on libcore).
2 parents 71696e5 + 9aec3df commit a32f9c5

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,17 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
507507

508508
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
509509
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span {
510+
let start = decoder.position();
510511
let mode = SpanEncodingMode::decode(decoder);
511512
let data = match mode {
512513
SpanEncodingMode::Direct => SpanData::decode(decoder),
513-
SpanEncodingMode::Shorthand(position) => decoder.with_position(position, |decoder| {
514-
let mode = SpanEncodingMode::decode(decoder);
515-
debug_assert!(matches!(mode, SpanEncodingMode::Direct));
516-
SpanData::decode(decoder)
517-
}),
514+
SpanEncodingMode::Shorthand(offset) => {
515+
decoder.with_position(start - offset, |decoder| {
516+
let mode = SpanEncodingMode::decode(decoder);
517+
debug_assert!(matches!(mode, SpanEncodingMode::Direct));
518+
SpanData::decode(decoder)
519+
})
520+
}
518521
};
519522
Span::new(data.lo, data.hi, data.ctxt, data.parent)
520523
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
169169
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
170170
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
171171
match s.span_shorthands.entry(*self) {
172-
Entry::Occupied(o) => SpanEncodingMode::Shorthand(*o.get()).encode(s),
172+
Entry::Occupied(o) => {
173+
// Encode the offset rather than the absolute position. The underlying encoding uses
174+
// LEB128, and offsets are always smaller than the absolute position.
175+
let offset = s.opaque.position() - *o.get();
176+
SpanEncodingMode::Shorthand(offset).encode(s)
177+
}
173178
Entry::Vacant(v) => {
174179
let position = s.opaque.position();
175180
v.insert(position);

0 commit comments

Comments
 (0)