mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-30 20:14:23 +00:00
perf: Make WasmPlugins be Arc<WasmPlugin> instead of Box<Arc<WasmPlugin>> (#2631)
* Make Plugin trait use &self instead of &mut self, don't make WasmPlugin be Box<Arc<T>> * fix git thing
This commit is contained in:
@@ -39,26 +39,16 @@ pub trait Plugin: Send + Sync + 'static {
|
||||
/// Asynchronous method called when the plugin is loaded.
|
||||
///
|
||||
/// This method initializes the plugin within the server context.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `_server`: Reference to the server's context.
|
||||
///
|
||||
/// # Returns
|
||||
/// - `Ok(())` on success, or `Err(String)` on failure.
|
||||
fn on_load(&mut self, _server: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
#[expect(unused)]
|
||||
fn on_load(&self, server: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
Box::pin(async move { Ok(()) })
|
||||
}
|
||||
|
||||
/// Asynchronous method called when the plugin is unloaded.
|
||||
///
|
||||
/// This method cleans up resources when the plugin is removed from the server context.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `_server`: Reference to the server's context.
|
||||
///
|
||||
/// # Returns
|
||||
/// - `Ok(())` on success, or `Err(String)` on failure.
|
||||
fn on_unload(&mut self, _server: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
#[expect(unused)]
|
||||
fn on_unload(&self, server: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
Box::pin(async move { Ok(()) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::plugin::{PluginMetadata, api::Plugin, loader::wasm::wasm_host::PluginInitError};
|
||||
use std::{any::Any, path::Path, pin::Pin};
|
||||
use std::{any::Any, path::Path, pin::Pin, sync::Arc};
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod native;
|
||||
@@ -9,7 +9,7 @@ pub type PluginLoadFuture<'a> = Pin<
|
||||
Box<
|
||||
dyn Future<
|
||||
Output = Result<
|
||||
(Box<dyn Plugin>, PluginMetadata, Box<dyn Any + Send + Sync>),
|
||||
(Arc<dyn Plugin>, PluginMetadata, Box<dyn Any + Send + Sync>),
|
||||
LoaderError,
|
||||
>,
|
||||
> + Send
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::any::Any;
|
||||
use std::sync::LazyLock;
|
||||
use std::{
|
||||
any::Any,
|
||||
sync::{Arc, LazyLock},
|
||||
};
|
||||
|
||||
use libloading::Library;
|
||||
|
||||
@@ -53,7 +55,7 @@ impl PluginLoader for NativePluginLoader {
|
||||
};
|
||||
|
||||
Ok((
|
||||
plugin_factory(),
|
||||
Arc::from(plugin_factory()),
|
||||
metadata,
|
||||
Box::new(library) as Box<dyn Any + Send + Sync>,
|
||||
))
|
||||
|
||||
@@ -9,22 +9,24 @@ use crate::plugin::{
|
||||
|
||||
pub mod wasm_host;
|
||||
|
||||
impl Plugin for Arc<WasmPlugin> {
|
||||
fn on_load(&mut self, context: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
impl Plugin for WasmPlugin {
|
||||
fn on_load(&self, context: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
Box::pin(async move {
|
||||
self.as_ref()
|
||||
.on_load(context)
|
||||
// More qualified syntax to not call the current on_load function recursively and instead call
|
||||
// WasmPlugin::on_load
|
||||
Self::on_load(self, context)
|
||||
.await
|
||||
.map_err(|err| err.to_string())?
|
||||
.map_err(|err| err.to_string())
|
||||
.flatten()
|
||||
})
|
||||
}
|
||||
|
||||
fn on_unload(&mut self, context: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
fn on_unload(&self, context: Arc<Context>) -> PluginFuture<'_, Result<(), String>> {
|
||||
Box::pin(async move {
|
||||
self.as_ref()
|
||||
.on_unload(context)
|
||||
Self::on_unload(self, context)
|
||||
.await
|
||||
.map_err(|err| err.to_string())?
|
||||
.map_err(|err| err.to_string())
|
||||
.flatten()
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -39,7 +41,7 @@ impl PluginLoader for WasmPluginLoader {
|
||||
let (plugin, metadata) = runtime.init_plugin(&path).await?;
|
||||
|
||||
Ok((
|
||||
Box::new(plugin) as Box<dyn Plugin>,
|
||||
plugin as Arc<dyn Plugin>,
|
||||
metadata,
|
||||
Box::new(()) as Box<dyn Any + Send + Sync>,
|
||||
))
|
||||
|
||||
@@ -193,7 +193,7 @@ pub struct PluginManager {
|
||||
/// - Windows: Plugin cannot be unloaded, it can be only active or not
|
||||
struct LoadedPlugin {
|
||||
metadata: PluginMetadata,
|
||||
instance: Option<Box<dyn Plugin>>,
|
||||
instance: Option<Arc<dyn Plugin>>,
|
||||
loader: Arc<dyn PluginLoader>,
|
||||
loader_data: Option<Box<dyn Any + Send + Sync>>,
|
||||
is_active: bool,
|
||||
@@ -533,7 +533,7 @@ impl PluginManager {
|
||||
#[expect(clippy::too_many_lines)]
|
||||
async fn spawn_plugin_initialization(
|
||||
&self,
|
||||
mut instance: Box<dyn Plugin>,
|
||||
instance: Arc<dyn Plugin>,
|
||||
metadata: PluginMetadata,
|
||||
loader_data: Box<dyn Any + Send + Sync>,
|
||||
loader: Arc<dyn PluginLoader>,
|
||||
@@ -731,7 +731,7 @@ impl PluginManager {
|
||||
let mut plugins_map: HashMap<
|
||||
String,
|
||||
(
|
||||
Box<dyn Plugin>,
|
||||
Arc<dyn Plugin>,
|
||||
PluginMetadata,
|
||||
Box<dyn Any + Send + Sync>,
|
||||
Arc<dyn PluginLoader>,
|
||||
@@ -942,7 +942,7 @@ impl PluginManager {
|
||||
plugins.remove(index)
|
||||
};
|
||||
|
||||
if let Some(mut instance) = plugin.instance.take() {
|
||||
if let Some(instance) = plugin.instance.take() {
|
||||
instance.on_unload(plugin.context.clone()).await.ok();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user