Skip to content

Commit 8c62dc1

Browse files
committed
rustc_span: Add conveniences for working with span formats
Centralize span ctxt updates in Span::update_ctxt. Stop requiring inline and interned ctxts in partially interned format to be synchronized.
1 parent 05965ae commit 8c62dc1

File tree

3 files changed

+173
-97
lines changed

3 files changed

+173
-97
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ impl MutVisitor for Marker {
3030
// it's some advanced case with macro-generated macros. So if we cache the marked version
3131
// of that context once, we'll typically have a 100% cache hit rate after that.
3232
let Marker(expn_id, transparency, ref mut cache) = *self;
33-
let data = span.data();
34-
let marked_ctxt = *cache
35-
.entry(data.ctxt)
36-
.or_insert_with(|| data.ctxt.apply_mark(expn_id.to_expn_id(), transparency));
37-
*span = data.with_ctxt(marked_ctxt);
33+
span.update_ctxt(|ctxt| {
34+
*cache
35+
.entry(ctxt)
36+
.or_insert_with(|| ctxt.apply_mark(expn_id.to_expn_id(), transparency))
37+
});
3838
}
3939
}
4040

compiler/rustc_span/src/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,9 @@ impl SpanData {
520520
pub fn with_hi(&self, hi: BytePos) -> Span {
521521
Span::new(self.lo, hi, self.ctxt, self.parent)
522522
}
523+
/// Avoid if possible, `Span::update_ctxt` should be preferred.
523524
#[inline]
524-
pub fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
525+
fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
525526
Span::new(self.lo, self.hi, ctxt, self.parent)
526527
}
527528
#[inline]
@@ -576,8 +577,9 @@ impl Span {
576577
self.data().with_hi(hi)
577578
}
578579
#[inline]
579-
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
580-
self.data_untracked().with_ctxt(ctxt)
580+
pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Span {
581+
self.update_ctxt(|_| ctxt);
582+
self
581583
}
582584
#[inline]
583585
pub fn parent(self) -> Option<LocalDefId> {
@@ -1051,9 +1053,9 @@ impl Span {
10511053
}
10521054

10531055
#[inline]
1054-
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> Span {
1055-
let span = self.data();
1056-
span.with_ctxt(span.ctxt.apply_mark(expn_id, transparency))
1056+
pub fn apply_mark(mut self, expn_id: ExpnId, transparency: Transparency) -> Span {
1057+
self.update_ctxt(|ctxt| ctxt.apply_mark(expn_id, transparency));
1058+
self
10571059
}
10581060

10591061
#[inline]
@@ -1101,15 +1103,15 @@ impl Span {
11011103
}
11021104

11031105
#[inline]
1104-
pub fn normalize_to_macros_2_0(self) -> Span {
1105-
let span = self.data();
1106-
span.with_ctxt(span.ctxt.normalize_to_macros_2_0())
1106+
pub fn normalize_to_macros_2_0(mut self) -> Span {
1107+
self.update_ctxt(|ctxt| ctxt.normalize_to_macros_2_0());
1108+
self
11071109
}
11081110

11091111
#[inline]
1110-
pub fn normalize_to_macro_rules(self) -> Span {
1111-
let span = self.data();
1112-
span.with_ctxt(span.ctxt.normalize_to_macro_rules())
1112+
pub fn normalize_to_macro_rules(mut self) -> Span {
1113+
self.update_ctxt(|ctxt| ctxt.normalize_to_macro_rules());
1114+
self
11131115
}
11141116
}
11151117

compiler/rustc_span/src/span_encoding.rs

+154-80
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::SPAN_TRACK;
44
use crate::{BytePos, SpanData};
55

66
use rustc_data_structures::fx::FxIndexSet;
7-
87
// This code is very hot and uses lots of arithmetic, avoid overflow checks for performance.
98
// See https://github.com/rust-lang/rust/pull/119440#issuecomment-1874255727
109
use rustc_serialize::int_overflow::DebugStrictAdd;
10+
use std::mem::transmute;
1111

1212
/// A compressed span.
1313
///
@@ -87,6 +87,106 @@ pub struct Span {
8787
ctxt_or_parent_or_marker: u16,
8888
}
8989

90+
// Convenience structures for all span formats.
91+
#[derive(Clone, Copy)]
92+
struct InlineCtxt {
93+
lo: u32,
94+
len: u16,
95+
ctxt: u16,
96+
}
97+
98+
#[derive(Clone, Copy)]
99+
struct InlineParent {
100+
lo: u32,
101+
len_with_tag: u16,
102+
parent: u16,
103+
}
104+
105+
#[derive(Clone, Copy)]
106+
struct PartiallyInterned {
107+
index: u32,
108+
_marker1: u16,
109+
ctxt: u16,
110+
}
111+
112+
#[derive(Clone, Copy)]
113+
struct Interned {
114+
index: u32,
115+
_marker1: u16,
116+
_marker2: u16,
117+
}
118+
119+
enum Fmt<'a> {
120+
InlineCtxt(&'a mut InlineCtxt),
121+
InlineParent(&'a mut InlineParent),
122+
PartiallyInterned(&'a mut PartiallyInterned),
123+
Interned(&'a mut Interned),
124+
}
125+
126+
impl InlineCtxt {
127+
fn data(self) -> SpanData {
128+
let len = self.len as u32;
129+
debug_assert!(len <= MAX_LEN);
130+
SpanData {
131+
lo: BytePos(self.lo),
132+
hi: BytePos(self.lo.debug_strict_add(len)),
133+
ctxt: SyntaxContext::from_u32(self.ctxt as u32),
134+
parent: None,
135+
}
136+
}
137+
fn to_span(self) -> Span {
138+
unsafe { transmute(self) }
139+
}
140+
}
141+
142+
impl InlineParent {
143+
fn data(self) -> SpanData {
144+
let len = (self.len_with_tag & !PARENT_TAG) as u32;
145+
debug_assert!(len <= MAX_LEN);
146+
SpanData {
147+
lo: BytePos(self.lo),
148+
hi: BytePos(self.lo.debug_strict_add(len)),
149+
ctxt: SyntaxContext::root(),
150+
parent: Some(LocalDefId { local_def_index: DefIndex::from_u32(self.parent as u32) }),
151+
}
152+
}
153+
fn to_span(self) -> Span {
154+
unsafe { transmute(self) }
155+
}
156+
}
157+
158+
impl PartiallyInterned {
159+
fn data(self) -> SpanData {
160+
SpanData {
161+
ctxt: SyntaxContext::from_u32(self.ctxt as u32),
162+
..with_span_interner(|interner| interner.spans[self.index as usize])
163+
}
164+
}
165+
fn to_span(self) -> Span {
166+
unsafe { transmute(self) }
167+
}
168+
}
169+
170+
impl Interned {
171+
fn data(self) -> SpanData {
172+
with_span_interner(|interner| interner.spans[self.index as usize])
173+
}
174+
fn to_span(self) -> Span {
175+
unsafe { transmute(self) }
176+
}
177+
}
178+
179+
impl Fmt<'_> {
180+
fn data(self) -> SpanData {
181+
match self {
182+
Fmt::InlineCtxt(span) => span.data(),
183+
Fmt::InlineParent(span) => span.data(),
184+
Fmt::PartiallyInterned(span) => span.data(),
185+
Fmt::Interned(span) => span.data(),
186+
}
187+
}
188+
}
189+
90190
// `MAX_LEN` is chosen so that `PARENT_TAG | MAX_LEN` is distinct from
91191
// `BASE_LEN_INTERNED_MARKER`. (If `MAX_LEN` was 1 higher, this wouldn't be true.)
92192
const MAX_LEN: u32 = 0b0111_1111_1111_1110;
@@ -111,42 +211,47 @@ impl Span {
111211
std::mem::swap(&mut lo, &mut hi);
112212
}
113213

114-
let (lo2, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
115-
214+
// Small len may enable one of fully inline formats (or may not).
215+
let (len, ctxt32) = (hi.0 - lo.0, ctxt.as_u32());
116216
if len <= MAX_LEN {
117-
if ctxt2 <= MAX_CTXT && parent.is_none() {
118-
// Inline-context format.
119-
return Span {
120-
lo_or_index: lo2,
121-
len_with_tag_or_marker: len as u16,
122-
ctxt_or_parent_or_marker: ctxt2 as u16,
123-
};
124-
} else if ctxt2 == SyntaxContext::root().as_u32()
217+
if ctxt32 <= MAX_CTXT && parent.is_none() {
218+
return InlineCtxt { lo: lo.0, len: len as u16, ctxt: ctxt32 as u16 }.to_span();
219+
} else if ctxt32 == 0
125220
&& let Some(parent) = parent
126-
&& let parent2 = parent.local_def_index.as_u32()
127-
&& parent2 <= MAX_CTXT
221+
&& let parent32 = parent.local_def_index.as_u32()
222+
&& parent32 <= MAX_CTXT
128223
{
129-
// Inline-parent format.
130-
return Span {
131-
lo_or_index: lo2,
132-
len_with_tag_or_marker: PARENT_TAG | len as u16,
133-
ctxt_or_parent_or_marker: parent2 as u16,
134-
};
224+
let len_with_tag = PARENT_TAG | len as u16;
225+
return InlineParent { lo: lo.0, len_with_tag, parent: parent32 as u16 }.to_span();
135226
}
136227
}
137228

138-
// Partially-interned or fully-interned format.
139-
let index =
140-
with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }));
141-
let ctxt_or_parent_or_marker = if ctxt2 <= MAX_CTXT {
142-
ctxt2 as u16 // partially-interned
143-
} else {
144-
CTXT_INTERNED_MARKER // fully-interned
229+
// Otherwise small ctxt may enable the partially inline format.
230+
let index = |ctxt| {
231+
with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }))
145232
};
146-
Span {
147-
lo_or_index: index,
148-
len_with_tag_or_marker: BASE_LEN_INTERNED_MARKER,
149-
ctxt_or_parent_or_marker,
233+
if ctxt32 <= MAX_CTXT {
234+
let index = index(SyntaxContext::from_u32(u32::MAX)); // any value, should never be read
235+
PartiallyInterned { index, _marker1: BASE_LEN_INTERNED_MARKER, ctxt: ctxt32 as u16 }
236+
.to_span()
237+
} else {
238+
let index = index(ctxt);
239+
Interned { index, _marker1: BASE_LEN_INTERNED_MARKER, _marker2: CTXT_INTERNED_MARKER }
240+
.to_span()
241+
}
242+
}
243+
244+
fn fmt(&mut self) -> Fmt<'_> {
245+
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
246+
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
247+
Fmt::InlineCtxt(unsafe { transmute(self) })
248+
} else {
249+
Fmt::InlineParent(unsafe { transmute(self) })
250+
}
251+
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
252+
Fmt::PartiallyInterned(unsafe { transmute(self) })
253+
} else {
254+
Fmt::Interned(unsafe { transmute(self) })
150255
}
151256
}
152257

