Skip to content

Commit 54be7e1

Browse files
committed
Replace SmallVec with Vec for niche optimizations
Niche-filling optimization was improved in rust-lang/rust#94075, which allows PrefixEntry to use the niche in Vec, but not SmallVec.
1 parent 3a5f256 commit 54be7e1

File tree

2 files changed

+9
-40
lines changed

2 files changed

+9
-40
lines changed

src/syntax/prefix.rs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use std::collections::HashMap;
1010
use std::fmt::{self, Debug, Formatter};
1111

12-
use smallvec::{smallvec, SmallVec};
13-
1412
use crate::syntax::{TokenSeq, VariantIndex};
1513
use crate::text::EncodingError;
1614

@@ -23,20 +21,20 @@ pub struct PrefixTable<T, O> {
2321
#[derive(Clone, Debug)]
2422
pub enum PrefixEntry<O> {
2523
Terminal(O),
26-
Prefix(SmallVec<[O; 16]>),
24+
Prefix(Vec<O>),
2725
}
2826

2927
#[derive(Clone, Debug, PartialEq, Eq)]
3028
pub struct ConflictError<T: VariantIndex, O> {
3129
prefix: TokenSeq<T>,
32-
opcodes: SmallVec<[O; 16]>,
30+
opcodes: Vec<O>,
3331
}
3432

3533
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3634
pub enum PrefixError<T: VariantIndex, O> {
3735
EncodingError(EncodingError, TokenSeq<T>),
3836
UnknownOpcode(TokenSeq<T>),
39-
IncompleteOpcode(TokenSeq<T>, SmallVec<[O; 16]>),
37+
IncompleteOpcode(TokenSeq<T>, Vec<O>),
4038
}
4139

4240
impl<T, O> PrefixTable<T, O>
@@ -85,17 +83,17 @@ where
8583
let entry = self.get_mut(seq);
8684
match entry {
8785
Some(PrefixEntry::Terminal(terminal)) => {
88-
return Err(ConflictError::new(seq, smallvec![*terminal, opcode]));
86+
return Err(ConflictError::new(seq, vec![*terminal, opcode]));
8987
}
9088
Some(PrefixEntry::Prefix(opcodes)) => opcodes.push(opcode),
91-
None => *entry = Some(PrefixEntry::Prefix(smallvec![opcode])),
89+
None => *entry = Some(PrefixEntry::Prefix(vec![opcode])),
9290
}
9391
seq.push(tok);
9492
}
9593
let entry = self.get_mut(seq);
9694
match entry {
9795
Some(PrefixEntry::Terminal(terminal)) => {
98-
return Err(ConflictError::new(seq, smallvec![*terminal, opcode]));
96+
return Err(ConflictError::new(seq, vec![*terminal, opcode]));
9997
}
10098
Some(PrefixEntry::Prefix(opcodes)) => {
10199
let mut opcodes = opcodes.clone();
@@ -135,7 +133,7 @@ where
135133
let prefix = match self.get(seq) {
136134
Some(PrefixEntry::Terminal(opcode)) => return Some(Ok(*opcode)),
137135
Some(PrefixEntry::Prefix(opcodes)) => opcodes.clone(),
138-
None => SmallVec::new(),
136+
None => Vec::new(),
139137
};
140138
return Some(Err(PrefixError::IncompleteOpcode(seq, prefix)));
141139
}
@@ -200,7 +198,7 @@ where
200198
impl<T: VariantIndex, O> ConflictError<T, O> {
201199
#[inline]
202200
#[must_use]
203-
const fn new(prefix: TokenSeq<T>, opcodes: SmallVec<[O; 16]>) -> Self {
201+
const fn new(prefix: TokenSeq<T>, opcodes: Vec<O>) -> Self {
204202
ConflictError { prefix, opcodes }
205203
}
206204
}
@@ -211,31 +209,3 @@ pub trait Tokens {
211209
#[must_use]
212210
fn tokens(&self) -> &'static [Self::Token];
213211
}
214-
215-
#[cfg(test)]
216-
mod tests {
217-
use std::mem::size_of;
218-
219-
use static_assertions::{assert_eq_size, const_assert};
220-
221-
use super::*;
222-
use crate::ws::inst::Opcode;
223-
224-
#[test]
225-
fn optimal_size() {
226-
#[allow(dead_code)]
227-
enum PrefixEntryOf<T, P> {
228-
Terminal(T),
229-
Prefix(P),
230-
}
231-
232-
assert_eq_size!(
233-
PrefixEntry<Opcode>,
234-
Option<PrefixEntry<Opcode>>,
235-
PrefixEntryOf<Opcode, Vec<Opcode>>,
236-
PrefixEntryOf<Opcode, SmallVec<[Opcode; 16]>>,
237-
);
238-
assert_eq_size!(Vec<Opcode>, SmallVec<[Opcode; 16]>);
239-
const_assert!(size_of::<SmallVec<[Opcode; 16]>>() < size_of::<SmallVec<[Opcode; 17]>>());
240-
}
241-
}

src/ws/parse.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::iter::FusedIterator;
1010
use std::sync::LazyLock;
1111

1212
use bitvec::vec::BitVec;
13-
use smallvec::SmallVec;
1413

1514
use crate::syntax::{PrefixError, PrefixTable, TokenSeq, Tokens};
1615
use crate::text::EncodingError;
@@ -35,7 +34,7 @@ pub struct Parser<'a, L> {
3534
pub enum ParseError {
3635
EncodingError(EncodingError, Vec<Token>),
3736
UnknownOpcode(TokenSeq<Token>),
38-
IncompleteInst(TokenSeq<Token>, SmallVec<[Opcode; 16]>),
37+
IncompleteInst(TokenSeq<Token>, Vec<Opcode>),
3938
UnterminatedArg(Opcode, BitVec),
4039
}
4140

0 commit comments

Comments
 (0)