Skip to content

Commit cfa7765

Browse files
committed
Auto merge of #59693 - nnethercote:64-bit-Spans, r=<try>
Increase `Span` from 4 bytes to 8 bytes. This increases the size of some important types, such as `ast::Expr` and `mir::Statement`. However, it drastically reduces how much the interner is used, and the fields are more natural sizes that don't require bit operations to extract. As a result, instruction counts drop across a range of workloads, by as much as 10% for `script-servo` incremental builds. Peak memory usage goes up a little for some cases, but down by more for some other cases -- as much as 18% for non-incremental builds of `packed-simd`. The commit also: - removes the `repr(packed)`, because it has negligible effect, but can cause undefined behaviour; - replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.) with derived ones. r? @petrochenkov
2 parents f717b58 + 7f2ca81 commit cfa7765

File tree

4 files changed

+66
-103
lines changed

4 files changed

+66
-103
lines changed

Diff for: src/librustc/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ pub struct Statement<'tcx> {
17351735

17361736
// `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
17371737
#[cfg(target_arch = "x86_64")]
1738-
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Statement<'_>>() == 48);
1738+
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Statement<'_>>() == 56);
17391739

17401740
impl<'tcx> Statement<'tcx> {
17411741
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids

Diff for: src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ pub struct Expr {
946946

947947
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
948948
#[cfg(target_arch = "x86_64")]
949-
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 88);
949+
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 96);
950950

951951
impl Expr {
952952
/// Whether this expression would be valid somewhere that expects a value; for example, an `if`

Diff for: src/libsyntax_pos/hygiene.rs

+3
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,17 @@ pub fn clear_markings() {
218218
}
219219

220220
impl SyntaxContext {
221+
#[inline]
221222
pub const fn empty() -> Self {
222223
SyntaxContext(0)
223224
}
224225

226+
#[inline]
225227
crate fn as_u32(self) -> u32 {
226228
self.0
227229
}
228230

231+
#[inline]
229232
crate fn from_u32(raw: u32) -> SyntaxContext {
230233
SyntaxContext(raw)
231234
}

Diff for: src/libsyntax_pos/span_encoding.rs

+61-101
Original file line numberDiff line numberDiff line change
@@ -9,122 +9,82 @@ use crate::{BytePos, SpanData};
99
use crate::hygiene::SyntaxContext;
1010

1111
use rustc_data_structures::fx::FxHashMap;
12-
use std::hash::{Hash, Hasher};
1312

1413
/// A compressed span.
15-
/// Contains either fields of `SpanData` inline if they are small, or index into span interner.
16-
/// The primary goal of `Span` is to be as small as possible and fit into other structures
17-
/// (that's why it uses `packed` as well). Decoding speed is the second priority.
18-
/// See `SpanData` for the info on span fields in decoded representation.
19-
#[repr(packed)]
20-
pub struct Span(u32);
21-
22-
impl Copy for Span {}
23-
impl Clone for Span {
24-
#[inline]
25-
fn clone(&self) -> Span {
26-
*self
27-
}
28-
}
29-
impl PartialEq for Span {
30-
#[inline]
31-
fn eq(&self, other: &Span) -> bool {
32-
let a = self.0;
33-
let b = other.0;
34-
a == b
35-
}
36-
}
37-
impl Eq for Span {}
38-
impl Hash for Span {
39-
#[inline]
40-
fn hash<H: Hasher>(&self, state: &mut H) {
41-
let a = self.0;
42-
a.hash(state)
43-
}
14+
///
15+
/// `SpanData` is 12 bytes, which is a bit too big to stick everywhere. `Span`
16+
/// is a form that only takes up 8 bytes, with less space for the length and
17+
/// context. The vast majority of `SpanData` instances will fit within those 8
18+
/// bytes; any `SpanData` whose fields don't fit into a `Span` are stored in a
19+
/// separate interner table, and the `Span` will index into that table. (An
20+
/// earlier version of this code used only 4 bytes for `Span`, but that was
21+
/// slower because the interner was used a lot more.)
22+
///
23+
/// Inline (compressed) format:
24+
/// - span.base_or_index = span_data.lo
25+
/// - span.len_or_tag = len = span_data.hi - span_data.lo (must be <= MAX_LEN)
26+
/// - span.ctxt = span_data.ctxt (must be <= MAX_CTXT)
27+
///
28+
/// Interned format:
29+
/// - span.base_or_index = index into the interner vector
30+
/// - span.len_or_tag = LEN_TAG (high bit set, all other bits are zero)
31+
/// - span.ctxt = 0
32+
///
33+
/// Note: the inline form uses 0 for the tag value (rather than 1) so that we
34+
/// don't need to mask out the tag bit when getting the length, and so that the
35+
/// dummy span can be all zeroes.
36+
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
37+
pub struct Span {
38+
base_or_index: u32,
39+
len_or_tag: u16,
40+
ctxt_or_zero: u16
4441
}
4542

43+
const LEN_TAG: u16 = 0b1000_0000_0000_0000;
44+
const MAX_LEN: u32 = 0b0111_1111_1111_1111;
45+
const MAX_CTXT: u32 = 0b1111_1111_1111_1111;
46+
4647
/// Dummy span, both position and length are zero, syntax context is zero as well.
47-
/// This span is kept inline and encoded with format 0.
48-
pub const DUMMY_SP: Span = Span(0);
48+
pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_zero: 0 };
4949

5050
impl Span {
5151
#[inline]
52-
pub fn new(lo: BytePos, hi: BytePos, ctxt: SyntaxContext) -> Self {
53-
encode(&match lo <= hi {
54-
true => SpanData { lo, hi, ctxt },
55-
false => SpanData { lo: hi, hi: lo, ctxt },
56-
})
52+
pub fn new(mut lo: BytePos, mut hi: BytePos, ctxt: SyntaxContext) -> Self {
53+
if lo > hi {
54+
std::mem::swap(&mut lo, &mut hi);
55+
}
56+
57+
let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
58+
59+
if len <= MAX_LEN && ctxt2 <= MAX_CTXT {
60+
// Inline format.
61+
Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_zero: ctxt2 as u16 }
62+
} else {
63+
// Interned format.
64+
let index = with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt }));
65+
Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_zero: 0 }
66+
}
5767
}
5868

