Merge pull request #6 from schungx/master

Avoid copying iterator sources.
This commit is contained in:
Stephen Chung 2020-04-26 17:03:28 +08:00 committed by GitHub
commit 5da9bc0f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 27 deletions

View File

@ -44,19 +44,19 @@ impl<F: Fn(&mut T, U) + 'static, T, U> ObjectSetCallback<T, U> for F {}
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub trait IteratorCallback: pub trait IteratorCallback:
Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static
{ {
} }
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
impl<F: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static> IteratorCallback impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static> IteratorCallback
for F for F
{ {
} }
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub trait IteratorCallback: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static {} pub trait IteratorCallback: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static {}
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
impl<F: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {} impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {}
/// Engine public API /// Engine public API
impl Engine { impl Engine {

View File

@ -49,9 +49,9 @@ pub type FnAny =
pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result<Dynamic, Box<EvalAltResult>>; pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result<Dynamic, Box<EvalAltResult>>;
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub type IteratorFn = dyn Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync; pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync;
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub type IteratorFn = dyn Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>>; pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub const MAX_CALL_STACK_DEPTH: usize = 28; pub const MAX_CALL_STACK_DEPTH: usize = 28;
@ -1608,8 +1608,7 @@ impl Engine {
.find(|pkg| pkg.type_iterators.contains_key(&tid)) .find(|pkg| pkg.type_iterators.contains_key(&tid))
.and_then(|pkg| pkg.type_iterators.get(&tid)) .and_then(|pkg| pkg.type_iterators.get(&tid))
}) { }) {
// Add the loop variable - variable name is copied // Add the loop variable
// TODO - avoid copying variable name
scope.push(name.clone(), ()); scope.push(name.clone(), ());
let entry = ScopeSource { let entry = ScopeSource {
@ -1618,7 +1617,7 @@ impl Engine {
typ: ScopeEntryType::Normal, typ: ScopeEntryType::Normal,
}; };
for a in iter_fn(&arr) { for a in iter_fn(arr) {
*scope.get_mut(entry) = a; *scope.get_mut(entry) = a;
match self.eval_stmt(scope, fn_lib, body, level) { match self.eval_stmt(scope, fn_lib, body, level) {

View File

@ -116,8 +116,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
// Register array iterator // Register array iterator
lib.type_iterators.insert( lib.type_iterators.insert(
TypeId::of::<Array>(), TypeId::of::<Array>(),
Box::new(|a: &Dynamic| { Box::new(|a: Dynamic| {
Box::new(a.downcast_ref::<Array>().unwrap().clone().into_iter()) Box::new(a.cast::<Array>().into_iter())
as Box<dyn Iterator<Item = Dynamic>> as Box<dyn Iterator<Item = Dynamic>>
}), }),
); );

View File

@ -19,14 +19,9 @@ where
{ {
lib.type_iterators.insert( lib.type_iterators.insert(
TypeId::of::<Range<T>>(), TypeId::of::<Range<T>>(),
Box::new(|source: &Dynamic| { Box::new(|source: Dynamic| {
Box::new( Box::new(source.cast::<Range<T>>().map(|x| x.into_dynamic()))
source as Box<dyn Iterator<Item = Dynamic>>
.downcast_ref::<Range<T>>()
.cloned()
.unwrap()
.map(|x| x.into_dynamic()),
) as Box<dyn Iterator<Item = Dynamic>>
}), }),
); );
} }
@ -64,14 +59,9 @@ where
{ {
lib.type_iterators.insert( lib.type_iterators.insert(
TypeId::of::<StepRange<T>>(), TypeId::of::<StepRange<T>>(),
Box::new(|source: &Dynamic| { Box::new(|source: Dynamic| {
Box::new( Box::new(source.cast::<StepRange<T>>().map(|x| x.into_dynamic()))
source as Box<dyn Iterator<Item = Dynamic>>
.downcast_ref::<StepRange<T>>()
.cloned()
.unwrap()
.map(|x| x.into_dynamic()),
) as Box<dyn Iterator<Item = Dynamic>>
}), }),
); );
} }