Optimize Imports.

This commit is contained in:
Stephen Chung 2020-11-19 10:14:43 +08:00
parent 21c641d21f
commit a8cdb661f8
3 changed files with 43 additions and 29 deletions

View File

@ -52,83 +52,97 @@ pub const TYPICAL_MAP_SIZE: usize = 8; // Small maps are typical
// We cannot use &str or Cow<str> here because `eval` may load a module and the module name will live beyond // We cannot use &str or Cow<str> here because `eval` may load a module and the module name will live beyond
// the AST of the eval script text. The best we can do is a shared reference. // the AST of the eval script text. The best we can do is a shared reference.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Imports(StaticVec<(ImmutableString, Shared<Module>)>); pub struct Imports(Option<StaticVec<(ImmutableString, Shared<Module>)>>);
impl Imports { impl Imports {
/// Get the length of this stack of imported modules. /// Get the length of this stack of imported modules.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.len() self.0.as_ref().map_or(0, StaticVec::len)
} }
/// Is this stack of imported modules empty? /// Is this stack of imported modules empty?
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0.is_empty() self.0.as_ref().map_or(true, StaticVec::is_empty)
} }
/// Get the imported module at a particular index. /// Get the imported module at a particular index.
pub fn get(&self, index: usize) -> Option<Shared<Module>> { pub fn get(&self, index: usize) -> Option<Shared<Module>> {
self.0.get(index).map(|(_, m)| m).cloned() self.0
.as_ref()
.and_then(|x| x.get(index))
.map(|(_, m)| m)
.cloned()
} }
/// Get the index of an imported module by name. /// Get the index of an imported module by name.
pub fn find(&self, name: &str) -> Option<usize> { pub fn find(&self, name: &str) -> Option<usize> {
self.0 self.0.as_ref().and_then(|x| {
.iter() x.iter()
.enumerate() .enumerate()
.rev() .rev()
.find(|(_, (key, _))| key.as_str() == name) .find(|(_, (key, _))| key.as_str() == name)
.map(|(index, _)| index) .map(|(index, _)| index)
})
} }
/// Push an imported module onto the stack. /// Push an imported module onto the stack.
pub fn push(&mut self, name: impl Into<ImmutableString>, module: impl Into<Shared<Module>>) { pub fn push(&mut self, name: impl Into<ImmutableString>, module: impl Into<Shared<Module>>) {
self.0.push((name.into(), module.into())); if self.0.is_none() {
self.0 = Some(Default::default());
}
self.0.as_mut().unwrap().push((name.into(), module.into()));
} }
/// Truncate the stack of imported modules to a particular length. /// Truncate the stack of imported modules to a particular length.
pub fn truncate(&mut self, size: usize) { pub fn truncate(&mut self, size: usize) {
self.0.truncate(size); if self.0.is_some() {
self.0.as_mut().unwrap().truncate(size);
}
} }
/// Get an iterator to this stack of imported modules. /// Get an iterator to this stack of imported modules.
#[allow(dead_code)] #[allow(dead_code)]
pub fn iter(&self) -> impl Iterator<Item = (&str, Shared<Module>)> { pub fn iter(&self) -> impl Iterator<Item = (&str, Shared<Module>)> {
self.0 self.0.iter().flat_map(|lib| {
.iter() lib.iter()
.map(|(name, module)| (name.as_str(), module.clone())) .map(|(name, module)| (name.as_str(), module.clone()))
})
} }
/// Get an iterator to this stack of imported modules. /// Get an iterator to this stack of imported modules.
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) fn iter_raw<'a>( pub(crate) fn iter_raw<'a>(
&'a self, &'a self,
) -> impl Iterator<Item = (ImmutableString, Shared<Module>)> + 'a { ) -> impl Iterator<Item = (ImmutableString, Shared<Module>)> + 'a {
self.0.iter().cloned() self.0.iter().flat_map(|lib| lib.iter().cloned())
} }
/// Get a consuming iterator to this stack of imported modules. /// Get a consuming iterator to this stack of imported modules.
pub fn into_iter(self) -> impl Iterator<Item = (ImmutableString, Shared<Module>)> { pub fn into_iter(self) -> impl Iterator<Item = (ImmutableString, Shared<Module>)> {
self.0.into_iter() self.0.into_iter().flat_map(|lib| lib.into_iter())
} }
/// Add a stream of imported modules. /// Add a stream of imported modules.
pub fn extend(&mut self, stream: impl Iterator<Item = (ImmutableString, Shared<Module>)>) { pub fn extend(&mut self, stream: impl Iterator<Item = (ImmutableString, Shared<Module>)>) {
self.0.extend(stream) self.0.as_mut().unwrap().extend(stream)
} }
/// Does the specified function hash key exist in this stack of imported modules? /// Does the specified function hash key exist in this stack of imported modules?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_fn(&self, hash: u64) -> bool { pub fn contains_fn(&self, hash: u64) -> bool {
self.0.iter().any(|(_, m)| m.contains_qualified_fn(hash)) self.0.as_ref().map_or(false, |x| {
x.iter().any(|(_, m)| m.contains_qualified_fn(hash))
})
} }
/// Get specified function via its hash key. /// Get specified function via its hash key.
pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> {
self.0 self.0
.iter() .as_ref()
.rev() .and_then(|x| x.iter().rev().find_map(|(_, m)| m.get_qualified_fn(hash)))
.find_map(|(_, m)| m.get_qualified_fn(hash))
} }
/// Does the specified TypeId iterator exist in this stack of imported modules? /// Does the specified TypeId iterator exist in this stack of imported modules?
#[allow(dead_code)] #[allow(dead_code)]
pub fn contains_iter(&self, id: TypeId) -> bool { pub fn contains_iter(&self, id: TypeId) -> bool {
self.0.iter().any(|(_, m)| m.contains_qualified_iter(id)) self.0.as_ref().map_or(false, |x| {
x.iter().any(|(_, m)| m.contains_qualified_iter(id))
})
} }
/// Get the specified TypeId iterator. /// Get the specified TypeId iterator.
pub fn get_iter(&self, id: TypeId) -> Option<IteratorFn> { pub fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
self.0 self.0
.iter() .as_ref()
.rev() .and_then(|x| x.iter().rev().find_map(|(_, m)| m.get_qualified_iter(id)))
.find_map(|(_, m)| m.get_qualified_iter(id))
} }
} }

View File

@ -1588,7 +1588,7 @@ impl Module {
.. ..
}, },
)| { )| {
// Flatten all methods so they can be available without namespace qualifiers // Flatten all functions with global namespace
if namespace.is_global() { if namespace.is_global() {
functions.insert(hash, func.clone()); functions.insert(hash, func.clone());
} }

View File

@ -116,9 +116,9 @@ fn calc_fn_hash<'a>(
modules.next().is_some().hash(s); modules.next().is_some().hash(s);
// We always skip the first module // We always skip the first module
modules.for_each(|m| m.hash(s)); modules.for_each(|m| m.hash(s));
s.write(fn_name.as_bytes()); fn_name.hash(s);
if let Some(num) = num { if let Some(num) = num {
s.write_usize(num); num.hash(s);
} else { } else {
params.for_each(|t| t.hash(s)); params.for_each(|t| t.hash(s));
} }