|
1 |
| -// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 |
| -// file at the top-level directory of this distribution and at |
3 |
| -// http://rust-lang.org/COPYRIGHT. |
4 |
| -// |
5 |
| -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
6 |
| -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
7 |
| -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
8 |
| -// option. This file may not be copied, modified, or distributed |
9 |
| -// except according to those terms. |
10 |
| - |
11 | 1 | // Spans are encoded using 1-bit tag and 2 different encoding formats (one for
|
12 | 2 | // each tag value). One format is used for keeping span data inline,
|
13 | 3 | // another contains index into an out-of-line span interner.
|
14 | 4 | // The encoding format for inline spans were obtained by optimizing over crates
|
15 | 5 | // in rustc/libstd. See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28
|
16 |
| -use super::hygiene::SyntaxContext; |
17 |
| -use crate::syntax_pos::{BytePos, SpanData, CM, GLOBALS}; |
| 6 | + |
| 7 | +use crate::{hygiene::SyntaxContext, syntax_pos::CM, BytePos, SpanData, GLOBALS}; |
18 | 8 | use hashbrown::HashMap;
|
19 | 9 | use serde::{
|
20 | 10 | de::Deserializer,
|
21 | 11 | ser::{SerializeStruct, Serializer},
|
22 | 12 | Deserialize, Serialize,
|
23 | 13 | };
|
24 |
| -use std::hash::{Hash, Hasher}; |
25 |
| - |
26 | 14 | /// A compressed span.
|
27 |
| -/// Contains either fields of `SpanData` inline if they are small, or index into |
28 |
| -/// span interner. The primary goal of `Span` is to be as small as possible and |
29 |
| -/// fit into other structures (that's why it uses `packed` as well). Decoding |
30 |
| -/// speed is the second priority. See `SpanData` for the info on span fields in |
31 |
| -/// decoded representation. |
32 |
| -#[repr(packed)] |
33 |
| -pub struct Span(u32); |
| 15 | +/// |
| 16 | +/// `SpanData` is 12 bytes, which is a bit too big to stick everywhere. `Span` |
| 17 | +/// is a form that only takes up 8 bytes, with less space for the length and |
| 18 | +/// context. The vast majority (99.9%+) of `SpanData` instances will fit within |
| 19 | +/// those 8 bytes; any `SpanData` whose fields don't fit into a `Span` are |
| 20 | +/// stored in a separate interner table, and the `Span` will index into that |
| 21 | +/// table. Interning is rare enough that the cost is low, but common enough |
| 22 | +/// that the code is exercised regularly. |
| 23 | +/// |
| 24 | +/// An earlier version of this code used only 4 bytes for `Span`, but that was |
| 25 | +/// slower because only 80--90% of spans could be stored inline (even less in |
| 26 | +/// very large crates) and so the interner was used a lot more. |
| 27 | +/// |
| 28 | +/// Inline (compressed) format: |
| 29 | +/// - `span.base_or_index == span_data.lo` |
| 30 | +/// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= |
| 31 | +/// MAX_LEN`) |
| 32 | +/// - `span.ctxt == span_data.ctxt` (must be `<= MAX_CTXT`) |
| 33 | +/// |
| 34 | +/// Interned format: |
| 35 | +/// - `span.base_or_index == index` (indexes into the interner table) |
| 36 | +/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero) |
| 37 | +/// - `span.ctxt == 0` |
| 38 | +/// |
| 39 | +/// The inline form uses 0 for the tag value (rather than 1) so that we don't |
| 40 | +/// need to mask out the tag bit when getting the length, and so that the |
| 41 | +/// dummy span can be all zeroes. |
| 42 | +/// |
| 43 | +/// Notes about the choice of field sizes: |
| 44 | +/// - `base` is 32 bits in both `Span` and `SpanData`, which means that `base` |
| 45 | +/// values never cause interning. The number of bits needed for `base` depends |
| 46 | +/// on the crate size. 32 bits allows up to 4 GiB of code in a crate. |
| 47 | +/// `script-servo` is the largest crate in `rustc-perf`, requiring 26 bits for |
| 48 | +/// some spans. |
| 49 | +/// - `len` is 15 bits in `Span` (a u16, minus 1 bit for the tag) and 32 bits in |
| 50 | +/// `SpanData`, which means that large `len` values will cause interning. The |
| 51 | +/// number of bits needed for `len` does not depend on the crate size. The |
| 52 | +/// most common number of bits for `len` are 0--7, with a peak usually at 3 or |
| 53 | +/// 4, and then it drops off quickly from 8 onwards. 15 bits is enough for |
| 54 | +/// 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur |
| 55 | +/// dozens of times in a typical crate. |
| 56 | +/// - `ctxt` is 16 bits in `Span` and 32 bits in `SpanData`, which means that |
| 57 | +/// large `ctxt` values will cause interning. The number of bits needed for |
| 58 | +/// `ctxt` values depend partly on the crate size and partly on the form of |
| 59 | +/// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt`, but |
| 60 | +/// larger crates might need more than 16 bits. |
| 61 | +#[derive(Clone, Copy, Eq, PartialEq, Hash)] |
| 62 | +pub struct Span { |
| 63 | + base_or_index: u32, |
| 64 | + len_or_tag: u16, |
| 65 | + ctxt_or_zero: u16, |
| 66 | +} |
| 67 | + |
| 68 | +const LEN_TAG: u16 = 0b1000_0000_0000_0000; |
| 69 | +const MAX_LEN: u32 = 0b0111_1111_1111_1111; |
| 70 | +const MAX_CTXT: u32 = 0b1111_1111_1111_1111; |
| 71 | + |
| 72 | +/// Dummy span, both position and length are zero, syntax context is zero as |
| 73 | +/// well. |
| 74 | +pub const DUMMY_SP: Span = Span { |
| 75 | + base_or_index: 0, |
| 76 | + len_or_tag: 0, |
| 77 | + ctxt_or_zero: 0, |
| 78 | +}; |
| 79 | + |
| 80 | +impl Span { |
| 81 | + #[inline] |
| 82 | + pub fn new(mut lo: BytePos, mut hi: BytePos, ctxt: SyntaxContext) -> Self { |
| 83 | + if lo > hi { |
| 84 | + std::mem::swap(&mut lo, &mut hi); |
| 85 | + } |
| 86 | + |
| 87 | + let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32()); |
| 88 | + |
| 89 | + if len <= MAX_LEN && ctxt2 <= MAX_CTXT { |
| 90 | + // Inline format. |
| 91 | + Span { |
| 92 | + base_or_index: base, |
| 93 | + len_or_tag: len as u16, |
| 94 | + ctxt_or_zero: ctxt2 as u16, |
| 95 | + } |
| 96 | + } else { |
| 97 | + // Interned format. |
| 98 | + let index = with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt })); |
| 99 | + Span { |
| 100 | + base_or_index: index, |
| 101 | + len_or_tag: LEN_TAG, |
| 102 | + ctxt_or_zero: 0, |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #[inline] |
| 108 | + pub fn data(self) -> SpanData { |
| 109 | + if self.len_or_tag != LEN_TAG { |
| 110 | + // Inline format. |
| 111 | + debug_assert!(self.len_or_tag as u32 <= MAX_LEN); |
| 112 | + SpanData { |
| 113 | + lo: BytePos(self.base_or_index), |
| 114 | + hi: BytePos(self.base_or_index + self.len_or_tag as u32), |
| 115 | + ctxt: SyntaxContext::from_u32(self.ctxt_or_zero as u32), |
| 116 | + } |
| 117 | + } else { |
| 118 | + // Interned format. |
| 119 | + debug_assert!(self.ctxt_or_zero == 0); |
| 120 | + let index = self.base_or_index; |
| 121 | + with_span_interner(|interner| *interner.get(index)) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[derive(Default)] |
| 127 | +pub struct SpanInterner { |
| 128 | + spans: HashMap<SpanData, u32>, |
| 129 | + span_data: Vec<SpanData>, |
| 130 | +} |
| 131 | + |
| 132 | +impl SpanInterner { |
| 133 | + fn intern(&mut self, span_data: &SpanData) -> u32 { |
| 134 | + if let Some(index) = self.spans.get(span_data) { |
| 135 | + return *index; |
| 136 | + } |
| 137 | + |
| 138 | + let index = self.spans.len() as u32; |
| 139 | + self.span_data.push(*span_data); |
| 140 | + self.spans.insert(*span_data, index); |
| 141 | + index |
| 142 | + } |
| 143 | + |
| 144 | + #[inline] |
| 145 | + fn get(&self, index: u32) -> &SpanData { |
| 146 | + &self.span_data[index as usize] |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// If an interner exists, return it. Otherwise, prepare a fresh one. |
| 151 | +#[inline] |
| 152 | +fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T { |
| 153 | + GLOBALS.with(|globals| f(&mut *globals.span_interner.lock())) |
| 154 | +} |
34 | 155 |
|
35 | 156 | #[derive(Serialize)]
|
36 | 157 | struct Loc {
|
@@ -90,153 +211,3 @@ impl<'de> Deserialize<'de> for Span {
|
90 | 211 | Ok(Span::new(data.lo, data.hi, data.ctxt))
|
91 | 212 | }
|
92 | 213 | }
|
93 |
| - |
94 |
| -impl Copy for Span {} |
95 |
| -impl Clone for Span { |
96 |
| - #[inline] |
97 |
| - fn clone(&self) -> Span { |
98 |
| - *self |
99 |
| - } |
100 |
| -} |
101 |
| -impl PartialEq for Span { |
102 |
| - #[inline] |
103 |
| - fn eq(&self, other: &Span) -> bool { |
104 |
| - let a = self.0; |
105 |
| - let b = other.0; |
106 |
| - a == b |
107 |
| - } |
108 |
| -} |
109 |
| -impl Eq for Span {} |
110 |
| -impl Hash for Span { |
111 |
| - #[inline] |
112 |
| - fn hash<H: Hasher>(&self, state: &mut H) { |
113 |
| - let a = self.0; |
114 |
| - a.hash(state) |
115 |
| - } |
116 |
| -} |
117 |
| - |
118 |
| -/// Dummy span, both position and length are zero, syntax context is zero as |
119 |
| -/// well. This span is kept inline and encoded with format 0. |
120 |
| -pub const DUMMY_SP: Span = Span(0); |
121 |
| - |
122 |
| -impl Span { |
123 |
| - #[inline] |
124 |
| - pub fn new(lo: BytePos, hi: BytePos, ctxt: SyntaxContext) -> Self { |
125 |
| - encode(&if lo <= hi { |
126 |
| - SpanData { lo, hi, ctxt } |
127 |
| - } else { |
128 |
| - SpanData { |
129 |
| - lo: hi, |
130 |
| - hi: lo, |
131 |
| - ctxt, |
132 |
| - } |
133 |
| - }) |
134 |
| - } |
135 |
| - |
136 |
| - #[inline] |
137 |
| - pub fn data(self) -> SpanData { |
138 |
| - decode(self) |
139 |
| - } |
140 |
| -} |
141 |
| - |
142 |
| -// Tags |
143 |
| -const TAG_INLINE: u32 = 0; |
144 |
| -const TAG_INTERNED: u32 = 1; |
145 |
| -const TAG_MASK: u32 = 1; |
146 |
| - |
147 |
| -// Fields indexes |
148 |
| -const BASE_INDEX: usize = 0; |
149 |
| -const LEN_INDEX: usize = 1; |
150 |
| -const CTXT_INDEX: usize = 2; |
151 |
| - |
152 |
| -// Tag = 0, inline format. |
153 |
| -// ------------------------------------------------------------- |
154 |
| -// | base 31:8 | len 7:1 | ctxt (currently 0 bits) | tag 0:0 | |
155 |
| -// ------------------------------------------------------------- |
156 |
| -// Since there are zero bits for ctxt, only SpanData with a 0 SyntaxContext |
157 |
| -// can be inline. |
158 |
| -const INLINE_SIZES: [u32; 3] = [24, 7, 0]; |
159 |
| -const INLINE_OFFSETS: [u32; 3] = [8, 1, 1]; |
160 |
| - |
161 |
| -// Tag = 1, interned format. |
162 |
| -// ------------------------ |
163 |
| -// | index 31:1 | tag 0:0 | |
164 |
| -// ------------------------ |
165 |
| -const INTERNED_INDEX_SIZE: u32 = 31; |
166 |
| -const INTERNED_INDEX_OFFSET: u32 = 1; |
167 |
| - |
168 |
| -#[inline] |
169 |
| -fn encode(sd: &SpanData) -> Span { |
170 |
| - let (base, len, ctxt) = (sd.lo.0, sd.hi.0 - sd.lo.0, sd.ctxt.as_u32()); |
171 |
| - |
172 |
| - let val = if (base >> INLINE_SIZES[BASE_INDEX]) == 0 |
173 |
| - && (len >> INLINE_SIZES[LEN_INDEX]) == 0 |
174 |
| - && (ctxt >> INLINE_SIZES[CTXT_INDEX]) == 0 |
175 |
| - { |
176 |
| - (base << INLINE_OFFSETS[BASE_INDEX]) |
177 |
| - | (len << INLINE_OFFSETS[LEN_INDEX]) |
178 |
| - | (ctxt << INLINE_OFFSETS[CTXT_INDEX]) |
179 |
| - | TAG_INLINE |
180 |
| - } else { |
181 |
| - let index = with_span_interner(|interner| interner.intern(sd)); |
182 |
| - (index << INTERNED_INDEX_OFFSET) | TAG_INTERNED |
183 |
| - }; |
184 |
| - Span(val) |
185 |
| -} |
186 |
| - |
187 |
| -#[inline] |
188 |
| -fn decode(span: Span) -> SpanData { |
189 |
| - let val = span.0; |
190 |
| - |
191 |
| - // Extract a field at position `pos` having size `size`. |
192 |
| - let extract = |pos: u32, size: u32| { |
193 |
| - let mask = ((!0u32) as u64 >> (32 - size)) as u32; // Can't shift u32 by 32 |
194 |
| - (val >> pos) & mask |
195 |
| - }; |
196 |
| - |
197 |
| - let (base, len, ctxt) = if val & TAG_MASK == TAG_INLINE { |
198 |
| - ( |
199 |
| - extract(INLINE_OFFSETS[BASE_INDEX], INLINE_SIZES[BASE_INDEX]), |
200 |
| - extract(INLINE_OFFSETS[LEN_INDEX], INLINE_SIZES[LEN_INDEX]), |
201 |
| - extract(INLINE_OFFSETS[CTXT_INDEX], INLINE_SIZES[CTXT_INDEX]), |
202 |
| - ) |
203 |
| - } else { |
204 |
| - let index = extract(INTERNED_INDEX_OFFSET, INTERNED_INDEX_SIZE); |
205 |
| - return with_span_interner(|interner| *interner.get(index)); |
206 |
| - }; |
207 |
| - SpanData { |
208 |
| - lo: BytePos(base), |
209 |
| - hi: BytePos(base + len), |
210 |
| - ctxt: SyntaxContext::from_u32(ctxt), |
211 |
| - } |
212 |
| -} |
213 |
| - |
214 |
| -#[derive(Default)] |
215 |
| -pub struct SpanInterner { |
216 |
| - spans: HashMap<SpanData, u32>, |
217 |
| - span_data: Vec<SpanData>, |
218 |
| -} |
219 |
| - |
220 |
| -impl SpanInterner { |
221 |
| - fn intern(&mut self, span_data: &SpanData) -> u32 { |
222 |
| - if let Some(index) = self.spans.get(span_data) { |
223 |
| - return *index; |
224 |
| - } |
225 |
| - |
226 |
| - let index = self.spans.len() as u32; |
227 |
| - self.span_data.push(*span_data); |
228 |
| - self.spans.insert(*span_data, index); |
229 |
| - index |
230 |
| - } |
231 |
| - |
232 |
| - #[inline] |
233 |
| - fn get(&self, index: u32) -> &SpanData { |
234 |
| - &self.span_data[index as usize] |
235 |
| - } |
236 |
| -} |
237 |
| - |
238 |
| -// If an interner exists, return it. Otherwise, prepare a fresh one. |
239 |
| -#[inline] |
240 |
| -fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T { |
241 |
| - GLOBALS.with(|globals| f(&mut *globals.span_interner.lock())) |
242 |
| -} |
0 commit comments