Change Map keys to ImmutableString to facilitate fast keys().

This commit is contained in:
Stephen Chung 2020-06-21 16:37:05 +08:00
parent 00c4052636
commit 7cc1a3f5dc
5 changed files with 23 additions and 14 deletions

View File

@ -550,8 +550,15 @@ impl Dynamic {
/// Convert the `Dynamic` into `String` and return it. /// Convert the `Dynamic` into `String` and return it.
/// Returns the name of the actual type if the cast fails. /// Returns the name of the actual type if the cast fails.
pub fn take_string(self) -> Result<String, &'static str> { pub fn take_string(self) -> Result<String, &'static str> {
self.take_immutable_string()
.map(ImmutableString::into_owned)
}
/// Convert the `Dynamic` into `ImmutableString` and return it.
/// Returns the name of the actual type if the cast fails.
pub(crate) fn take_immutable_string(self) -> Result<ImmutableString, &'static str> {
match self.0 { match self.0 {
Union::Str(s) => Ok(s.into_owned()), Union::Str(s) => Ok(s),
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
@ -615,7 +622,7 @@ impl<T: Variant + Clone> From<HashMap<String, T>> for Dynamic {
Self(Union::Map(Box::new( Self(Union::Map(Box::new(
value value
.into_iter() .into_iter()
.map(|(k, v)| (k, Dynamic::from(v))) .map(|(k, v)| (k.into(), Dynamic::from(v)))
.collect(), .collect(),
))) )))
} }

View File

@ -38,7 +38,7 @@ pub type Array = Vec<Dynamic>;
/// ///
/// Not available under the `no_object` feature. /// Not available under the `no_object` feature.
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub type Map = HashMap<String, Dynamic>; pub type Map = HashMap<ImmutableString, Dynamic>;
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -1389,7 +1389,7 @@ impl Engine {
// val_map[idx] // val_map[idx]
Ok(if create { Ok(if create {
let index = idx let index = idx
.take_string() .take_immutable_string()
.map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
map.entry(index).or_insert(Default::default()).into() map.entry(index).or_insert(Default::default()).into()
@ -1398,7 +1398,7 @@ impl Engine {
.downcast_ref::<String>() .downcast_ref::<String>()
.ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; .ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
map.get_mut(index) map.get_mut(index.as_str())
.map(Target::from) .map(Target::from)
.unwrap_or_else(|| Target::from(())) .unwrap_or_else(|| Target::from(()))
}) })
@ -1498,7 +1498,9 @@ impl Engine {
Dynamic(Union::Map(rhs_value)) => match lhs_value { Dynamic(Union::Map(rhs_value)) => match lhs_value {
// Only allows String or char // Only allows String or char
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_str()).into()), Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_str()).into()),
Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()), Dynamic(Union::Char(c)) => {
Ok(rhs_value.contains_key(c.to_string().as_str()).into())
}
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))), _ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
}, },
Dynamic(Union::Str(rhs_value)) => match lhs_value { Dynamic(Union::Str(rhs_value)) => match lhs_value {

View File

@ -408,7 +408,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
let pos = m.1; let pos = m.1;
m.0.into_iter().find(|((name, _), _)| name == prop) m.0.into_iter().find(|((name, _), _)| name.as_str() == prop)
.map(|(_, expr)| expr.set_position(pos)) .map(|(_, expr)| expr.set_position(pos))
.unwrap_or_else(|| Expr::Unit(pos)) .unwrap_or_else(|| Expr::Unit(pos))
} }
@ -434,7 +434,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
let pos = m.1; let pos = m.1;
m.0.into_iter().find(|((name, _), _)| name == s.0.as_ref()) m.0.into_iter().find(|((name, _), _)| *name == s.0)
.map(|(_, expr)| expr.set_position(pos)) .map(|(_, expr)| expr.set_position(pos))
.unwrap_or_else(|| Expr::Unit(pos)) .unwrap_or_else(|| Expr::Unit(pos))
} }
@ -472,7 +472,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// "xxx" in #{...} // "xxx" in #{...}
(Expr::StringConstant(a), Expr::Map(b)) => { (Expr::StringConstant(a), Expr::Map(b)) => {
state.set_dirty(); state.set_dirty();
if b.0.iter().find(|((name, _), _)| name == a.0.as_ref()).is_some() { if b.0.iter().find(|((name, _), _)| *name == a.0).is_some() {
Expr::True(a.1) Expr::True(a.1)
} else { } else {
Expr::False(a.1) Expr::False(a.1)
@ -483,7 +483,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
state.set_dirty(); state.set_dirty();
let ch = a.0.to_string(); let ch = a.0.to_string();
if b.0.iter().find(|((name, _), _)| name == &ch).is_some() { if b.0.iter().find(|((name, _), _)| name.as_str() == ch.as_str()).is_some() {
Expr::True(a.1) Expr::True(a.1)
} else { } else {
Expr::False(a.1) Expr::False(a.1)

View File

@ -6,10 +6,10 @@ use crate::engine::Map;
use crate::module::FuncReturn; use crate::module::FuncReturn;
use crate::parser::{ImmutableString, INT}; use crate::parser::{ImmutableString, INT};
use crate::stdlib::{string::ToString, vec::Vec}; use crate::stdlib::vec::Vec;
fn map_get_keys(map: &mut Map) -> FuncReturn<Vec<Dynamic>> { fn map_get_keys(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
Ok(map.iter().map(|(k, _)| k.to_string().into()).collect()) Ok(map.iter().map(|(k, _)| k.clone().into()).collect())
} }
fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> { fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
Ok(map.iter().map(|(_, v)| v.clone()).collect()) Ok(map.iter().map(|(_, v)| v.clone()).collect())

View File

@ -464,7 +464,7 @@ pub enum Expr {
/// [ expr, ... ] /// [ expr, ... ]
Array(Box<(StaticVec<Expr>, Position)>), Array(Box<(StaticVec<Expr>, Position)>),
/// #{ name:expr, ... } /// #{ name:expr, ... }
Map(Box<(StaticVec<((String, Position), Expr)>, Position)>), Map(Box<(StaticVec<((ImmutableString, Position), Expr)>, Position)>),
/// lhs in rhs /// lhs in rhs
In(Box<(Expr, Expr, Position)>), In(Box<(Expr, Expr, Position)>),
/// lhs && rhs /// lhs && rhs
@ -1196,7 +1196,7 @@ fn parse_map_literal(
} }
let expr = parse_expr(input, state, settings.level_up())?; let expr = parse_expr(input, state, settings.level_up())?;
map.push(((name, pos), expr)); map.push(((Into::<ImmutableString>::into(name), pos), expr));
} }
} }