Skip to content

Commit c10b23e

Browse files
committedSep 24, 2017
encode region::Scope using fewer bytes
Now that region::Scope is no longer interned, its size is more important. This PR encodes region::Scope in 8 bytes instead of 12, which should speed up region inference somewhat (perf testing needed) and should improve the margins on #36799 by 64MB (that's not a lot, I did this PR mostly to speed up region inference).
1 parent 8214ab1 commit c10b23e

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed
 

‎src/librustc/ich/impls_ty.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -515,22 +515,7 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
515515
});
516516

517517
impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { idx });
518-
519-
impl<'gcx> HashStable<StableHashingContext<'gcx>> for ::middle::region::Scope {
520-
fn hash_stable<W: StableHasherResult>(&self,
521-
hcx: &mut StableHashingContext<'gcx>,
522-
hasher: &mut StableHasher<W>) {
523-
self.data().hash_stable(hcx, hasher)
524-
}
525-
}
526-
527-
impl_stable_hash_for!(enum ::middle::region::ScopeData {
528-
Node(local_id),
529-
Destruction(local_id),
530-
CallSite(local_id),
531-
Arguments(local_id),
532-
Remainder(block_remainder)
533-
});
518+
impl_stable_hash_for!(struct ::middle::region::Scope { id, code });
534519

535520
impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for region::Scope {
536521
type KeyType = region::Scope;

‎src/librustc/middle/region.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,16 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
9898
/// generated via deriving here.
9999
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
100100
pub struct Scope {
101-
pub scope_data: ScopeData
101+
pub(crate) id: hir::ItemLocalId,
102+
pub(crate) code: u32
102103
}
103104

105+
const SCOPE_DATA_NODE: u32 = !0;
106+
const SCOPE_DATA_CALLSITE: u32 = !1;
107+
const SCOPE_DATA_ARGUMENTS: u32 = !2;
108+
const SCOPE_DATA_DESTRUCTION: u32 = !3;
109+
const SCOPE_DATA_REMAINDER_MAX: u32 = !4;
110+
104111
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
105112
pub enum ScopeData {
106113
Node(hir::ItemLocalId),
@@ -148,11 +155,9 @@ pub struct BlockRemainder {
148155
RustcDecodable, Debug, Copy)]
149156
pub struct FirstStatementIndex { pub idx: u32 }
150157

151-
pub const FIRST_STATEMENT_MAX: usize = !0u32 as usize;
152-
153158
impl Idx for FirstStatementIndex {
154159
fn new(idx: usize) -> Self {
155-
assert!(idx <= FIRST_STATEMENT_MAX);
160+
assert!(idx <= SCOPE_DATA_REMAINDER_MAX as usize);
156161
FirstStatementIndex { idx: idx as u32 }
157162
}
158163

@@ -164,15 +169,31 @@ impl Idx for FirstStatementIndex {
164169
impl From<ScopeData> for Scope {
165170
#[inline]
166171
fn from(scope_data: ScopeData) -> Self {
167-
Self { scope_data }
172+
let (id, code) = match scope_data {
173+
ScopeData::Node(id) => (id, SCOPE_DATA_NODE),
174+
ScopeData::CallSite(id) => (id, SCOPE_DATA_CALLSITE),
175+
ScopeData::Arguments(id) => (id, SCOPE_DATA_ARGUMENTS),
176+
ScopeData::Destruction(id) => (id, SCOPE_DATA_DESTRUCTION),
177+
ScopeData::Remainder(r) => (r.block, r.first_statement_index.index() as u32)
178+
};
179+
Self { id, code }
168180
}
169181
}
170182

171183
#[allow(non_snake_case)]
172184
impl Scope {
173185
#[inline]
174186
pub fn data(self) -> ScopeData {
175-
self.scope_data
187+
match self.code {
188+
SCOPE_DATA_NODE => ScopeData::Node(self.id),
189+
SCOPE_DATA_CALLSITE => ScopeData::CallSite(self.id),
190+
SCOPE_DATA_ARGUMENTS => ScopeData::Arguments(self.id),
191+
SCOPE_DATA_DESTRUCTION => ScopeData::Destruction(self.id),
192+
idx => ScopeData::Remainder(BlockRemainder {
193+
block: self.id,
194+
first_statement_index: FirstStatementIndex { idx }
195+
})
196+
}
176197
}
177198

178199
#[inline]
@@ -207,17 +228,7 @@ impl Scope {
207228
/// NB: likely to be replaced as API is refined; e.g. pnkfelix
208229
/// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
209230
pub fn item_local_id(&self) -> hir::ItemLocalId {
210-
// TODO: killme
211-
match self.data() {
212-
ScopeData::Node(id) => id,
213-
214-
// These cases all return rough approximations to the
215-
// precise scope denoted by `self`.
216-
ScopeData::Remainder(br) => br.block,
217-
ScopeData::Destruction(id) |
218-
ScopeData::CallSite(id) |
219-
ScopeData::Arguments(id) => id,
220-
}
231+
self.id
221232
}
222233

223234
pub fn node_id(&self, tcx: TyCtxt, scope_tree: &ScopeTree) -> ast::NodeId {

0 commit comments

Comments
 (0)
Please sign in to comment.