Skip to content

Commit 89779ca

Browse files
committed
internal: improve TokenSet implementation
1 parent 5dbe3fe commit 89779ca

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

crates/parser/src/token_set.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,46 @@ use crate::SyntaxKind;
44

55
/// A bit-set of `SyntaxKind`s
66
#[derive(Clone, Copy)]
7-
pub(crate) struct TokenSet(u128);
7+
pub(crate) struct TokenSet([u64; 3]);
8+
9+
const LAST_TOKEN_KIND_DISCRIMINANT: usize = SyntaxKind::SHEBANG as usize;
810

911
impl TokenSet {
10-
pub(crate) const EMPTY: TokenSet = TokenSet(0);
12+
pub(crate) const EMPTY: TokenSet = TokenSet([0; 3]);
1113

1214
pub(crate) const fn new(kinds: &[SyntaxKind]) -> TokenSet {
13-
let mut res = 0u128;
15+
let mut res = [0; 3];
1416
let mut i = 0;
1517
while i < kinds.len() {
16-
res |= mask(kinds[i]);
18+
let kind = kinds[i];
19+
debug_assert!(
20+
kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT,
21+
"Expected a token `SyntaxKind`"
22+
);
23+
let idx = kind as usize / 64;
24+
res[idx] |= mask(kind);
1725
i += 1;
1826
}
1927
TokenSet(res)
2028
}
2129

2230
pub(crate) const fn union(self, other: TokenSet) -> TokenSet {
23-
TokenSet(self.0 | other.0)
31+
TokenSet([self.0[0] | other.0[0], self.0[1] | other.0[1], self.0[2] | other.0[2]])
2432
}
2533

2634
pub(crate) const fn contains(&self, kind: SyntaxKind) -> bool {
27-
self.0 & mask(kind) != 0
35+
debug_assert!(
36+
kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT,
37+
"Expected a token `SyntaxKind`"
38+
);
39+
let idx = kind as usize / 64;
40+
self.0[idx] & mask(kind) != 0
2841
}
2942
}
3043

31-
const fn mask(kind: SyntaxKind) -> u128 {
32-
1u128 << (kind as usize)
44+
const fn mask(kind: SyntaxKind) -> u64 {
45+
debug_assert!(kind as usize <= LAST_TOKEN_KIND_DISCRIMINANT, "Expected a token `SyntaxKind`");
46+
1 << (kind as usize % 64)
3347
}
3448

3549
#[test]

0 commit comments

Comments
 (0)