Skip to content

Commit fe2cfd4

Browse files
committed
Auto merge of #119367 - Mark-Simulacrum:relative-spans, r=wesleywiser
Shrink span encoding further Spans are now stored in a more compact form which cuts down on at least 1 byte per span (indirect/direct encoding) and at most 3 bytes per span (indirect/direct encoding, context byte, length byte). As a result, libcore metadata shrinks by 1.5MB. I'm not a huge fan of the fairly manual encoding/decoding from bits implemented here. Something like Tokio's pack abstraction (https://github.com/tokio-rs/tokio/blob/master/tokio/src/util/bit.rs) might be desirable to cut down on some of the shifting etc. We might also say that this isn't worth doing :) I took a look at copying the span encoding we use in memory (described [here](https://github.com/rust-lang/rust/blob/master/compiler/rustc_span/src/span_encoding.rs)). I think the format there makes a lot more sense for in-memory storage where prioritizing a fixed length (i.e., 4 or 8 bytes) is much more important. In metadata, it's much easier for us to have variable-length values, so there's less of a cliff if we don't quite fit. The bit packing scheme there would need changes to fit the varint scheme since it has a lot of all-1s patterns as the "relative offset" form.
2 parents 8d76d07 + 09e619d commit fe2cfd4

File tree

3 files changed

+130
-47
lines changed

3 files changed

+130
-47
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -508,39 +508,37 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
508508
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span {
509509
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span {
510510
let start = decoder.position();
511-
let mode = SpanEncodingMode::decode(decoder);
512-
let data = match mode {
513-
SpanEncodingMode::Direct => SpanData::decode(decoder),
514-
SpanEncodingMode::RelativeOffset(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-
}
521-
SpanEncodingMode::AbsoluteOffset(addr) => decoder.with_position(addr, |decoder| {
522-
let mode = SpanEncodingMode::decode(decoder);
523-
debug_assert!(matches!(mode, SpanEncodingMode::Direct));
524-
SpanData::decode(decoder)
525-
}),
511+
let tag = SpanTag(decoder.peek_byte());
512+
let data = if tag.kind() == SpanKind::Indirect {
513+
// Skip past the tag we just peek'd.
514+
decoder.read_u8();
515+
let offset_or_position = decoder.read_usize();
516+
let position = if tag.is_relative_offset() {
517+
start - offset_or_position
518+
} else {
519+
offset_or_position
520+
};
521+
decoder.with_position(position, SpanData::decode)
522+
} else {
523+
SpanData::decode(decoder)
526524
};
527525
Span::new(data.lo, data.hi, data.ctxt, data.parent)
528526
}
529527
}
530528

531529
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
532530
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SpanData {
533-
let ctxt = SyntaxContext::decode(decoder);
534-
let tag = u8::decode(decoder);
531+
let tag = SpanTag::decode(decoder);
532+
let ctxt = tag.context().unwrap_or_else(|| SyntaxContext::decode(decoder));
535533

536-
if tag == TAG_PARTIAL_SPAN {
534+
if tag.kind() == SpanKind::Partial {
537535
return DUMMY_SP.with_ctxt(ctxt).data();
538536
}
539537

540-
debug_assert!(tag == TAG_VALID_SPAN_LOCAL || tag == TAG_VALID_SPAN_FOREIGN);
538+
debug_assert!(tag.kind() == SpanKind::Local || tag.kind() == SpanKind::Foreign);
541539

542540
let lo = BytePos::decode(decoder);
543-
let len = BytePos::decode(decoder);
541+
let len = tag.length().unwrap_or_else(|| BytePos::decode(decoder));
544542
let hi = lo + len;
545543

546544
let Some(sess) = decoder.sess else {
@@ -581,7 +579,7 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SpanData {
581579
// treat the 'local' and 'foreign' cases almost identically during deserialization:
582580
// we can call `imported_source_file` for the proper crate, and binary search
583581
// through the returned slice using our span.
584-
let source_file = if tag == TAG_VALID_SPAN_LOCAL {
582+
let source_file = if tag.kind() == SpanKind::Local {
585583
decoder.cdata().imported_source_file(metadata_index, sess)
586584
} else {
587585
// When we encode a proc-macro crate, all `Span`s should be encoded

compiler/rustc_metadata/src/rmeta/encoder.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,17 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
177177
// previously saved offset must be smaller than the current position.
178178
let offset = s.opaque.position() - last_location;
179179
if offset < last_location {
180-
SpanEncodingMode::RelativeOffset(offset).encode(s)
180+
SpanTag::indirect(true).encode(s);
181+
offset.encode(s);
181182
} else {
182-
SpanEncodingMode::AbsoluteOffset(last_location).encode(s)
183+
SpanTag::indirect(false).encode(s);
184+
last_location.encode(s);
183185
}
184186
}
185187
Entry::Vacant(v) => {
186188
let position = s.opaque.position();
187189
v.insert(position);
188-
SpanEncodingMode::Direct.encode(s);
190+
// Data is encoded with a SpanTag prefix (see below).
189191
self.data().encode(s);
190192
}
191193
}
@@ -225,14 +227,15 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
225227
// IMPORTANT: If this is ever changed, be sure to update
226228
// `rustc_span::hygiene::raw_encode_expn_id` to handle
227229
// encoding `ExpnData` for proc-macro crates.
228-
if s.is_proc_macro {
229-
SyntaxContext::root().encode(s);
230-
} else {
231-
self.ctxt.encode(s);
232-
}
230+
let ctxt = if s.is_proc_macro { SyntaxContext::root() } else { self.ctxt };
233231

234232
if self.is_dummy() {
235-
return TAG_PARTIAL_SPAN.encode(s);
233+
let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
234+
tag.encode(s);
235+
if tag.context().is_none() {
236+
ctxt.encode(s);
237+
}
238+
return;
236239
}
237240

238241
// The Span infrastructure should make sure that this invariant holds:
@@ -250,7 +253,12 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
250253
if !source_file.contains(self.hi) {
251254
// Unfortunately, macro expansion still sometimes generates Spans
252255
// that malformed in this way.
253-
return TAG_PARTIAL_SPAN.encode(s);
256+
let tag = SpanTag::new(SpanKind::Partial, ctxt, 0);
257+
tag.encode(s);
258+
if tag.context().is_none() {
259+
ctxt.encode(s);
260+
}
261+
return;
254262
}
255263

256264
// There are two possible cases here:
@@ -269,7 +277,7 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
269277
// if we're a proc-macro crate.
270278
// This allows us to avoid loading the dependencies of proc-macro crates: all of
271279
// the information we need to decode `Span`s is stored in the proc-macro crate.
272-
let (tag, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
280+
let (kind, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
273281
// To simplify deserialization, we 'rebase' this span onto the crate it originally came
274282
// from (the crate that 'owns' the file it references. These rebased 'lo' and 'hi'
275283
// values are relative to the source map information for the 'foreign' crate whose
@@ -287,7 +295,7 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
287295
}
288296
};
289297

290-
(TAG_VALID_SPAN_FOREIGN, metadata_index)
298+
(SpanKind::Foreign, metadata_index)
291299
} else {
292300
// Record the fact that we need to encode the data for this `SourceFile`
293301
let source_files =
@@ -296,7 +304,7 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
296304
let metadata_index: u32 =
297305
metadata_index.try_into().expect("cannot export more than U32_MAX files");
298306

299-
(TAG_VALID_SPAN_LOCAL, metadata_index)
307+
(SpanKind::Local, metadata_index)
300308
};
301309

302310
// Encode the start position relative to the file start, so we profit more from the
@@ -307,14 +315,20 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SpanData {
307315
// from the variable-length integer encoding that we use.
308316
let len = self.hi - self.lo;
309317

318+
let tag = SpanTag::new(kind, ctxt, len.0 as usize);
310319
tag.encode(s);
320+
if tag.context().is_none() {
321+
ctxt.encode(s);
322+
}
311323
lo.encode(s);
312-
len.encode(s);
324+
if tag.length().is_none() {
325+
len.encode(s);
326+
}
313327

314328
// Encode the index of the `SourceFile` for the span, in order to make decoding faster.
315329
metadata_index.encode(s);
316330

317-
if tag == TAG_VALID_SPAN_FOREIGN {
331+
if kind == SpanKind::Foreign {
318332
// This needs to be two lines to avoid holding the `s.source_file_cache`
319333
// while calling `cnum.encode(s)`
320334
let cnum = s.source_file_cache.0.cnum;

compiler/rustc_metadata/src/rmeta/mod.rs

+82-11
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ const METADATA_VERSION: u8 = 9;
6666
/// unsigned integer, and further followed by the rustc version string.
6767
pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
6868

69-
#[derive(Encodable, Decodable)]
70-
enum SpanEncodingMode {
71-
RelativeOffset(usize),
72-
AbsoluteOffset(usize),
73-
Direct,
74-
}
75-
7669
/// A value of type T referred to by its absolute position
7770
/// in the metadata, and which can be decoded lazily.
7871
///
@@ -488,10 +481,88 @@ bitflags::bitflags! {
488481
}
489482
}
490483

491-
// Tags used for encoding Spans:
492-
const TAG_VALID_SPAN_LOCAL: u8 = 0;
493-
const TAG_VALID_SPAN_FOREIGN: u8 = 1;
494-
const TAG_PARTIAL_SPAN: u8 = 2;
484+
/// A span tag byte encodes a bunch of data, so that we can cut out a few extra bytes from span
485+
/// encodings (which are very common, for example, libcore has ~650,000 unique spans and over 1.1
486+
/// million references to prior-written spans).
487+
///
488+
/// The byte format is split into several parts:
489+
///
490+
/// [ a a a a a c d d ]
491+
///
492+
/// `a` bits represent the span length. We have 5 bits, so we can store lengths up to 30 inline, with
493+
/// an all-1s pattern representing that the length is stored separately.
494+
///
495+
/// `c` represents whether the span context is zero (and then it is not stored as a separate varint)
496+
/// for direct span encodings, and whether the offset is absolute or relative otherwise (zero for
497+
/// absolute).
498+
///
499+
/// d bits represent the kind of span we are storing (local, foreign, partial, indirect).
500+
#[derive(Encodable, Decodable, Copy, Clone)]
501+
struct SpanTag(u8);
502+
503+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
504+
enum SpanKind {
505+
Local = 0b00,
506+
Foreign = 0b01,
507+
Partial = 0b10,
508+
// Indicates the actual span contents are elsewhere.
509+
// If this is the kind, then the span context bit represents whether it is a relative or
510+
// absolute offset.
511+
Indirect = 0b11,
512+
}
513+
514+
impl SpanTag {
515+
fn new(kind: SpanKind, context: rustc_span::SyntaxContext, length: usize) -> SpanTag {
516+
let mut data = 0u8;
517+
data |= kind as u8;
518+
if context.is_root() {
519+
data |= 0b100;
520+
}
521+
let all_1s_len = (0xffu8 << 3) >> 3;
522+
// strictly less than - all 1s pattern is a sentinel for storage being out of band.
523+
if length < all_1s_len as usize {
524+
data |= (length as u8) << 3;
525+
} else {
526+
data |= all_1s_len << 3;
527+
}
528+
529+
SpanTag(data)
530+
}
531+
532+
fn indirect(relative: bool) -> SpanTag {
533+
let mut tag = SpanTag(SpanKind::Indirect as u8);
534+
if relative {
535+
tag.0 |= 0b100;
536+
}
537+
tag
538+
}
539+
540+
fn kind(self) -> SpanKind {
541+
let masked = self.0 & 0b11;
542+
match masked {
543+
0b00 => SpanKind::Local,
544+
0b01 => SpanKind::Foreign,
545+
0b10 => SpanKind::Partial,
546+
0b11 => SpanKind::Indirect,
547+
_ => unreachable!(),
548+
}
549+
}
550+
551+
fn is_relative_offset(self) -> bool {
552+
debug_assert_eq!(self.kind(), SpanKind::Indirect);
553+
self.0 & 0b100 != 0
554+
}
555+
556+
fn context(self) -> Option<rustc_span::SyntaxContext> {
557+
if self.0 & 0b100 != 0 { Some(rustc_span::SyntaxContext::root()) } else { None }
558+
}
559+
560+
fn length(self) -> Option<rustc_span::BytePos> {
561+
let all_1s_len = (0xffu8 << 3) >> 3;
562+
let len = self.0 >> 3;
563+
if len != all_1s_len { Some(rustc_span::BytePos(u32::from(len))) } else { None }
564+
}
565+
}
495566

496567
// Tags for encoding Symbol's
497568
const SYMBOL_STR: u8 = 0;

0 commit comments

Comments
 (0)