Skip to content

Commit

Permalink
Auto merge of rust-lang#119302 - Mark-Simulacrum:relative-spans, r=<try>
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bors committed Dec 25, 2023
2 parents 71696e5 + 9aec3df commit a32f9c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 8 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,17 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {

impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span {
let start = decoder.position();
let mode = SpanEncodingMode::decode(decoder);
let data = match mode {
SpanEncodingMode::Direct => SpanData::decode(decoder),
SpanEncodingMode::Shorthand(position) => decoder.with_position(position, |decoder| {
let mode = SpanEncodingMode::decode(decoder);
debug_assert!(matches!(mode, SpanEncodingMode::Direct));
SpanData::decode(decoder)
}),
SpanEncodingMode::Shorthand(offset) => {
decoder.with_position(start - offset, |decoder| {
let mode = SpanEncodingMode::decode(decoder);
debug_assert!(matches!(mode, SpanEncodingMode::Direct));
SpanData::decode(decoder)
})
}
};
Span::new(data.lo, data.hi, data.ctxt, data.parent)
}
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId {
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
match s.span_shorthands.entry(*self) {
Entry::Occupied(o) => SpanEncodingMode::Shorthand(*o.get()).encode(s),
Entry::Occupied(o) => {
// Encode the offset rather than the absolute position. The underlying encoding uses
// LEB128, and offsets are always smaller than the absolute position.
let offset = s.opaque.position() - *o.get();
SpanEncodingMode::Shorthand(offset).encode(s)
}
Entry::Vacant(v) => {
let position = s.opaque.position();
v.insert(position);
Expand Down

0 comments on commit a32f9c5

Please sign in to comment.