rustc_span: Optimize syntax context updates in spans
This commit is contained in:
parent
a70b2ae577
commit
d5dd2d8284
3 changed files with 149 additions and 73 deletions
|
@ -30,11 +30,11 @@ impl MutVisitor for Marker {
|
||||||
// it's some advanced case with macro-generated macros. So if we cache the marked version
|
// it's some advanced case with macro-generated macros. So if we cache the marked version
|
||||||
// of that context once, we'll typically have a 100% cache hit rate after that.
|
// of that context once, we'll typically have a 100% cache hit rate after that.
|
||||||
let Marker(expn_id, transparency, ref mut cache) = *self;
|
let Marker(expn_id, transparency, ref mut cache) = *self;
|
||||||
let data = span.data();
|
span.update_ctxt(|ctxt| {
|
||||||
let marked_ctxt = *cache
|
*cache
|
||||||
.entry(data.ctxt)
|
.entry(ctxt)
|
||||||
.or_insert_with(|| data.ctxt.apply_mark(expn_id.to_expn_id(), transparency));
|
.or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency))
|
||||||
*span = data.with_ctxt(marked_ctxt);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -521,7 +521,7 @@ impl SpanData {
|
||||||
Span::new(self.lo, hi, self.ctxt, self.parent)
|
Span::new(self.lo, hi, self.ctxt, self.parent)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
|
fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
|
||||||
Span::new(self.lo, self.hi, ctxt, self.parent)
|
Span::new(self.lo, self.hi, ctxt, self.parent)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -576,8 +576,9 @@ impl Span {
|
||||||
self.data().with_hi(hi)
|
self.data().with_hi(hi)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
|
pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Span {
|
||||||
self.data_untracked().with_ctxt(ctxt)
|
self.update_ctxt(|_| ctxt);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parent(self) -> Option<LocalDefId> {
|
pub fn parent(self) -> Option<LocalDefId> {
|
||||||
|
@ -1058,9 +1059,9 @@ impl Span {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
|
pub fn apply_mark(mut self, expn_id: ExpnId, transparency: Transparency) -> Span {
|
||||||
let span = self.data();
|
self.update_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency));
|
||||||
span.with_ctxt(span.ctxt.apply_mark(expn_id, transparency))
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1108,15 +1109,15 @@ impl Span {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn normalize_to_macros_2_0(self) -> Span {
|
pub fn normalize_to_macros_2_0(mut self) -> Span {
|
||||||
let span = self.data();
|
self.update_ctxt(|ctxt| ctxt.normalize_to_macros_2_0());
|
||||||
span.with_ctxt(span.ctxt.normalize_to_macros_2_0())
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn normalize_to_macro_rules(self) -> Span {
|
pub fn normalize_to_macro_rules(mut self) -> Span {
|
||||||
let span = self.data();
|
self.update_ctxt(|ctxt| ctxt.normalize_to_macro_rules());
|
||||||
span.with_ctxt(span.ctxt.normalize_to_macro_rules())
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,6 +87,45 @@ pub struct Span {
|
||||||
ctxt_or_parent_or_marker: u16,
|
ctxt_or_parent_or_marker: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Span {
|
||||||
|
#[inline]
|
||||||
|
fn data_inline_ctxt(self) -> SpanData {
|
||||||
|
let len = self.len_with_tag_or_marker as u32;
|
||||||
|
debug_assert!(len <= MAX_LEN);
|
||||||
|
SpanData {
|
||||||
|
lo: BytePos(self.lo_or_index),
|
||||||
|
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
|
||||||
|
ctxt: SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32),
|
||||||
|
parent: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn data_inline_parent(self) -> SpanData {
|
||||||
|
let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
|
||||||
|
debug_assert!(len <= MAX_LEN);
|
||||||
|
let parent = LocalDefId {
|
||||||
|
local_def_index: DefIndex::from_u32(self.ctxt_or_parent_or_marker as u32),
|
||||||
|
};
|
||||||
|
SpanData {
|
||||||
|
lo: BytePos(self.lo_or_index),
|
||||||
|
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
|
||||||
|
ctxt: SyntaxContext::root(),
|
||||||
|
parent: Some(parent),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn data_partially_interned(self) -> SpanData {
|
||||||
|
SpanData {
|
||||||
|
ctxt: SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32),
|
||||||
|
..with_span_interner(|interner| interner.spans[self.lo_or_index as usize])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
fn data_interned(self) -> SpanData {
|
||||||
|
with_span_interner(|interner| interner.spans[self.lo_or_index as usize])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// `MAX_LEN` is chosen so that `PARENT_TAG | MAX_LEN` is distinct from
|
// `MAX_LEN` is chosen so that `PARENT_TAG | MAX_LEN` is distinct from
|
||||||
// `BASE_LEN_INTERNED_MARKER`. (If `MAX_LEN` was 1 higher, this wouldn't be true.)
|
// `BASE_LEN_INTERNED_MARKER`. (If `MAX_LEN` was 1 higher, this wouldn't be true.)
|
||||||
const MAX_LEN: u32 = 0b0111_1111_1111_1110;
|
const MAX_LEN: u32 = 0b0111_1111_1111_1110;
|
||||||
|
@ -111,42 +150,49 @@ impl Span {
|
||||||
std::mem::swap(&mut lo, &mut hi);
|
std::mem::swap(&mut lo, &mut hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (lo2, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
|
// Small len may enable one of fully inline formats (or may not).
|
||||||
|
let (len, ctxt32) = (hi.0 - lo.0, ctxt.as_u32());
|
||||||
if len <= MAX_LEN {
|
if len <= MAX_LEN {
|
||||||
if ctxt2 <= MAX_CTXT && parent.is_none() {
|
if ctxt32 <= MAX_CTXT && parent.is_none() {
|
||||||
// Inline-context format.
|
// Inline-context format.
|
||||||
return Span {
|
return Span {
|
||||||
lo_or_index: lo2,
|
lo_or_index: lo.0,
|
||||||
len_with_tag_or_marker: len as u16,
|
len_with_tag_or_marker: len as u16,
|
||||||
ctxt_or_parent_or_marker: ctxt2 as u16,
|
ctxt_or_parent_or_marker: ctxt32 as u16,
|
||||||
};
|
};
|
||||||
} else if ctxt2 == SyntaxContext::root().as_u32()
|
} else if ctxt32 == 0
|
||||||
&& let Some(parent) = parent
|
&& let Some(parent) = parent
|
||||||
&& let parent2 = parent.local_def_index.as_u32()
|
&& let parent32 = parent.local_def_index.as_u32()
|
||||||
&& parent2 <= MAX_CTXT
|
&& parent32 <= MAX_CTXT
|
||||||
{
|
{
|
||||||
// Inline-parent format.
|
// Inline-parent format.
|
||||||
return Span {
|
return Span {
|
||||||
lo_or_index: lo2,
|
lo_or_index: lo.0,
|
||||||
len_with_tag_or_marker: PARENT_TAG | len as u16,
|
len_with_tag_or_marker: PARENT_TAG | len as u16,
|
||||||
ctxt_or_parent_or_marker: parent2 as u16,
|
ctxt_or_parent_or_marker: parent32 as u16,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Partially-interned or fully-interned format.
|
// Otherwise small ctxt may enable the partially inline format.
|
||||||
let index =
|
let index = |ctxt| {
|
||||||
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_parent_or_marker = if ctxt2 <= MAX_CTXT {
|
|
||||||
ctxt2 as u16 // partially-interned
|
|
||||||
} else {
|
|
||||||
CTXT_INTERNED_MARKER // fully-interned
|
|
||||||
};
|
};
|
||||||
|
if ctxt32 <= MAX_CTXT {
|
||||||
|
// Partially-interned format.
|
||||||
Span {
|
Span {
|
||||||
lo_or_index: index,
|
// Interned ctxt should never be read, so it can use any value.
|
||||||
|
lo_or_index: index(SyntaxContext::from_u32(u32::MAX)),
|
||||||
len_with_tag_or_marker: BASE_LEN_INTERNED_MARKER,
|
len_with_tag_or_marker: BASE_LEN_INTERNED_MARKER,
|
||||||
ctxt_or_parent_or_marker,
|
ctxt_or_parent_or_marker: ctxt32 as u16,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Interned format.
|
||||||
|
Span {
|
||||||
|
lo_or_index: index(ctxt),
|
||||||
|
len_with_tag_or_marker: BASE_LEN_INTERNED_MARKER,
|
||||||
|
ctxt_or_parent_or_marker: CTXT_INTERNED_MARKER,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,34 +212,17 @@ impl Span {
|
||||||
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
|
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
|
||||||
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
|
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
|
||||||
// Inline-context format.
|
// Inline-context format.
|
||||||
let len = self.len_with_tag_or_marker as u32;
|
self.data_inline_ctxt()
|
||||||
debug_assert!(len <= MAX_LEN);
|
|
||||||
SpanData {
|
|
||||||
lo: BytePos(self.lo_or_index),
|
|
||||||
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
|
|
||||||
ctxt: SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32),
|
|
||||||
parent: None,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Inline-parent format.
|
// Inline-parent format.
|
||||||
let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
|
self.data_inline_parent()
|
||||||
debug_assert!(len <= MAX_LEN);
|
|
||||||
let parent = LocalDefId {
|
|
||||||
local_def_index: DefIndex::from_u32(self.ctxt_or_parent_or_marker as u32),
|
|
||||||
};
|
|
||||||
SpanData {
|
|
||||||
lo: BytePos(self.lo_or_index),
|
|
||||||
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
|
|
||||||
ctxt: SyntaxContext::root(),
|
|
||||||
parent: Some(parent),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
|
||||||
|
// Partially-interned format.
|
||||||
|
self.data_partially_interned()
|
||||||
} else {
|
} else {
|
||||||
// Fully-interned or partially-interned format. In either case,
|
// Interned format.
|
||||||
// the interned value contains all the data, so we don't need to
|
self.data_interned()
|
||||||
// distinguish them.
|
|
||||||
let index = self.lo_or_index;
|
|
||||||
with_span_interner(|interner| interner.spans[index as usize])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,27 +243,73 @@ impl Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For optimization we are interested in cases in which the context is inline and the context
|
||||||
|
// update doesn't change format. All non-inline or format changing scenarios require accessing
|
||||||
|
// interner and can fall back to `Span::new`.
|
||||||
|
#[inline]
|
||||||
|
pub fn update_ctxt(&mut self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) {
|
||||||
|
let (updated_ctxt32, data);
|
||||||
|
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
|
||||||
|
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
|
||||||
|
// Inline-context format.
|
||||||
|
updated_ctxt32 =
|
||||||
|
update(SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)).as_u32();
|
||||||
|
// Any small new context including zero will preserve the format.
|
||||||
|
if updated_ctxt32 <= MAX_CTXT {
|
||||||
|
self.ctxt_or_parent_or_marker = updated_ctxt32 as u16;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data = self.data_inline_ctxt();
|
||||||
|
} else {
|
||||||
|
// Inline-parent format.
|
||||||
|
updated_ctxt32 = update(SyntaxContext::root()).as_u32();
|
||||||
|
// Only if the new context is zero the format will be preserved.
|
||||||
|
if updated_ctxt32 == 0 {
|
||||||
|
// Do nothing.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data = self.data_inline_parent();
|
||||||
|
}
|
||||||
|
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
|
||||||
|
// Partially-interned format.
|
||||||
|
updated_ctxt32 =
|
||||||
|
update(SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)).as_u32();
|
||||||
|
// Any small new context excluding zero will preserve the format.
|
||||||
|
// Zero may change the format to `InlineParent` if parent and len are small enough.
|
||||||
|
if updated_ctxt32 <= MAX_CTXT && updated_ctxt32 != 0 {
|
||||||
|
self.ctxt_or_parent_or_marker = updated_ctxt32 as u16;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data = self.data_partially_interned();
|
||||||
|
} else {
|
||||||
|
// Interned format.
|
||||||
|
data = self.data_interned();
|
||||||
|
updated_ctxt32 = update(data.ctxt).as_u32();
|
||||||
|
}
|
||||||
|
|
||||||
|
// We could not keep the span in the same inline format, fall back to the complete logic.
|
||||||
|
*self = data.with_ctxt(SyntaxContext::from_u32(updated_ctxt32));
|
||||||
|
}
|
||||||
|
|
||||||
// Returns either syntactic context, if it can be retrieved without taking the interner lock,
|
// Returns either syntactic context, if it can be retrieved without taking the interner lock,
|
||||||
// or an index into the interner if it cannot.
|
// or an index into the interner if it cannot.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
|
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
|
||||||
Ok(if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
|
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
|
||||||
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
|
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
|
||||||
// Inline-context format.
|
// Inline-context format.
|
||||||
SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
|
Ok(SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32))
|
||||||
} else {
|
} else {
|
||||||
// Inline-parent format. We know that the SyntaxContext is root.
|
// Inline-parent format.
|
||||||
SyntaxContext::root()
|
Ok(SyntaxContext::root())
|
||||||
}
|
}
|
||||||
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
|
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
|
||||||
// Partially-interned format. This path avoids looking up the
|
// Partially-interned format.
|
||||||
// interned value, and is the whole point of the
|
Ok(SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32))
|
||||||
// partially-interned format.
|
|
||||||
SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
|
|
||||||
} else {
|
} else {
|
||||||
// Fully-interned format.
|
// Interned format.
|
||||||
return Err(self.lo_or_index as usize);
|
Err(self.lo_or_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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue