Added parallelism to event execution using rayon

This commit is contained in:
ccgauche 2020-12-12 16:56:09 +01:00 committed by Joshua Barretto
parent cae81e625e
commit b18eda37b5
2 changed files with 7 additions and 8 deletions

View File

@ -11,6 +11,8 @@ use plugin_api::Event;
use self::{ errors::PluginError, module::{PluginModule, PreparedEventQuery}};
use rayon::prelude::*;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PluginData {
name: String,
@ -88,12 +90,9 @@ impl PluginMgr {
}
pub fn execute_prepared<T>(&self, event_name: &str,event: &PreparedEventQuery<T>) -> Result<Vec<T::Response>, PluginError> where T: Event {
let mut out = Vec::new();
for plugin in &self.plugins {
let exe = plugin.execute_prepared(event_name, event)?;
out.extend(exe);
}
Ok(out)
Ok(self.plugins.par_iter().map(|plugin| {
plugin.execute_prepared(event_name, event)
}).collect::<Result<Vec<_>,_>>()?.into_iter().flatten().collect())
}
pub fn execute_event<T>(&self, event_name: &str,event: &T) -> Result<Vec<T::Response>, PluginError> where T: Event {

View File

@ -8,8 +8,8 @@ pub enum Action {
KillEntity(usize)
}
pub trait Event: Serialize + DeserializeOwned{
type Response: Serialize + DeserializeOwned;
pub trait Event: Serialize + DeserializeOwned + Send + Sync{
type Response: Serialize + DeserializeOwned + Send + Sync;
}
// TODO: Unify this with common/src/comp/uid.rs:Uid