@@ -162,39 +267,8 @@ impl Span {
162267
/// Internal function to translate between an encoded span and the expanded representation.
163268
/// This function must not be used outside the incremental engine.
164269
#[inline]
165-
pub fn data_untracked(self) -> SpanData {
166-
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
167-
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
168-
// Inline-context format.
169-
let len = self.len_with_tag_or_marker as u32;
170-
debug_assert!(len <= MAX_LEN);
171-
SpanData {
172-
lo: BytePos(self.lo_or_index),
173-
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
174-
ctxt: SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32),
175-
parent: None,
176-
}
177-
} else {
178-
// Inline-parent format.
179-
let len = (self.len_with_tag_or_marker & !PARENT_TAG) as u32;
180-
debug_assert!(len <= MAX_LEN);
181-
let parent = LocalDefId {
182-
local_def_index: DefIndex::from_u32(self.ctxt_or_parent_or_marker as u32),
183-
};
184-
SpanData {
185-
lo: BytePos(self.lo_or_index),
186-
hi: BytePos(self.lo_or_index.debug_strict_add(len)),
187-
ctxt: SyntaxContext::root(),
188-
parent: Some(parent),
189-
}
190-
}
191-
} else {
192-
// Fully-interned or partially-interned format. In either case,
193-
// the interned value contains all the data, so we don't need to
194-
// distinguish them.
195-
let index = self.lo_or_index;
196-
with_span_interner(|interner| interner.spans[index as usize])
197-
}
270+
pub fn data_untracked(mut self) -> SpanData {
271+
self.fmt().data()
198272
}
199273

