Skip to content

Commit 39a0969

Browse files
committed
Make NodeId a newtype_index to enable niche optimizations
1 parent d1d79ae commit 39a0969

File tree

7 files changed

+26
-46
lines changed

7 files changed

+26
-46
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use hir::map::{self, Map};
4949
use super::itemlikevisit::DeepVisitor;
5050

5151
use std::cmp;
52-
use std::u32;
5352

5453
#[derive(Copy, Clone)]
5554
pub enum FnKind<'a> {
@@ -1152,8 +1151,8 @@ pub struct IdRange {
11521151
impl IdRange {
11531152
pub fn max() -> IdRange {
11541153
IdRange {
1155-
min: NodeId::from_u32(u32::MAX),
1156-
max: NodeId::from_u32(u32::MIN),
1154+
min: NodeId::MAX,
1155+
max: NodeId::from_u32(0),
11571156
}
11581157
}
11591158

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
124124
.enumerate()
125125
.find(|&(_, &entry)| hir_id == entry)
126126
.expect("no node_to_hir_id entry");
127-
let node_id = NodeId::new(node_id);
127+
let node_id = NodeId::from_usize(node_id);
128128
missing_items.push(format!("[local_id: {}, node:{}]",
129129
local_id,
130130
self.hir_map.node_to_string(node_id)));

src/librustc/session/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Session {
393393

394394
match id.as_usize().checked_add(count) {
395395
Some(next) => {
396-
self.next_node_id.set(ast::NodeId::new(next));
396+
self.next_node_id.set(ast::NodeId::from_usize(next));
397397
}
398398
None => bug!("Input too large, ran out of node ids!"),
399399
}
@@ -1160,7 +1160,7 @@ pub fn build_session_(
11601160
recursion_limit: Once::new(),
11611161
type_length_limit: Once::new(),
11621162
const_eval_stack_frame_limit: 100,
1163-
next_node_id: OneThread::new(Cell::new(NodeId::new(1))),
1163+
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
11641164
allocator_kind: Once::new(),
11651165
injected_panic_runtime: Once::new(),
11661166
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ impl FromStr for UserIdentifiedItem {
566566
type Err = ();
567567
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
568568
Ok(s.parse()
569-
.map(ast::NodeId::new)
569+
.map(ast::NodeId::from_u32)
570570
.map(ItemViaNode)
571571
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
572572
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
663663
let mut errors = false;
664664
let mut seen_spans = FxHashSet::default();
665665
let mut error_vec = Vec::new();
666-
let mut prev_root_id: NodeId = NodeId::new(0);
666+
let mut prev_root_id: NodeId = NodeId::from_u32(0);
667667
for i in 0 .. self.determined_imports.len() {
668668
let import = self.determined_imports[i];
669669
let error = self.finalize_import(import);

src/libsyntax/ast.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub use util::parser::ExprPrecedence;
1818
use ext::hygiene::{Mark, SyntaxContext};
1919
use print::pprust;
2020
use ptr::P;
21-
use rustc_data_structures::indexed_vec;
2221
use rustc_data_structures::indexed_vec::Idx;
2322
use rustc_target::spec::abi::Abi;
2423
use source_map::{dummy_spanned, respan, Spanned};
@@ -31,7 +30,6 @@ use rustc_data_structures::fx::FxHashSet;
3130
use rustc_data_structures::sync::Lrc;
3231
use serialize::{self, Decoder, Encoder};
3332
use std::fmt;
34-
use std::u32;
3533

3634
pub use rustc_target::abi::FloatTy;
3735

@@ -213,71 +211,53 @@ pub struct ParenthesisedArgs {
213211
pub output: Option<P<Ty>>,
214212
}
215213

216-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
217-
pub struct NodeId(u32);
218-
219-
impl NodeId {
220-
pub fn new(x: usize) -> NodeId {
221-
assert!(x < (u32::MAX as usize));
222-
NodeId(x as u32)
223-
}
224-
225-
pub fn from_u32(x: u32) -> NodeId {
226-
NodeId(x)
227-
}
228-
229-
pub fn as_usize(&self) -> usize {
230-
self.0 as usize
214+
// hack to ensure that we don't try to access the private parts of `NodeId` in this module
215+
mod node_id_inner {
216+
use rustc_data_structures::indexed_vec::Idx;
217+
newtype_index! {
218+
pub struct NodeId {
219+
ENCODABLE = custom
220+
}
231221
}
222+
}
232223

233-
pub fn as_u32(&self) -> u32 {
234-
self.0
235-
}
224+
pub use self::node_id_inner::NodeId;
236225

226+
impl NodeId {
237227
pub fn placeholder_from_mark(mark: Mark) -> Self {
238-
NodeId(mark.as_u32())
228+
NodeId::from_u32(mark.as_u32())
239229
}
240230

241231
pub fn placeholder_to_mark(self) -> Mark {
242-
Mark::from_u32(self.0)
232+
Mark::from_u32(self.as_u32())
243233
}
244234
}
245235

246236
impl fmt::Display for NodeId {
247237
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248-
fmt::Display::fmt(&self.0, f)
238+
fmt::Display::fmt(&self.as_u32(), f)
249239
}
250240
}
251241

252242
impl serialize::UseSpecializedEncodable for NodeId {
253243
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
254-
s.emit_u32(self.0)
244+
s.emit_u32(self.as_u32())
255245
}
256246
}
257247

258248
impl serialize::UseSpecializedDecodable for NodeId {
259249
fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
260-
d.read_u32().map(NodeId)
261-
}
262-
}
263-
264-
impl indexed_vec::Idx for NodeId {
265-
fn new(idx: usize) -> Self {
266-
NodeId::new(idx)
267-
}
268-
269-
fn index(self) -> usize {
270-
self.as_usize()
250+
d.read_u32().map(NodeId::from_u32)
271251
}
272252
}
273253

274254
/// Node id used to represent the root of the crate.
275-
pub const CRATE_NODE_ID: NodeId = NodeId(0);
255+
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0);
276256

277257
/// When parsing and doing expansions, we initially give all AST nodes this AST
278258
/// node value. Then later, in the renumber pass, we renumber them to have
279259
/// small, positive ids.
280-
pub const DUMMY_NODE_ID: NodeId = NodeId(!0);
260+
pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
281261

282262
/// A modifier on a bound, currently this is only used for `?Sized`, where the
283263
/// modifier is `Maybe`. Negative bounds should also be handled here.

src/libsyntax/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(rustc_diagnostic_macros)]
2727
#![feature(slice_sort_by_cached_key)]
2828
#![feature(str_escape)]
29+
#![feature(step_trait)]
2930
#![feature(try_trait)]
3031
#![feature(unicode_internals)]
3132

@@ -37,7 +38,7 @@ extern crate serialize;
3738
#[macro_use] extern crate log;
3839
pub extern crate rustc_errors as errors;
3940
extern crate syntax_pos;
40-
extern crate rustc_data_structures;
41+
#[macro_use] extern crate rustc_data_structures;
4142
extern crate rustc_target;
4243
#[macro_use] extern crate scoped_tls;
4344
#[macro_use]

0 commit comments

Comments
 (0)