Optimize Scope layout.

This commit is contained in:
Stephen Chung 2021-06-29 23:22:54 +08:00
parent fc349f67f8
commit 69352d74c2
2 changed files with 15 additions and 17 deletions

View File

@ -2206,7 +2206,7 @@ mod tests {
96 96
} }
); );
assert_eq!(size_of::<Scope>(), 160); assert_eq!(size_of::<Scope>(), 464);
assert_eq!(size_of::<LexError>(), 56); assert_eq!(size_of::<LexError>(), 56);
assert_eq!( assert_eq!(
size_of::<ParseError>(), size_of::<ParseError>(),

View File

@ -49,14 +49,14 @@ const SCOPE_ENTRIES_INLINED: usize = 8;
// look up a variable. Variable lookup is usually via direct indexing, by-passing the name altogether. // look up a variable. Variable lookup is usually via direct indexing, by-passing the name altogether.
// //
// Since [`Dynamic`] is reasonably small, packing it tightly improves cache locality when variables are accessed. // Since [`Dynamic`] is reasonably small, packing it tightly improves cache locality when variables are accessed.
//
// The alias is `Box`'ed because it occurs infrequently.
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub struct Scope<'a> { pub struct Scope<'a> {
/// Current value of the entry. /// Current value of the entry.
values: smallvec::SmallVec<[Dynamic; SCOPE_ENTRIES_INLINED]>, values: smallvec::SmallVec<[Dynamic; SCOPE_ENTRIES_INLINED]>,
/// (Name, aliases) of the entry. /// (Name, aliases) of the entry.
names: Vec<(Cow<'a, str>, Option<Box<StaticVec<Identifier>>>)>, names: smallvec::SmallVec<
[(Cow<'a, str>, Option<Box<StaticVec<Identifier>>>); SCOPE_ENTRIES_INLINED],
>,
} }
impl Default for Scope<'_> { impl Default for Scope<'_> {
@ -253,7 +253,7 @@ impl<'a> Scope<'a> {
access: AccessMode, access: AccessMode,
mut value: Dynamic, mut value: Dynamic,
) -> &mut Self { ) -> &mut Self {
self.names.push((name.into(), None)); self.names.push((name.into(), Default::default()));
value.set_access_mode(access); value.set_access_mode(access);
self.values.push(value.into()); self.values.push(value.into());
self self
@ -436,20 +436,18 @@ impl<'a> Scope<'a> {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self { pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self {
let entry = self let (_, aliases) = self
.names .names
.get_mut(index) .get_mut(index)
.expect("never fails unless the index is out of bounds"); .expect("never fails unless the index is out of bounds");
if entry.1.is_none() { match aliases {
// Initialize the alias list if it is empty. None => {
entry.1 = Some(Default::default()); let mut list = StaticVec::new();
} list.push(alias);
let list = entry *aliases = Some(list.into());
.1 }
.as_mut() Some(aliases) if !aliases.iter().any(|a| a == &alias) => aliases.push(alias),
.expect("never fails because the list is initialized"); Some(_) => (),
if !list.iter().any(|a| a == &alias) {
list.push(alias);
} }
self self
} }
@ -534,7 +532,7 @@ impl<'a, K: Into<Cow<'a, str>>> Extend<(K, Dynamic)> for Scope<'a> {
#[inline(always)] #[inline(always)]
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
iter.into_iter().for_each(|(name, value)| { iter.into_iter().for_each(|(name, value)| {
self.names.push((name.into(), None)); self.names.push((name.into(), Default::default()));
self.values.push(value); self.values.push(value);
}); });
} }