1
Fork 0

Optimize Span::is_dummy.

It's quite hot, and worth having a version that works directly at the
`Span` level, rather than first converting to the `SpanData` level.
This commit is contained in:
Nicholas Nethercote 2023-09-06 10:05:35 +10:00
parent e525e4f10a
commit 5790372736
2 changed files with 18 additions and 11 deletions

View file

@ -510,10 +510,6 @@ impl SpanData {
pub fn is_dummy(self) -> bool { pub fn is_dummy(self) -> bool {
self.lo.0 == 0 && self.hi.0 == 0 self.lo.0 == 0 && self.hi.0 == 0
} }
#[inline]
pub fn is_visible(self, sm: &SourceMap) -> bool {
!self.is_dummy() && sm.is_span_accessible(self.span())
}
/// Returns `true` if `self` fully encloses `other`. /// Returns `true` if `self` fully encloses `other`.
pub fn contains(self, other: Self) -> bool { pub fn contains(self, other: Self) -> bool {
self.lo <= other.lo && other.hi <= self.hi self.lo <= other.lo && other.hi <= self.hi
@ -573,15 +569,9 @@ impl Span {
self.data().with_parent(ctxt) self.data().with_parent(ctxt)
} }
/// Returns `true` if this is a dummy span with any hygienic context.
#[inline]
pub fn is_dummy(self) -> bool {
self.data_untracked().is_dummy()
}
#[inline] #[inline]
pub fn is_visible(self, sm: &SourceMap) -> bool { pub fn is_visible(self, sm: &SourceMap) -> bool {
self.data_untracked().is_visible(sm) !self.is_dummy() && sm.is_span_accessible(self)
} }
/// Returns `true` if this span comes from any kind of macro, desugaring or inlining. /// Returns `true` if this span comes from any kind of macro, desugaring or inlining.

View file

@ -193,6 +193,23 @@ impl Span {
} }
} }
/// Returns `true` if this is a dummy span with any hygienic context.
#[inline]
pub fn is_dummy(self) -> bool {
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
// Inline-context or inline-parent format.
let lo = self.lo_or_index;
let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
debug_assert!(len <= MAX_LEN);
lo == 0 && len == 0
} else {
// Fully-interned or partially-interned format.
let index = self.lo_or_index;
let data = with_span_interner(|interner| interner.spans[index as usize]);
data.lo == BytePos(0) && data.hi == BytePos(0)
}
}
/// 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`. /// It's a cut-down version of `data_untracked`.
#[inline] #[inline]