Optimized ModuleInfo usage

This commit is contained in:
Syrus Akbary
2023-02-16 20:44:49 -08:00
parent c85b2bf62e
commit db6dd420e1
9 changed files with 57 additions and 29 deletions

View File

@ -293,8 +293,8 @@ impl Module {
/// This is normally useful for stacktraces and debugging. /// This is normally useful for stacktraces and debugging.
/// ///
/// It will return `true` if the module name was changed successfully, /// It will return `true` if the module name was changed successfully,
/// and return `false` otherwise (in case the module is already /// and return `false` otherwise (in case the module is cloned or
/// instantiated). /// already instantiated).
/// ///
/// # Example /// # Example
/// ///

View File

@ -29,7 +29,6 @@ pub struct Module {
// In the future, this code should be refactored to properly describe the // In the future, this code should be refactored to properly describe the
// ownership of the code and its metadata. // ownership of the code and its metadata.
artifact: Arc<Artifact>, artifact: Arc<Artifact>,
module_info: Arc<ModuleInfo>,
} }
impl Module { impl Module {
@ -91,10 +90,7 @@ impl Module {
} }
fn from_artifact(artifact: Arc<Artifact>) -> Self { fn from_artifact(artifact: Arc<Artifact>) -> Self {
Self { Self { artifact }
module_info: Arc::new(artifact.create_module_info()),
artifact,
}
} }
pub(crate) fn instantiate( pub(crate) fn instantiate(
@ -140,32 +136,31 @@ impl Module {
} }
pub(crate) fn name(&self) -> Option<&str> { 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 { pub(crate) fn set_name(&mut self, name: &str) -> bool {
Arc::get_mut(&mut self.module_info).map_or(false, |mut module_info| { Arc::get_mut(&mut self.artifact).map_or(false, |artifact| {
module_info.name = Some(name.to_string()); artifact.set_module_info_name(name.to_string())
true
}) })
} }
pub(crate) fn imports(&self) -> ImportsIterator<impl Iterator<Item = ImportType> + '_> { 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> + '_> { pub(crate) fn exports(&self) -> ExportsIterator<impl Iterator<Item = ExportType> + '_> {
self.module_info.exports() self.info().exports()
} }
pub(crate) fn custom_sections<'a>( pub(crate) fn custom_sections<'a>(
&'a self, &'a self,
name: &'a str, name: &'a str,
) -> impl Iterator<Item = Box<[u8]>> + 'a { ) -> impl Iterator<Item = Box<[u8]>> + 'a {
self.module_info.custom_sections(name) self.info().custom_sections(name)
} }
pub(crate) fn info(&self) -> &ModuleInfo { pub(crate) fn info(&self) -> &ModuleInfo {
&self.module_info self.artifact.module_info()
} }
} }

View File

