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
}
);
assert_eq!(size_of::<Scope>(), 160);
assert_eq!(size_of::<Scope>(), 464);
assert_eq!(size_of::<LexError>(), 56);
assert_eq!(
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.
//
// 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)]
pub struct Scope<'a> {
/// Current value of the entry.
values: smallvec::SmallVec<[Dynamic; SCOPE_ENTRIES_INLINED]>,
/// (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<'_> {
@ -253,7 +253,7 @@ impl<'a> Scope<'a> {
access: AccessMode,
mut value: Dynamic,
) -> &mut Self {
self.names.push((name.into(), None));
self.names.push((name.into(), Default::default()));
value.set_access_mode(access);
self.values.push(value.into());
self
@ -436,20 +436,18 @@ impl<'a> Scope<'a> {
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub(crate) fn add_entry_alias(&mut self, index: usize, alias: Identifier) -> &mut Self {
let entry = self
let (_, aliases) = self
.names
.get_mut(index)
.expect("never fails unless the index is out of bounds");
if entry.1.is_none() {
// Initialize the alias list if it is empty.
entry.1 = Some(Default::default());
}
let list = entry
.1
.as_mut()
.expect("never fails because the list is initialized");
if !list.iter().any(|a| a == &alias) {
list.push(alias);
match aliases {
None => {
let mut list = StaticVec::new();
list.push(alias);
*aliases = Some(list.into());
}
Some(aliases) if !aliases.iter().any(|a| a == &alias) => aliases.push(alias),
Some(_) => (),
}
self
}
@ -534,7 +532,7 @@ impl<'a, K: Into<Cow<'a, str>>> Extend<(K, Dynamic)> for Scope<'a> {
#[inline(always)]
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
iter.into_iter().for_each(|(name, value)| {
self.names.push((name.into(), None));
self.names.push((name.into(), Default::default()));
self.values.push(value);
});
}