perf(header): optimize when inserting a new type header

This commit is contained in:
Sean McArthur
2017-06-23 18:49:18 -07:00
parent 7369fe649f
commit 7d122bd15f
3 changed files with 59 additions and 8 deletions

View File

@@ -58,6 +58,11 @@ impl<V: ?Sized + Any + 'static> PtrMapCell<V> {
PtrMapCell(UnsafeCell::new(PtrMap::Empty))
}
#[inline]
pub fn with_one(key: TypeId, val: Box<V>) -> PtrMapCell<V> {
PtrMapCell(UnsafeCell::new(PtrMap::One(key, val)))
}
#[inline]
pub fn get(&self, key: TypeId) -> Option<&V> {
let map = unsafe { &*self.0.get() };

View File

@@ -23,12 +23,11 @@ impl Item {
}
#[inline]
pub fn new_typed(ty: Box<Header + Send + Sync>) -> Item {
let map = PtrMapCell::new();
unsafe { map.insert((*ty).get_type(), ty); }
pub fn new_typed<H: Header>(val: H) -> Item {
Item {
raw: OptCell::new(None),
typed: map,
typed: PtrMapCell::with_one(TypeId::of::<H>(), Box::new(val)),
}
}