Skip to content

Commit 1d64b24

Browse files
Switch syntax attribute tracking to BitVector
1 parent 8c069ce commit 1d64b24

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

src/libsyntax/ast.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use ext::hygiene::{Mark, SyntaxContext};
2323
use print::pprust;
2424
use ptr::P;
2525
use rustc_data_structures::indexed_vec;
26+
use rustc_data_structures::indexed_vec::Idx;
2627
use symbol::{Symbol, keywords};
2728
use tokenstream::{ThinTokenStream, TokenStream};
2829

@@ -1910,9 +1911,18 @@ pub enum AttrStyle {
19101911
Inner,
19111912
}
19121913

1913-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1914+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialOrd, Ord, Copy)]
19141915
pub struct AttrId(pub usize);
19151916

1917+
impl Idx for AttrId {
1918+
fn new(idx: usize) -> Self {
1919+
AttrId(idx)
1920+
}
1921+
fn index(self) -> usize {
1922+
self.0
1923+
}
1924+
}
1925+
19161926
/// Meta-data associated with an item
19171927
/// Doc-comments are promoted to attributes that have is_sugared_doc = true
19181928
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]

src/libsyntax/attr/mod.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,51 +41,27 @@ use std::iter;
4141

4242
pub fn mark_used(attr: &Attribute) {
4343
debug!("Marking {:?} as used.", attr);
44-
let AttrId(id) = attr.id;
4544
GLOBALS.with(|globals| {
46-
let mut slot = globals.used_attrs.lock();
47-
let idx = (id / 64) as usize;
48-
let shift = id % 64;
49-
if slot.len() <= idx {
50-
slot.resize(idx + 1, 0);
51-
}
52-
slot[idx] |= 1 << shift;
45+
globals.used_attrs.lock().insert(attr.id);
5346
});
5447
}
5548

5649
pub fn is_used(attr: &Attribute) -> bool {
57-
let AttrId(id) = attr.id;
5850
GLOBALS.with(|globals| {
59-
let slot = globals.used_attrs.lock();
60-
let idx = (id / 64) as usize;
61-
let shift = id % 64;
62-
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
63-
.unwrap_or(false)
51+
globals.used_attrs.lock().contains(attr.id)
6452
})
6553
}
6654

6755
pub fn mark_known(attr: &Attribute) {
6856
debug!("Marking {:?} as known.", attr);
69-
let AttrId(id) = attr.id;
7057
GLOBALS.with(|globals| {
71-
let mut slot = globals.known_attrs.lock();
72-
let idx = (id / 64) as usize;
73-
let shift = id % 64;
74-
if slot.len() <= idx {
75-
slot.resize(idx + 1, 0);
76-
}
77-
slot[idx] |= 1 << shift;
58+
globals.known_attrs.lock().insert(attr.id);
7859
});
7960
}
8061

8162
pub fn is_known(attr: &Attribute) -> bool {
82-
let AttrId(id) = attr.id;
8363
GLOBALS.with(|globals| {
84-
let slot = globals.known_attrs.lock();
85-
let idx = (id / 64) as usize;
86-
let shift = id % 64;
87-
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
88-
.unwrap_or(false)
64+
globals.known_attrs.lock().contains(attr.id)
8965
})
9066
}
9167

src/libsyntax/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ extern crate rustc_target;
4343
extern crate serialize as rustc_serialize; // used by deriving
4444

4545
use rustc_data_structures::sync::Lock;
46+
use rustc_data_structures::bitvec::BitVector;
47+
use ast::AttrId;
4648

4749
// A variant of 'try!' that panics on an Err. This is used as a crutch on the
4850
// way towards a non-panic!-prone parser. It should be used for fatal parsing
@@ -75,16 +77,18 @@ macro_rules! unwrap_or {
7577
}
7678

7779
pub struct Globals {
78-
used_attrs: Lock<Vec<u64>>,
79-
known_attrs: Lock<Vec<u64>>,
80+
used_attrs: Lock<BitVector<AttrId>>,
81+
known_attrs: Lock<BitVector<AttrId>>,
8082
syntax_pos_globals: syntax_pos::Globals,
8183
}
8284

8385
impl Globals {
8486
fn new() -> Globals {
8587
Globals {
86-
used_attrs: Lock::new(Vec::new()),
87-
known_attrs: Lock::new(Vec::new()),
88+
// We have no idea how many attributes their will be, so just
89+
// initiate the vectors with 0 bits. We'll grow them as necessary.
90+
used_attrs: Lock::new(BitVector::new(0)),
91+
known_attrs: Lock::new(BitVector::new(0)),
8892
syntax_pos_globals: syntax_pos::Globals::new(),
8993
}
9094
}

0 commit comments

Comments
 (0)