peepmatic: Make the results of match operations a smaller and more cache friendly
This commit is contained in:
@@ -7,11 +7,11 @@
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryInto;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
/// An identifier for an interned integer.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct IntegerId(#[doc(hidden)] pub u32);
|
||||
pub struct IntegerId(#[doc(hidden)] pub NonZeroU32);
|
||||
|
||||
/// An interner for integer values.
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
@@ -40,7 +40,8 @@ impl IntegerInterner {
|
||||
return *id;
|
||||
}
|
||||
|
||||
let id = IntegerId(self.values.len().try_into().unwrap());
|
||||
assert!((self.values.len() as u64) < (std::u32::MAX as u64));
|
||||
let id = IntegerId(unsafe { NonZeroU32::new_unchecked(self.values.len() as u32 + 1) });
|
||||
|
||||
self.values.push(value);
|
||||
self.map.insert(value, id);
|
||||
@@ -59,13 +60,21 @@ impl IntegerInterner {
|
||||
/// Lookup a previously interned integer by id.
|
||||
#[inline]
|
||||
pub fn lookup(&self, id: IntegerId) -> u64 {
|
||||
self.values[id.0 as usize]
|
||||
let index = id.0.get() as usize - 1;
|
||||
self.values[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntegerId> for u32 {
|
||||
#[inline]
|
||||
fn from(id: IntegerId) -> u32 {
|
||||
id.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntegerId> for NonZeroU32 {
|
||||
#[inline]
|
||||
fn from(id: IntegerId) -> NonZeroU32 {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user