mirror of
https://github.com/mii443/wasmer.git
synced 2025-08-25 01:39:26 +00:00
Optimized ModuleInfo usage
This commit is contained in:
@ -293,8 +293,8 @@ impl Module {
|
||||
/// This is normally useful for stacktraces and debugging.
|
||||
///
|
||||
/// It will return `true` if the module name was changed successfully,
|
||||
/// and return `false` otherwise (in case the module is already
|
||||
/// instantiated).
|
||||
/// and return `false` otherwise (in case the module is cloned or
|
||||
/// already instantiated).
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
|
@ -29,7 +29,6 @@ pub struct Module {
|
||||
// In the future, this code should be refactored to properly describe the
|
||||
// ownership of the code and its metadata.
|
||||
artifact: Arc<Artifact>,
|
||||
module_info: Arc<ModuleInfo>,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
@ -91,10 +90,7 @@ impl Module {
|
||||
}
|
||||
|
||||
fn from_artifact(artifact: Arc<Artifact>) -> Self {
|
||||
Self {
|
||||
module_info: Arc::new(artifact.create_module_info()),
|
||||
artifact,
|
||||
}
|
||||
Self { artifact }
|
||||
}
|
||||
|
||||
pub(crate) fn instantiate(
|
||||
@ -140,32 +136,31 @@ impl Module {
|
||||
}
|
||||
|
||||
pub(crate) fn name(&self) -> Option<&str> {
|
||||
self.module_info.name.as_deref()
|
||||
self.info().name.as_deref()
|
||||
}
|
||||
|
||||
pub(crate) fn set_name(&mut self, name: &str) -> bool {
|
||||
Arc::get_mut(&mut self.module_info).map_or(false, |mut module_info| {
|
||||
module_info.name = Some(name.to_string());
|
||||
true
|
||||
Arc::get_mut(&mut self.artifact).map_or(false, |artifact| {
|
||||
artifact.set_module_info_name(name.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn imports(&self) -> ImportsIterator<impl Iterator<Item = ImportType> + '_> {
|
||||
self.module_info.imports()
|
||||
self.info().imports()
|
||||
}
|
||||
|
||||
pub(crate) fn exports(&self) -> ExportsIterator<impl Iterator<Item = ExportType> + '_> {
|
||||
self.module_info.exports()
|
||||
self.info().exports()
|
||||
}
|
||||
|
||||
pub(crate) fn custom_sections<'a>(
|
||||
&'a self,
|
||||
name: &'a str,
|
||||
) -> impl Iterator<Item = Box<[u8]>> + 'a {
|
||||
self.module_info.custom_sections(name)
|
||||
self.info().custom_sections(name)
|
||||
}
|
||||
|
||||
pub(crate) fn info(&self) -> &ModuleInfo {
|
||||
&self.module_info
|
||||
self.artifact.module_info()
|
||||
}
|
||||
}
|
||||
|
@ -302,7 +302,7 @@ mod tests {
|
||||
) {
|
||||
let compile_info = CompileModuleInfo {
|
||||
features: Features::new(),
|
||||
module: ModuleInfo::new(),
|
||||
module: Arc::new(ModuleInfo::new()),
|
||||
memory_styles: PrimaryMap::<MemoryIndex, MemoryStyle>::new(),
|
||||
table_styles: PrimaryMap::<TableIndex, TableStyle>::new(),
|
||||
};
|
||||
|
@ -8,6 +8,7 @@ use crate::EngineInner;
|
||||
use crate::Features;
|
||||
use crate::{ModuleEnvironment, ModuleMiddlewareChain};
|
||||
use enumset::EnumSet;
|
||||
use std::sync::Arc;
|
||||
use wasmer_types::entity::PrimaryMap;
|
||||
#[cfg(feature = "compiler")]
|
||||
use wasmer_types::CompileModuleInfo;
|
||||
@ -57,7 +58,7 @@ impl ArtifactBuild {
|
||||
middlewares.apply_on_module_info(&mut module);
|
||||
|
||||
let compile_info = CompileModuleInfo {
|
||||
module,
|
||||
module: Arc::new(module),
|
||||
features,
|
||||
memory_styles,
|
||||
table_styles,
|
||||
@ -195,10 +196,21 @@ impl ArtifactBuild {
|
||||
}
|
||||
|
||||
impl ArtifactCreate for ArtifactBuild {
|
||||
fn create_module_info(&self) -> ModuleInfo {
|
||||
fn create_module_info(&self) -> Arc<ModuleInfo> {
|
||||
self.serializable.compile_info.module.clone()
|
||||
}
|
||||
|
||||
fn set_module_info_name(&mut self, name: String) -> bool {
|
||||
Arc::get_mut(&mut self.serializable.compile_info.module).map_or(false, |mut module_info| {
|
||||
module_info.name = Some(name.to_string());
|
||||
true
|
||||
})
|
||||
}
|
||||
|
||||
fn module_info(&self) -> &ModuleInfo {
|
||||
&self.serializable.compile_info.module
|
||||
}
|
||||
|
||||
fn features(&self) -> &Features {
|
||||
&self.serializable.compile_info.features
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ impl Artifact {
|
||||
allocated: None,
|
||||
});
|
||||
}
|
||||
let module_info = artifact.create_module_info();
|
||||
let module_info = artifact.module_info();
|
||||
let (
|
||||
finished_functions,
|
||||
finished_function_call_trampolines,
|
||||
@ -237,10 +237,18 @@ impl Artifact {
|
||||
}
|
||||
|
||||
impl ArtifactCreate for Artifact {
|
||||
fn create_module_info(&self) -> ModuleInfo {
|
||||
fn set_module_info_name(&mut self, name: String) -> bool {
|
||||
self.artifact.set_module_info_name(name)
|
||||
}
|
||||
|
||||
fn create_module_info(&self) -> Arc<ModuleInfo> {
|
||||
self.artifact.create_module_info()
|
||||
}
|
||||
|
||||
fn module_info(&self) -> &ModuleInfo {
|
||||
self.artifact.module_info()
|
||||
}
|
||||
|
||||
fn features(&self) -> &Features {
|
||||
self.artifact.features()
|
||||
}
|
||||
@ -381,7 +389,7 @@ impl Artifact {
|
||||
|
||||
self.preinstantiate()?;
|
||||
|
||||
let module = Arc::new(self.create_module_info());
|
||||
let module = self.create_module_info();
|
||||
let imports = resolve_imports(
|
||||
&module,
|
||||
imports,
|
||||
@ -499,7 +507,7 @@ impl Artifact {
|
||||
.collect();
|
||||
|
||||
let compile_info = CompileModuleInfo {
|
||||
module,
|
||||
module: Arc::new(module),
|
||||
features: features.clone(),
|
||||
memory_styles,
|
||||
table_styles,
|
||||
@ -576,7 +584,7 @@ impl Artifact {
|
||||
features: &Features,
|
||||
) -> Result<
|
||||
(
|
||||
ModuleInfo,
|
||||
Arc<ModuleInfo>,
|
||||
Object<'data>,
|
||||
usize,
|
||||
Box<dyn wasmer_types::SymbolRegistry>,
|
||||
|
@ -13,7 +13,7 @@
|
||||
//! ```
|
||||
use std::cmp;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use wasmer_types::entity::{BoxedSlice, EntityRef, PrimaryMap};
|
||||
use wasmer_types::{CompiledFunctionFrameInfo, SourceLoc, TrapInformation};
|
||||
use wasmer_types::{LocalFunctionIndex, ModuleInfo};
|
||||
@ -54,7 +54,7 @@ pub struct GlobalFrameInfoRegistration {
|
||||
struct ModuleInfoFrameInfo {
|
||||
start: usize,
|
||||
functions: BTreeMap<usize, FunctionInfo>,
|
||||
module: ModuleInfo,
|
||||
module: Arc<ModuleInfo>,
|
||||
frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>,
|
||||
}
|
||||
|
||||
@ -187,7 +187,7 @@ pub struct FunctionExtent {
|
||||
/// then `None` will be returned. Otherwise the returned object, when
|
||||
/// dropped, will be used to unregister all name information from this map.
|
||||
pub fn register(
|
||||
module: ModuleInfo,
|
||||
module: Arc<ModuleInfo>,
|
||||
finished_functions: &BoxedSlice<LocalFunctionIndex, FunctionExtent>,
|
||||
frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>,
|
||||
) -> Option<GlobalFrameInfoRegistration> {
|
||||
|
@ -3,6 +3,7 @@
|
||||
use crate::Features;
|
||||
use enumset::EnumSet;
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
use wasmer_types::entity::PrimaryMap;
|
||||
use wasmer_types::SerializeError;
|
||||
use wasmer_types::{
|
||||
@ -17,7 +18,13 @@ use wasmer_types::{
|
||||
/// module at runtime, such as [`ModuleInfo`] and [`Features`].
|
||||
pub trait ArtifactCreate: Send + Sync + Upcastable {
|
||||
/// Create a `ModuleInfo` for instantiation
|
||||
fn create_module_info(&self) -> ModuleInfo;
|
||||
fn create_module_info(&self) -> Arc<ModuleInfo>;
|
||||
|
||||
/// Sets the `ModuleInfo` name
|
||||
fn set_module_info_name(&mut self, name: String) -> bool;
|
||||
|
||||
/// Returns the `ModuleInfo` for instantiation
|
||||
fn module_info(&self) -> &ModuleInfo;
|
||||
|
||||
/// Returns the features for this Artifact
|
||||
fn features(&self) -> &Features;
|
||||
|
@ -4,6 +4,7 @@ use crate::{Features, MemoryIndex, MemoryStyle, ModuleInfo, TableIndex, TableSty
|
||||
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
|
||||
#[cfg(feature = "enable-serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The required info for compiling a module.
|
||||
///
|
||||
@ -16,7 +17,7 @@ pub struct CompileModuleInfo {
|
||||
/// The features used for compiling the module
|
||||
pub features: Features,
|
||||
/// The module information
|
||||
pub module: ModuleInfo,
|
||||
pub module: Arc<ModuleInfo>,
|
||||
/// The memory styles used for compiling.
|
||||
///
|
||||
/// The compiler will emit the most optimal code based
|
||||
|
@ -128,7 +128,12 @@ impl SerializableModule {
|
||||
|
||||
/// Create a `ModuleInfo` for instantiation
|
||||
pub fn create_module_info(&self) -> ModuleInfo {
|
||||
self.compile_info.module.clone()
|
||||
self.compile_info.module.as_ref().clone()
|
||||
}
|
||||
|
||||
/// Returns the `ModuleInfo` for instantiation
|
||||
pub fn module_info(&self) -> &ModuleInfo {
|
||||
&self.compile_info.module
|
||||
}
|
||||
|
||||
/// Returns the features for this Artifact
|
||||
|
Reference in New Issue
Block a user