Skip to content

Commit

Permalink
Merge pull request #4552 from wasmerio/module-cache-optimization-round2
Browse files Browse the repository at this point in the history
Module cache optimization round2
  • Loading branch information
syrusakbary authored Apr 15, 2024
2 parents a28b1f1 + ecca8a6 commit 77c3d0e
Show file tree
Hide file tree
Showing 25 changed files with 365 additions and 95 deletions.
8 changes: 6 additions & 2 deletions lib/api/src/sys/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ impl NativeEngineExt for crate::engine::Engine {
&self,
file_ref: &Path,
) -> Result<crate::Module, DeserializeError> {
let bytes = std::fs::read(file_ref)?;
let artifact = Arc::new(Artifact::deserialize_unchecked(&self.0, bytes.into())?);
let file = std::fs::File::open(file_ref)?;
let artifact = Arc::new(Artifact::deserialize_unchecked(
&self.0,
OwnedBuffer::from_file(&file)
.map_err(|e| DeserializeError::Generic(format!("{e:?}")))?,
)?);
Ok(crate::Module(super::module::Module::from_artifact(
artifact,
)))
Expand Down
41 changes: 23 additions & 18 deletions lib/compiler/src/artifact_builders/artifact_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#[cfg(feature = "compiler")]
use super::trampoline::{libcall_trampoline_len, make_libcall_trampolines};
use crate::ArtifactCreate;
#[cfg(feature = "compiler")]
use crate::EngineInner;
use crate::Features;
#[cfg(feature = "compiler")]
use crate::{ModuleEnvironment, ModuleMiddlewareChain};
use core::mem::MaybeUninit;
use enumset::EnumSet;
Expand All @@ -18,7 +20,6 @@ use wasmer_types::entity::{ArchivedPrimaryMap, PrimaryMap};
use wasmer_types::ArchivedOwnedDataInitializer;
use wasmer_types::ArchivedSerializableCompilation;
use wasmer_types::ArchivedSerializableModule;
#[cfg(feature = "compiler")]
use wasmer_types::CompileModuleInfo;
use wasmer_types::DeserializeError;
use wasmer_types::{
Expand Down Expand Up @@ -133,21 +134,6 @@ impl ArtifactBuild {
Ok(Self { serializable })
}

/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(not(feature = "compiler"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn new(
_inner_engine: &mut EngineInner,
_data: &[u8],
_target: &Target,
_memory_styles: PrimaryMap<MemoryIndex, MemoryStyle>,
_table_styles: PrimaryMap<TableIndex, TableStyle>,
) -> Result<Self, CompileError> {
Err(CompileError::Codegen(
"Compilation is not enabled in the engine".to_string(),
))
}

/// Create a new ArtifactBuild from a SerializableModule
pub fn from_serializable(serializable: SerializableModule) -> Self {
Self { serializable }
Expand Down Expand Up @@ -250,6 +236,7 @@ impl<'a> ArtifactCreate<'a> for ArtifactBuild {

/// Module loaded from an archive. Since `CompileModuleInfo` is part of the public
/// interface of this crate and has to be mutable, it has to be deserialized completely.
#[derive(Debug)]
pub struct ModuleFromArchive<'a> {
/// The main serializable compilation object
pub compilation: &'a ArchivedSerializableCompilation,
Expand Down Expand Up @@ -283,11 +270,14 @@ self_cell!(
#[covariant]
dependent: ModuleFromArchive,
}

impl {Debug}
);

/// A compiled wasm module that was loaded from a serialized archive.
#[derive(Clone, Debug)]
pub struct ArtifactBuildFromArchive {
cell: ArtifactBuildFromArchiveCell,
cell: Arc<ArtifactBuildFromArchiveCell>,

/// Compilation informations
compile_info: CompileModuleInfo,
Expand All @@ -314,7 +304,15 @@ impl ArtifactBuildFromArchive {

// Safety: we know the lambda will execute before getting here and assign both values
let compile_info = unsafe { compile_info.assume_init() };
Ok(Self { cell, compile_info })
Ok(Self {
cell: Arc::new(cell),
compile_info,
})
}

/// Gets the owned buffer
pub fn owned_buffer(&self) -> &OwnedBuffer {
self.cell.borrow_owner()
}

/// Get Functions Bodies ref
Expand Down Expand Up @@ -392,6 +390,13 @@ impl ArtifactBuildFromArchive {
}
}

/// Get Function Relocations ref
pub fn get_frame_info_ref(
&self,
) -> &ArchivedPrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo> {
&self.cell.borrow_dependent().compilation.function_frame_info
}

/// Get Function Relocations ref
pub fn deserialize_frame_info_ref(
&self,
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler/src/artifact_builders/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Generic Artifact abstraction for Wasmer Engines.
mod artifact_builder;
#[cfg(feature = "compiler")]
mod trampoline;

pub use self::artifact_builder::{ArtifactBuild, ArtifactBuildFromArchive, ModuleFromArchive};
#[cfg(feature = "compiler")]
pub use self::trampoline::*;
7 changes: 5 additions & 2 deletions lib/compiler/src/engine/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::ArtifactBuild;
use crate::ArtifactBuildFromArchive;
use crate::ArtifactCreate;
use crate::Features;
use crate::FrameInfosVariant;
use crate::ModuleEnvironment;
use crate::{
register_frame_info, resolve_imports, FunctionExtent, GlobalFrameInfoRegistration,
Expand Down Expand Up @@ -680,8 +681,10 @@ impl Artifact {
self.artifact.create_module_info(),
&finished_function_extents,
match &self.artifact {
ArtifactBuildVariant::Plain(p) => p.get_frame_info_ref().clone(),
ArtifactBuildVariant::Archived(a) => a.deserialize_frame_info_ref()?,
ArtifactBuildVariant::Plain(p) => {
FrameInfosVariant::Owned(p.get_frame_info_ref().clone())
}
ArtifactBuildVariant::Archived(a) => FrameInfosVariant::Archived(a.clone()),
},
);

Expand Down
Loading

0 comments on commit 77c3d0e

Please sign in to comment.