200274
/// Returns `true` if this is a dummy span with any hygienic context.
@@ -214,26 +288,26 @@ impl Span {
214288
}
215289
}
216290

291+
// For optimization we are interested in cases in which the context is inline and the context
292+
// update doesn't change format. All non-inline or format changing scenarios require accessing
293+
// interner and can fall back to `Span::new`.
294+
pub fn update_ctxt(&mut self, update: impl FnOnce(SyntaxContext) -> SyntaxContext) {
295+
// FIXME(#125017): Update ctxt inline without touching interner when possible.
296+
let data = self.data();
297+
*self = data.with_ctxt(update(data.ctxt));
298+
}
299+
217300
// Returns either syntactic context, if it can be retrieved without taking the interner lock,
218301
// or an index into the interner if it cannot.
219-
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
220-
Ok(if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
221-
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
222-
// Inline-context format.
223-
SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
224-
} else {
225-
// Inline-parent format. We know that the SyntaxContext is root.
226-
SyntaxContext::root()
302+
fn inline_ctxt(mut self) -> Result<SyntaxContext, usize> {
303+
match self.fmt() {
304+
Fmt::InlineCtxt(InlineCtxt { ctxt, .. })
305+
| Fmt::PartiallyInterned(PartiallyInterned { ctxt, .. }) => {
306+
Ok(SyntaxContext::from_u32(*ctxt as u32))
227307
}
228-
} else if self.ctxt_or_parent_or_marker != CTXT_INTERNED_MARKER {
229-
// Partially-interned format. This path avoids looking up the
230-
// interned value, and is the whole point of the
231-
// partially-interned format.
232-
SyntaxContext::from_u32(self.ctxt_or_parent_or_marker as u32)
233-
} else {
234-
// Fully-interned format.
235-
return Err(self.lo_or_index as usize);
236-
})
308+
Fmt::InlineParent(_) => Ok(SyntaxContext::root()),
309+
Fmt::Interned(span) => Err(span.index as usize),
310+
}
237311
}
238312

239313
/// This function is used as a fast path when decoding the full `SpanData` is not necessary.

0 commit comments

Comments
 (0)