From 3a7d19f60ee0a6258316cdf570494fe249149b40 Mon Sep 17 00:00:00 2001 From: ccgauche Date: Sat, 12 Dec 2020 16:56:09 +0100 Subject: [PATCH] Added parallelism to event execution using rayon --- common/src/plugin/mod.rs | 11 +++++------ plugin/api/src/lib.rs | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/common/src/plugin/mod.rs b/common/src/plugin/mod.rs index 7055691b04..5715be75f4 100644 --- a/common/src/plugin/mod.rs +++ b/common/src/plugin/mod.rs @@ -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(&self, event_name: &str,event: &PreparedEventQuery) -> Result, 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::,_>>()?.into_iter().flatten().collect()) } pub fn execute_event(&self, event_name: &str,event: &T) -> Result, PluginError> where T: Event { diff --git a/plugin/api/src/lib.rs b/plugin/api/src/lib.rs index 3c6127cded..1bcd78ee45 100644 --- a/plugin/api/src/lib.rs +++ b/plugin/api/src/lib.rs @@ -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