[−][src]Function yaks::batch
pub fn batch<'query, 'world, Q, F>(
query_borrow: &'query mut QueryBorrow<'world, Q>,
batch_size: u32,
for_each: F
) where
Q: Query + Send + Sync + 'query,
F: Fn(Entity, <<Q as Query>::Fetch as Fetch<'query>>::Item) + Send + Sync,
Distributes over a rayon
thread pool the work of applying a function to items in a query.
See hecs::QueryBorrow::iter_batched()
.
If the default parallel
feature is disabled the functionality is identical
to query_borrow.iter().for_each(for_each)
.
Calling batch()
standalone will use the global rayon
thread pool:
yaks::batch( &mut world.query::<(&mut Pos, &Vel)>(), num_entities / 16, |_entity, (pos, vel)| { *pos += vel; }, );
Alternatively, a specific thread pool can be used via
rayon::ThreadPool::install()
:
thread_pool.install(|| { yaks::batch( &mut world.query::<(&mut Pos, &Vel)>(), num_entities / 16, |_entity, (pos, vel)| { *pos += vel; }, ) });
batch()
can be called in systems, where it will use whichever thread pool is used by
the system or the executor it's in:
let mut executor = Executor::<(u32, )>::builder() .system(|context, num_entities: &u32, query: QueryMarker<(&mut Pos, &Vel)>| { yaks::batch( &mut context.query(query), num_entities / 16, |_entity, (pos, vel)| { *pos += vel; }, ) }) .build(); executor.run(&world, &mut num_entities); thread_pool.install(|| { executor.run(&world, &mut num_entities); });