Speed up Array/Map Dynamic construction.

This commit is contained in:
Stephen Chung 2021-04-20 22:21:51 +08:00
parent 8ff1f57900
commit 61d06183ea
2 changed files with 19 additions and 7 deletions

View File

@ -1,6 +1,5 @@
//! Module defining the AST (abstract syntax tree). //! Module defining the AST (abstract syntax tree).
use crate::dynamic::{AccessMode, Union};
use crate::fn_native::shared_make_mut; use crate::fn_native::shared_make_mut;
use crate::module::NamespaceRef; use crate::module::NamespaceRef;
use crate::token::Token; use crate::token::Token;
@ -1761,10 +1760,7 @@ impl Expr {
Self::FloatConstant(x, _) => (*x).into(), Self::FloatConstant(x, _) => (*x).into(),
Self::CharConstant(x, _) => (*x).into(), Self::CharConstant(x, _) => (*x).into(),
Self::StringConstant(x, _) => x.clone().into(), Self::StringConstant(x, _) => x.clone().into(),
Self::FnPointer(x, _) => Dynamic(Union::FnPtr( Self::FnPointer(x, _) => FnPtr::new_unchecked(x.clone(), Default::default()).into(),
Box::new(FnPtr::new_unchecked(x.clone(), Default::default())),
AccessMode::ReadOnly,
)),
Self::BoolConstant(x, _) => (*x).into(), Self::BoolConstant(x, _) => (*x).into(),
Self::Unit(_) => Dynamic::UNIT, Self::Unit(_) => Dynamic::UNIT,
@ -1772,7 +1768,7 @@ impl Expr {
Self::Array(x, _) if self.is_constant() => { Self::Array(x, _) if self.is_constant() => {
let mut arr = Array::with_capacity(x.len()); let mut arr = Array::with_capacity(x.len());
arr.extend(x.iter().map(|v| v.get_constant_value().unwrap())); arr.extend(x.iter().map(|v| v.get_constant_value().unwrap()));
arr.into() Dynamic::from_array(arr)
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
@ -1781,7 +1777,7 @@ impl Expr {
x.0.iter().for_each(|(k, v)| { x.0.iter().for_each(|(k, v)| {
*map.get_mut(k.name.as_str()).unwrap() = v.get_constant_value().unwrap() *map.get_mut(k.name.as_str()).unwrap() = v.get_constant_value().unwrap()
}); });
map.into() Dynamic::from_map(map)
} }
_ => return None, _ => return None,

View File

@ -1721,6 +1721,14 @@ impl From<&crate::Identifier> for Dynamic {
} }
} }
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
impl Dynamic {
/// Create a [`Dynamc`] from an [`Array`].
#[inline(always)]
pub(crate) fn from_array(array: Array) -> Self {
Self(Union::Array(Box::new(array), AccessMode::ReadWrite))
}
}
#[cfg(not(feature = "no_index"))]
impl<T: Variant + Clone> From<Vec<T>> for Dynamic { impl<T: Variant + Clone> From<Vec<T>> for Dynamic {
#[inline(always)] #[inline(always)]
fn from(value: Vec<T>) -> Self { fn from(value: Vec<T>) -> Self {
@ -1751,6 +1759,14 @@ impl<T: Variant + Clone> std::iter::FromIterator<T> for Dynamic {
} }
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl Dynamic {
/// Create a [`Dynamc`] from a [`Map`].
#[inline(always)]
pub(crate) fn from_map(map: Map) -> Self {
Self(Union::Map(Box::new(map), AccessMode::ReadWrite))
}
}
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
impl<K: Into<crate::Identifier>, T: Variant + Clone> From<std::collections::HashMap<K, T>> impl<K: Into<crate::Identifier>, T: Variant + Clone> From<std::collections::HashMap<K, T>>
for Dynamic for Dynamic