-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #44646 - petrochenkov:scompress, r=<try>
Compress most of spans to 32 bits As described in https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28 Closes #15594 r? @michaelwoerister
- Loading branch information
Showing
6 changed files
with
185 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Spans are encoded using 2-bit tag and 4 different encoding formats for each tag. | ||
// Three formats are used for keeping span data inline, | ||
// the fourth one contains index into out-of-line span interner. | ||
// The encoding formats for inline spans were obtained by optimizing over crates in rustc/libstd. | ||
// See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28 | ||
|
||
use super::*; | ||
|
||
/// A compressed span. | ||
/// Contains either fields of `SpanData` inline if they are small, or index into span interner. | ||
/// The primary goal of `Span` is to be as small as possible and fit into other structures | ||
/// (that's why it uses `packed` as well). Decoding speed is the second priority. | ||
/// See `SpanData` for the info on span fields in decoded representation. | ||
#[derive(Clone, Copy, PartialEq, Eq, Hash)] | ||
#[repr(packed)] | ||
pub struct Span(u64); | ||
|
||
/// Dummy span, both position and length are zero, syntax context is zero as well. | ||
/// This span is kept inline and encoded with format 0. | ||
pub const DUMMY_SP: Span = Span(0); | ||
|
||
impl Span { | ||
#[inline] | ||
pub fn new(lo: BytePos, hi: BytePos, ctxt: SyntaxContext) -> Self { | ||
encode(&match lo <= hi { | ||
true => SpanData { lo, hi, ctxt }, | ||
false => SpanData { lo: hi, hi: lo, ctxt }, | ||
}) | ||
} | ||
|
||
#[inline] | ||
pub fn data(self) -> SpanData { | ||
decode(self) | ||
} | ||
} | ||
|
||
// Tags | ||
const TAG_INLINE: u64 = 0; | ||
const TAG_INTERNED: u64 = 1; | ||
const TAG_MASK: u64 = 1; | ||
|
||
// Fields indexes | ||
const BASE_INDEX: usize = 0; | ||
const LEN_INDEX: usize = 1; | ||
const CTXT_INDEX: usize = 2; | ||
|
||
// Tag = 0, inline format. | ||
// ----------------------------------- | ||
// | base 63:39 | len 38:15 | ctxt 14:1 | tag 0:0 | | ||
// ----------------------------------- | ||
const INLINE_SIZES: [u64; 3] = [25, 24, 14]; | ||
const INLINE_OFFSETS: [u64; 3] = [39, 15, 1]; | ||
|
||
// Tag = 1, interned format. | ||
// ------------------------ | ||
// | index 63:1 | tag 0:0 | | ||
// ------------------------ | ||
const INTERNED_INDEX_SIZE: u64 = 63; | ||
const INTERNED_INDEX_OFFSET: u64 = 1; | ||
|
||
fn encode(sd: &SpanData) -> Span { | ||
let (base, len, ctxt) = (sd.lo.0 as u64, (sd.hi.0 - sd.lo.0) as u64, sd.ctxt.0 as u64); | ||
|
||
let val = if (base >> INLINE_SIZES[BASE_INDEX]) == 0 && | ||
(len >> INLINE_SIZES[LEN_INDEX]) == 0 && | ||
(ctxt >> INLINE_SIZES[CTXT_INDEX]) == 0 { | ||
(base << INLINE_OFFSETS[BASE_INDEX]) | (len << INLINE_OFFSETS[LEN_INDEX]) | | ||
(ctxt << INLINE_OFFSETS[CTXT_INDEX]) | TAG_INLINE | ||
} else { | ||
let index = with_span_interner(|interner| interner.intern(sd)) as u64; | ||
(index << INTERNED_INDEX_OFFSET) | TAG_INTERNED | ||
}; | ||
Span(val) | ||
} | ||
|
||
fn decode(span: Span) -> SpanData { | ||
let val = span.0 as u64; | ||
|
||
// Extract a field at position `pos` having size `size`. | ||
let extract = |pos: u64, size: u64| { | ||
let mask = (!0u64) >> (64 - size); | ||
(val >> pos) & mask | ||
}; | ||
|
||
let (base, len, ctxt) = if val & TAG_MASK == TAG_INLINE {( | ||
extract(INLINE_OFFSETS[BASE_INDEX], INLINE_SIZES[BASE_INDEX]), | ||
extract(INLINE_OFFSETS[LEN_INDEX], INLINE_SIZES[LEN_INDEX]), | ||
extract(INLINE_OFFSETS[CTXT_INDEX], INLINE_SIZES[CTXT_INDEX]), | ||
)} else { | ||
let index = extract(INTERNED_INDEX_OFFSET, INTERNED_INDEX_SIZE); | ||
return with_span_interner(|interner| *interner.get(index as u32)); | ||
}; | ||
SpanData { | ||
lo: BytePos(base as u32), | ||
hi: BytePos((base + len) as u32), | ||
ctxt: SyntaxContext(ctxt as u32) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct SpanInterner { | ||
spans: HashMap<SpanData, u32>, | ||
span_data: Vec<SpanData>, | ||
} | ||
|
||
impl SpanInterner { | ||
fn intern(&mut self, span_data: &SpanData) -> u32 { | ||
if let Some(index) = self.spans.get(span_data) { | ||
return *index; | ||
} | ||
|
||
let index = self.spans.len() as u32; | ||
self.span_data.push(*span_data); | ||
self.spans.insert(*span_data, index); | ||
index | ||
} | ||
|
||
fn get(&self, index: u32) -> &SpanData { | ||
&self.span_data[index as usize] | ||
} | ||
} | ||
|
||
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one. | ||
fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T { | ||
thread_local!(static INTERNER: RefCell<SpanInterner> = { | ||
RefCell::new(SpanInterner::default()) | ||
}); | ||
INTERNER.with(|interner| f(&mut *interner.borrow_mut())) | ||
} |