@ -302,7 +302,7 @@ mod tests {
) { ) {
let compile_info = CompileModuleInfo { let compile_info = CompileModuleInfo {
features: Features::new(), features: Features::new(),
module: ModuleInfo::new(), module: Arc::new(ModuleInfo::new()),
memory_styles: PrimaryMap::<MemoryIndex, MemoryStyle>::new(), memory_styles: PrimaryMap::<MemoryIndex, MemoryStyle>::new(),
table_styles: PrimaryMap::<TableIndex, TableStyle>::new(), table_styles: PrimaryMap::<TableIndex, TableStyle>::new(),
}; };

View File

@ -8,6 +8,7 @@ use crate::EngineInner;
use crate::Features; use crate::Features;
use crate::{ModuleEnvironment, ModuleMiddlewareChain}; use crate::{ModuleEnvironment, ModuleMiddlewareChain};
use enumset::EnumSet; use enumset::EnumSet;
use std::sync::Arc;
use wasmer_types::entity::PrimaryMap; use wasmer_types::entity::PrimaryMap;
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
use wasmer_types::CompileModuleInfo; use wasmer_types::CompileModuleInfo;
@ -57,7 +58,7 @@ impl ArtifactBuild {
middlewares.apply_on_module_info(&mut module); middlewares.apply_on_module_info(&mut module);
let compile_info = CompileModuleInfo { let compile_info = CompileModuleInfo {
module, module: Arc::new(module),
features, features,
memory_styles, memory_styles,
table_styles, table_styles,
@ -195,10 +196,21 @@ impl ArtifactBuild {
} }
impl ArtifactCreate for ArtifactBuild { impl ArtifactCreate for ArtifactBuild {
fn create_module_info(&self) -> ModuleInfo { fn create_module_info(&self) -> Arc<ModuleInfo> {
self.serializable.compile_info.module.clone() 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 { fn features(&self) -> &Features {
&self.serializable.compile_info.features &self.serializable.compile_info.features
} }

View File

@ -149,7 +149,7 @@ impl Artifact {
allocated: None, allocated: None,
}); });
} }
let module_info = artifact.create_module_info(); let module_info = artifact.module_info();
let ( let (
finished_functions, finished_functions,
finished_function_call_trampolines, finished_function_call_trampolines,
@ -237,10 +237,18 @@ impl Artifact {
} }
impl ArtifactCreate for 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() self.artifact.create_module_info()
} }
fn module_info(&self) -> &ModuleInfo {
self.artifact.module_info()
}
fn features(&self) -> &Features { fn features(&self) -> &Features {
self.artifact.features() self.artifact.features()
} }
@ -381,7 +389,7 @@ impl Artifact {
self.preinstantiate()?; self.preinstantiate()?;
let module = Arc::new(self.create_module_info()); let module = self.create_module_info();
let imports = resolve_imports( let imports = resolve_imports(
&module, &module,
imports, imports,
@ -499,7 +507,7 @@ impl Artifact {
.collect(); .collect();
let compile_info = CompileModuleInfo { let compile_info = CompileModuleInfo {
module, module: Arc::new(module),
features: features.clone(), features: features.clone(),
memory_styles, memory_styles,
table_styles, table_styles,
@ -576,7 +584,7 @@ impl Artifact {
features: &Features, features: &Features,
) -> Result< ) -> Result<
( (
ModuleInfo, Arc<ModuleInfo>,
Object<'data>, Object<'data>,
usize, usize,
Box<dyn wasmer_types::SymbolRegistry>, Box<dyn wasmer_types::SymbolRegistry>,

View File

@ -13,7 +13,7 @@
//! ``` //! ```
use std::cmp; use std::cmp;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::sync::RwLock; use std::sync::{Arc, RwLock};
use wasmer_types::entity::{BoxedSlice, EntityRef, PrimaryMap}; use wasmer_types::entity::{BoxedSlice, EntityRef, PrimaryMap};
use wasmer_types::{CompiledFunctionFrameInfo, SourceLoc, TrapInformation}; use wasmer_types::{CompiledFunctionFrameInfo, SourceLoc, TrapInformation};
use wasmer_types::{LocalFunctionIndex, ModuleInfo}; use wasmer_types::{LocalFunctionIndex, ModuleInfo};
@ -54,7 +54,7 @@ pub struct GlobalFrameInfoRegistration {
struct ModuleInfoFrameInfo { struct ModuleInfoFrameInfo {
start: usize, start: usize,
functions: BTreeMap<usize, FunctionInfo>, functions: BTreeMap<usize, FunctionInfo>,
module: ModuleInfo, module: Arc<ModuleInfo>,
frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>, frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>,
} }
@ -187,7 +187,7 @@ pub struct FunctionExtent {
/// then `None` will be returned. Otherwise the returned object, when /// then `None` will be returned. Otherwise the returned object, when
/// dropped, will be used to unregister all name information from this map. /// dropped, will be used to unregister all name information from this map.
pub fn register( pub fn register(
module: ModuleInfo, module: Arc<ModuleInfo>,
finished_functions: &BoxedSlice<LocalFunctionIndex, FunctionExtent>, finished_functions: &BoxedSlice<LocalFunctionIndex, FunctionExtent>,
frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>, frame_infos: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>,
) -> Option<GlobalFrameInfoRegistration> { ) -> Option<GlobalFrameInfoRegistration> {

View File

@ -3,6 +3,7 @@
use crate::Features; use crate::Features;
use enumset::EnumSet; use enumset::EnumSet;
use std::any::Any; use std::any::Any;
use std::sync::Arc;
use wasmer_types::entity::PrimaryMap; use wasmer_types::entity::PrimaryMap;
use wasmer_types::SerializeError; use wasmer_types::SerializeError;
use wasmer_types::{ use wasmer_types::{
@ -17,7 +18,13 @@ use wasmer_types::{
/// module at runtime, such as [`ModuleInfo`] and [`Features`]. /// module at runtime, such as [`ModuleInfo`] and [`Features`].
pub trait ArtifactCreate: Send + Sync + Upcastable { pub trait ArtifactCreate: Send + Sync + Upcastable {
/// Create a `ModuleInfo` for instantiation /// 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 /// Returns the features for this Artifact
fn features(&self) -> &Features; fn features(&self) -> &Features;

View File

@ -4,6 +4,7 @@ use crate::{Features, MemoryIndex, MemoryStyle, ModuleInfo, TableIndex, TableSty
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize}; use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc;
/// The required info for compiling a module. /// The required info for compiling a module.
/// ///
@ -16,7 +17,7 @@ pub struct CompileModuleInfo {
/// The features used for compiling the module /// The features used for compiling the module
pub features: Features, pub features: Features,
/// The module information /// The module information
pub module: ModuleInfo, pub module: Arc<ModuleInfo>,
/// The memory styles used for compiling. /// The memory styles used for compiling.
/// ///
/// The compiler will emit the most optimal code based /// The compiler will emit the most optimal code based

View File

@ -128,7 +128,12 @@ impl SerializableModule {
/// Create a `ModuleInfo` for instantiation /// Create a `ModuleInfo` for instantiation
pub fn create_module_info(&self) -> ModuleInfo { 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 /// Returns the features for this Artifact