peepmatic: Make the results of match operations a smaller and more cache friendly

This commit is contained in:
Nick Fitzgerald
2020-05-07 12:15:50 -07:00
parent 9a1f8038b7
commit 469104c4d3
14 changed files with 580 additions and 149 deletions

View File

@@ -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
}
}