Refactor how signatures/trampolines are stored in Store
This commit refactors where trampolines and signature information is stored within a `Store`, namely moving them from `wasmtime_runtime::Instance` instead to `Store` itself. The goal here is to remove an allocation inside of an `Instance` and make them a bit cheaper to create. Additionally this should open up future possibilities like not creating duplicate trampolines for signatures already in the `Store` when using `Func::new`.
This commit is contained in:
@@ -11,7 +11,7 @@ use crate::traphandlers::Trap;
|
||||
use crate::vmcontext::{
|
||||
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport,
|
||||
VMGlobalDefinition, VMGlobalImport, VMInterrupts, VMMemoryDefinition, VMMemoryImport,
|
||||
VMSharedSignatureIndex, VMTableDefinition, VMTableImport, VMTrampoline,
|
||||
VMSharedSignatureIndex, VMTableDefinition, VMTableImport,
|
||||
};
|
||||
use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable};
|
||||
use memoffset::offset_of;
|
||||
@@ -62,9 +62,6 @@ pub(crate) struct Instance {
|
||||
/// get removed. A missing entry is considered equivalent to an empty slice.
|
||||
passive_data: RefCell<HashMap<DataIndex, Arc<[u8]>>>,
|
||||
|
||||
/// Pointers to trampoline functions used to enter particular signatures
|
||||
trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
|
||||
|
||||
/// Hosts can store arbitrary per-instance information here.
|
||||
host_state: Box<dyn Any>,
|
||||
|
||||
@@ -815,10 +812,9 @@ impl InstanceHandle {
|
||||
module: Arc<Module>,
|
||||
code: Arc<dyn Any>,
|
||||
finished_functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
|
||||
trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
|
||||
imports: Imports,
|
||||
mem_creator: Option<&dyn RuntimeMemoryCreator>,
|
||||
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
|
||||
lookup_shared_signature: &dyn Fn(SignatureIndex) -> VMSharedSignatureIndex,
|
||||
host_state: Box<dyn Any>,
|
||||
interrupts: *const VMInterrupts,
|
||||
externref_activations_table: *mut VMExternRefActivationsTable,
|
||||
@@ -857,7 +853,6 @@ impl InstanceHandle {
|
||||
tables,
|
||||
passive_elements: Default::default(),
|
||||
passive_data,
|
||||
trampolines,
|
||||
host_state,
|
||||
vmctx: VMContext {},
|
||||
};
|
||||
@@ -873,12 +868,12 @@ impl InstanceHandle {
|
||||
};
|
||||
let instance = handle.instance();
|
||||
|
||||
debug_assert_eq!(vmshared_signatures.len(), handle.module().signatures.len());
|
||||
ptr::copy(
|
||||
vmshared_signatures.values().as_slice().as_ptr(),
|
||||
instance.signature_ids_ptr() as *mut VMSharedSignatureIndex,
|
||||
vmshared_signatures.len(),
|
||||
);
|
||||
let mut ptr = instance.signature_ids_ptr();
|
||||
for (signature, _) in handle.module().signatures.iter() {
|
||||
*ptr = lookup_shared_signature(signature);
|
||||
ptr = ptr.add(1);
|
||||
}
|
||||
|
||||
debug_assert_eq!(imports.functions.len(), handle.module().num_imported_funcs);
|
||||
ptr::copy(
|
||||
imports.functions.as_ptr(),
|
||||
@@ -1128,11 +1123,6 @@ impl InstanceHandle {
|
||||
self.instance().get_defined_table(index)
|
||||
}
|
||||
|
||||
/// Gets the trampoline pre-registered for a particular signature
|
||||
pub fn trampoline(&self, sig: VMSharedSignatureIndex) -> Option<VMTrampoline> {
|
||||
self.instance().trampolines.get(&sig).cloned()
|
||||
}
|
||||
|
||||
/// Return a reference to the contained `Instance`.
|
||||
pub(crate) fn instance(&self) -> &Instance {
|
||||
unsafe { &*(self.instance as *const Instance) }
|
||||
|
||||
Reference in New Issue
Block a user