5969
#[inline]
6070
pub fn data(self) -> SpanData {
61-
decode(self)
71+
if self.len_or_tag != LEN_TAG {
72+
// Inline format.
73+
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
74+
SpanData {
75+
lo: BytePos(self.base_or_index),
76+
hi: BytePos(self.base_or_index + self.len_or_tag as u32),
77+
ctxt: SyntaxContext::from_u32(self.ctxt_or_zero as u32),
78+
}
79+
} else {
80+
// Interned format.
81+
debug_assert!(self.ctxt_or_zero == 0);
82+
let index = self.base_or_index;
83+
with_span_interner(|interner| *interner.get(index))
84+
}
6285
}
6386
}
6487

65-
// Tags
66-
const TAG_INLINE: u32 = 0;
67-
const TAG_INTERNED: u32 = 1;
68-
const TAG_MASK: u32 = 1;
69-
70-
// Fields indexes
71-
const BASE_INDEX: usize = 0;
72-
const LEN_INDEX: usize = 1;
73-
const CTXT_INDEX: usize = 2;
74-
75-
// Tag = 0, inline format.
76-
// -------------------------------------------------------------
77-
// | base 31:7 | len 6:1 | ctxt (currently 0 bits) | tag 0:0 |
78-
// -------------------------------------------------------------
79-
// Since there are zero bits for ctxt, only SpanData with a 0 SyntaxContext
80-
// can be inline.
81-
const INLINE_SIZES: [u32; 3] = [25, 6, 0];
82-
const INLINE_OFFSETS: [u32; 3] = [7, 1, 1];
83-
84-
// Tag = 1, interned format.
85-
// ------------------------
86-
// | index 31:1 | tag 0:0 |
87-
// ------------------------
88-
const INTERNED_INDEX_SIZE: u32 = 31;
89-
const INTERNED_INDEX_OFFSET: u32 = 1;
90-
91-
#[inline]
92-
fn encode(sd: &SpanData) -> Span {
93-
let (base, len, ctxt) = (sd.lo.0, sd.hi.0 - sd.lo.0, sd.ctxt.as_u32());
94-
95-
let val = if (base >> INLINE_SIZES[BASE_INDEX]) == 0 &&
96-
(len >> INLINE_SIZES[LEN_INDEX]) == 0 &&
97-
(ctxt >> INLINE_SIZES[CTXT_INDEX]) == 0 {
98-
(base << INLINE_OFFSETS[BASE_INDEX]) | (len << INLINE_OFFSETS[LEN_INDEX]) |
99-
(ctxt << INLINE_OFFSETS[CTXT_INDEX]) | TAG_INLINE
100-
} else {
101-
let index = with_span_interner(|interner| interner.intern(sd));
102-
(index << INTERNED_INDEX_OFFSET) | TAG_INTERNED
103-
};
104-
Span(val)
105-
}
106-
107-
#[inline]
108-
fn decode(span: Span) -> SpanData {
109-
let val = span.0;
110-
111-
// Extract a field at position `pos` having size `size`.
112-
let extract = |pos: u32, size: u32| {
113-
let mask = ((!0u32) as u64 >> (32 - size)) as u32; // Can't shift u32 by 32
114-
(val >> pos) & mask
115-
};
116-
117-
let (base, len, ctxt) = if val & TAG_MASK == TAG_INLINE {(
118-
extract(INLINE_OFFSETS[BASE_INDEX], INLINE_SIZES[BASE_INDEX]),
119-
extract(INLINE_OFFSETS[LEN_INDEX], INLINE_SIZES[LEN_INDEX]),
120-
extract(INLINE_OFFSETS[CTXT_INDEX], INLINE_SIZES[CTXT_INDEX]),
121-
)} else {
122-
let index = extract(INTERNED_INDEX_OFFSET, INTERNED_INDEX_SIZE);
123-
return with_span_interner(|interner| *interner.get(index));
124-
};
125-
SpanData { lo: BytePos(base), hi: BytePos(base + len), ctxt: SyntaxContext::from_u32(ctxt) }
126-
}
127-
12888
#[derive(Default)]
12989
pub struct SpanInterner {
13090
spans: FxHashMap<SpanData, u32>,

0 commit comments

Comments
 (0)