1
Fork 0

Span tweaks.

`Span` has undergone some changes over the years (addition of an optional
parent, and possible inlining of the context in interned spans) but the
comments and identifiers used haven't kept up. As a result, I find it
harder to understand than I should.

This commit reworks the comments, renames some identifiers, and
restructures the code slightly, all to make things clearer. I now feel
like I understand this code again.
This commit is contained in:
Nicholas Nethercote 2023-09-06 09:07:58 +10:00
parent 69ec43001a
commit e525e4f10a

View file

@ -1,9 +1,3 @@
// Spans are encoded using 1-bit tag and 2 different encoding formats (one for each tag value).
// One format is used for keeping span data inline,
// another contains index into an out-of-line span interner.
// The encoding format for inline spans were obtained by optimizing over crates in rustc/libstd.
// See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28
use crate::def_id::{DefIndex, LocalDefId}; use crate::def_id::{DefIndex, LocalDefId};
use crate::hygiene::SyntaxContext; use crate::hygiene::SyntaxContext;
use crate::SPAN_TRACK; use crate::SPAN_TRACK;
@ -13,59 +7,69 @@ use rustc_data_structures::fx::FxIndexSet;
/// A compressed span. /// A compressed span.
/// ///
/// Whereas [`SpanData`] is 16 bytes, which is a bit too big to stick everywhere, `Span` /// [`SpanData`] is 16 bytes, which is too big to stick everywhere. `Span` only
/// is a form that only takes up 8 bytes, with less space for the length, parent and /// takes up 8 bytes, with less space for the length, parent and context. The
/// context. The vast majority (99.9%+) of `SpanData` instances will fit within /// vast majority (99.9%+) of `SpanData` instances can be made to fit within
/// those 8 bytes; any `SpanData` whose fields don't fit into a `Span` are /// those 8 bytes. Any `SpanData` whose fields don't fit into a `Span` are
/// stored in a separate interner table, and the `Span` will index into that /// stored in a separate interner table, and the `Span` will index into that
/// table. Interning is rare enough that the cost is low, but common enough /// table. Interning is rare enough that the cost is low, but common enough
/// that the code is exercised regularly. /// that the code is exercised regularly.
/// ///
/// An earlier version of this code used only 4 bytes for `Span`, but that was /// An earlier version of this code used only 4 bytes for `Span`, but that was
/// slower because only 80--90% of spans could be stored inline (even less in /// slower because only 80--90% of spans could be stored inline (even less in
/// very large crates) and so the interner was used a lot more. /// very large crates) and so the interner was used a lot more. That version of
/// the code also predated the storage of parents.
/// ///
/// Inline (compressed) format with no parent: /// There are four different span forms.
/// - `span.base_or_index == span_data.lo`
/// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`)
/// - `span.ctxt_or_tag == span_data.ctxt` (must be `<= MAX_CTXT`)
/// ///
/// Interned format with inline `SyntaxContext`: /// Inline-context format (requires non-huge length, non-huge context, and no parent):
/// - `span.base_or_index == index` (indexes into the interner table) /// - `span.lo_or_index == span_data.lo`
/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero) /// - `span.len_with_tag_or_marker == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`)
/// - `span.ctxt_or_tag == span_data.ctxt` (must be `<= MAX_CTXT`) /// - `span.ctxt_or_parent_or_marker == span_data.ctxt` (must be `<= MAX_CTXT`)
/// ///
/// Inline (compressed) format with root context: /// Inline-parent format (requires non-huge length, root context, and non-huge parent):
/// - `span.base_or_index == span_data.lo` /// - `span.lo_or_index == span_data.lo`
/// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`) /// - `span.len_with_tag_or_marker & !PARENT_TAG == len == span_data.hi - span_data.lo`
/// - `span.len_or_tag` has top bit (`PARENT_MASK`) set /// (must be `<= MAX_LEN`)
/// - `span.ctxt == span_data.parent` (must be `<= MAX_CTXT`) /// - `span.len_with_tag_or_marker` has top bit (`PARENT_TAG`) set
/// - `span.ctxt_or_parent_or_marker == span_data.parent` (must be `<= MAX_CTXT`)
/// ///
/// Interned format: /// Partially-interned format (requires non-huge context):
/// - `span.base_or_index == index` (indexes into the interner table) /// - `span.lo_or_index == index` (indexes into the interner table)
/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero) /// - `span.len_with_tag_or_marker == BASE_LEN_INTERNED_MARKER`
/// - `span.ctxt_or_tag == CTXT_TAG` /// - `span.ctxt_or_parent_or_marker == span_data.ctxt` (must be `<= MAX_CTXT`)
/// ///
/// The inline form uses 0 for the tag value (rather than 1) so that we don't /// Fully-interned format (all cases not covered above):
/// need to mask out the tag bit when getting the length, and so that the /// - `span.lo_or_index == index` (indexes into the interner table)
/// dummy span can be all zeroes. /// - `span.len_with_tag_or_marker == BASE_LEN_INTERNED_MARKER`
/// - `span.ctxt_or_parent_or_marker == CTXT_INTERNED_MARKER`
///
/// The partially-interned form requires looking in the interning table for
/// lo and length, but the context is stored inline as well as interned.
/// This is useful because context lookups are often done in isolation, and
/// inline lookups are quicker.
/// ///
/// Notes about the choice of field sizes: /// Notes about the choice of field sizes:
/// - `base` is 32 bits in both `Span` and `SpanData`, which means that `base` /// - `lo` is 32 bits in both `Span` and `SpanData`, which means that `lo`
/// values never cause interning. The number of bits needed for `base` /// values never cause interning. The number of bits needed for `lo`
/// depends on the crate size. 32 bits allows up to 4 GiB of code in a crate. /// depends on the crate size. 32 bits allows up to 4 GiB of code in a crate.
/// - `len` is 15 bits in `Span` (a u16, minus 1 bit for the tag) and 32 bits /// Having no compression on this field means there is no performance cliff
/// in `SpanData`, which means that large `len` values will cause interning. /// if a crate exceeds a particular size.
/// The number of bits needed for `len` does not depend on the crate size. /// - `len` is ~15 bits in `Span` (a u16, minus 1 bit for PARENT_TAG) and 32
/// The most common numbers of bits for `len` are from 0 to 7, with a peak usually /// bits in `SpanData`, which means that large `len` values will cause
/// at 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough /// interning. The number of bits needed for `len` does not depend on the
/// for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur /// crate size. The most common numbers of bits for `len` are from 0 to 7,
/// dozens of times in a typical crate. /// with a peak usually at 3 or 4, and then it drops off quickly from 8
/// - `ctxt_or_tag` is 16 bits in `Span` and 32 bits in `SpanData`, which means that /// onwards. 15 bits is enough for 99.99%+ of cases, but larger values
/// large `ctxt` values will cause interning. The number of bits needed for /// (sometimes 20+ bits) might occur dozens of times in a typical crate.
/// `ctxt` values depend partly on the crate size and partly on the form of /// - `ctxt_or_parent_or_marker` is 16 bits in `Span` and two 32 bit fields in
/// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt_or_tag`, /// `SpanData`, which means intering will happen if `ctxt` is large, if
/// but larger crates might need more than 16 bits. /// `parent` is large, or if both values are non-zero. The number of bits
/// needed for `ctxt` values depend partly on the crate size and partly on
/// the form of the code. No crates in `rustc-perf` need more than 15 bits
/// for `ctxt_or_parent_or_marker`, but larger crates might need more than 16
/// bits. The number of bits needed for `parent` hasn't been measured,
/// because `parent` isn't currently used by default.
/// ///
/// In order to reliably use parented spans in incremental compilation, /// In order to reliably use parented spans in incremental compilation,
/// the dependency to the parent definition's span. This is performed /// the dependency to the parent definition's span. This is performed
@ -74,19 +78,22 @@ use rustc_data_structures::fx::FxIndexSet;
#[derive(Clone, Copy, Eq, PartialEq, Hash)] #[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[rustc_pass_by_value] #[rustc_pass_by_value]
pub struct Span { pub struct Span {
base_or_index: u32, lo_or_index: u32,
len_or_tag: u16, len_with_tag_or_marker: u16,
ctxt_or_tag: u16, ctxt_or_parent_or_marker: u16,
} }
const LEN_TAG: u16 = 0b1111_1111_1111_1111; // `MAX_LEN` is chosen so that `PARENT_TAG | MAX_LEN` is distinct from
const PARENT_MASK: u16 = 0b1000_0000_0000_0000; // `BASE_LEN_INTERNED_MARKER`. (If `MAX_LEN` was 1 higher, this wouldn't be true.)
const MAX_LEN: u32 = 0b0111_1111_1111_1111; const MAX_LEN: u32 = 0b0111_1111_1111_1110;
const CTXT_TAG: u32 = 0b1111_1111_1111_1111; const MAX_CTXT: u32 = 0b0111_1111_1111_1110;
const MAX_CTXT: u32 = CTXT_TAG - 1; const PARENT_TAG: u16 = 0b1000_0000_0000_0000;
const BASE_LEN_INTERNED_MARKER: u16 = 0b1111_1111_1111_1111;
const CTXT_INTERNED_MARKER: u16 = 0b1111_1111_1111_1111;
/// Dummy span, both position and length are zero, syntax context is zero as well. /// The dummy span has zero position, length, and context, and no parent.
pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_tag: 0 }; pub const DUMMY_SP: Span =
Span { lo_or_index: 0, len_with_tag_or_marker: 0, ctxt_or_parent_or_marker: 0 };
impl Span { impl Span {
#[inline] #[inline]
@ -100,39 +107,43 @@ impl Span {
std::mem::swap(&mut lo, &mut hi); std::mem::swap(&mut lo, &mut hi);
} }
let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32()); let (lo2, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
if len <= MAX_LEN && ctxt2 <= MAX_CTXT { if len <= MAX_LEN {
let len_or_tag = len as u16; if ctxt2 <= MAX_CTXT && parent.is_none() {
debug_assert_eq!(len_or_tag & PARENT_MASK, 0); // Inline-context format.
if let Some(parent) = parent {
// Inline format with parent.
let len_or_tag = len_or_tag | PARENT_MASK;
let parent2 = parent.local_def_index.as_u32();
if ctxt2 == SyntaxContext::root().as_u32()
&& parent2 <= MAX_CTXT
&& len_or_tag < LEN_TAG
{
debug_assert_ne!(len_or_tag, LEN_TAG);
return Span { base_or_index: base, len_or_tag, ctxt_or_tag: parent2 as u16 };
}
} else {
// Inline format with ctxt.
debug_assert_ne!(len_or_tag, LEN_TAG);
return Span { return Span {
base_or_index: base, lo_or_index: lo2,
len_or_tag: len as u16, len_with_tag_or_marker: len as u16,
ctxt_or_tag: ctxt2 as u16, ctxt_or_parent_or_marker: ctxt2 as u16,
};
} else if ctxt2 == SyntaxContext::root().as_u32()
&& let Some(parent) = parent
&& let parent2 = parent.local_def_index.as_u32()
&& parent2 <= MAX_CTXT
{
// Inline-parent format.
return Span {
lo_or_index: lo2,
len_with_tag_or_marker: PARENT_TAG | len as u16,
ctxt_or_parent_or_marker: parent2 as u16
}; };
} }
} }
// Interned format. // Partially-interned or fully-interned format.
let index = let index =
with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent })); with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }));
let ctxt_or_tag = if ctxt2 <= MAX_CTXT { ctxt2 } else { CTXT_TAG } as u16; let ctxt_or_parent_or_marker = if ctxt2 <= MAX_CTXT {
Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_tag } ctxt2 as u16 // partially-interned
} else {
CTXT_INTERNED_MARKER // fully-interned
};
Span {
lo_or_index: index,
len_with_tag_or_marker: BASE_LEN_INTERNED_MARKER,
ctxt_or_parent_or_marker,
}
} }
#[inline] #[inline]
@ -148,56 +159,63 @@ impl Span {
/// This function must not be used outside the incremental engine. /// This function must not be used outside the incremental engine.
#[inline] #[inline]
pub fn data_untracked(self) -> SpanData { pub fn data_untracked(self) -> SpanData {
if self.len_or_tag != LEN_TAG { if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
// Inline format. if self.len_with_tag_or_marker & PARENT_TAG == 0 {
if self.len_or_tag & PARENT_MASK == 0 { // Inline-context format.
debug_assert!(self.len_or_tag as u32 <= MAX_LEN); let len = self.len_with_tag_or_marker as u32;
debug_assert!(len <= MAX_LEN);
SpanData { SpanData {
lo: BytePos(self.base_or_index), lo: BytePos(self.lo_or_index),
hi: BytePos(self.base_or_index + self.len_or_tag as u32), hi: BytePos(self.lo_or_index + len),
ctxt: SyntaxContext::from_u32(self.ctxt_or_tag as u32), ctxt: SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32),
parent: None, parent: None,
} }
} else { } else {
let len = self.len_or_tag & !PARENT_MASK; // Inline-parent format.
debug_assert!(len as u32 <= MAX_LEN); let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
let parent = debug_assert!(len <= MAX_LEN);
LocalDefId { local_def_index: DefIndex::from_u32(self.ctxt_or_tag as u32) }; let parent = LocalDefId {
local_def_index: DefIndex::from_u32(self.ctxt_or_parent_or_marker as u32),
};
SpanData { SpanData {
lo: BytePos(self.base_or_index), lo: BytePos(self.lo_or_index),
hi: BytePos(self.base_or_index + len as u32), hi: BytePos(self.lo_or_index + len),
ctxt: SyntaxContext::root(), ctxt: SyntaxContext::root(),
parent: Some(parent), parent: Some(parent),
} }
} }
} else { } else {
// Interned format. // Fully-interned or partially-interned format. In either case,
let index = self.base_or_index; // the interned value contains all the data, so we don't need to
// distinguish them.
let index = self.lo_or_index;
with_span_interner(|interner| interner.spans[index as usize]) with_span_interner(|interner| interner.spans[index as usize])
} }
} }
/// This function is used as a fast path when decoding the full `SpanData` is not necessary. /// This function is used as a fast path when decoding the full `SpanData` is not necessary.
/// It's a cut-down version of `data_untracked`.
#[inline] #[inline]
pub fn ctxt(self) -> SyntaxContext { pub fn ctxt(self) -> SyntaxContext {
let ctxt_or_tag = self.ctxt_or_tag as u32; if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
// Check for interned format. if self.len_with_tag_or_marker & PARENT_TAG == 0 {
if self.len_or_tag == LEN_TAG { // Inline-context format.
if ctxt_or_tag == CTXT_TAG { SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
// Fully interned format.
let index = self.base_or_index;
with_span_interner(|interner| interner.spans[index as usize].ctxt)
} else { } else {
// Interned format with inline ctxt. // Inline-parent format. We know that the SyntaxContext is root.
SyntaxContext::from_u32(ctxt_or_tag) SyntaxContext::root()
} }
} else if self.len_or_tag & PARENT_MASK == 0 {
// Inline format with inline ctxt.
SyntaxContext::from_u32(ctxt_or_tag)
} else { } else {
// Inline format with inline parent. if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
// We know that the SyntaxContext is root. // Partially-interned format. This path avoids looking up the
SyntaxContext::root() // interned value, and is the whole point of the
// partially-interned format.
SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
} else {
// Fully-interned format.
let index = self.lo_or_index;
with_span_interner(|interner| interner.spans[index as usize].ctxt)
}
} }
} }
} }