Skip to content

Commit

Permalink
Merge pull request #3738 from cfallin/pooling-affinity
Browse files Browse the repository at this point in the history
Pooling allocator: add a reuse-affinity policy.
  • Loading branch information
cfallin authored Feb 2, 2022
2 parents 99ed8cc + 1cbd393 commit 5deb1f1
Show file tree
Hide file tree
Showing 11 changed files with 723 additions and 128 deletions.
7 changes: 6 additions & 1 deletion crates/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::vmcontext::{
VMCallerCheckedAnyfunc, VMContext, VMFunctionImport, VMGlobalDefinition, VMGlobalImport,
VMInterrupts, VMMemoryDefinition, VMMemoryImport, VMTableDefinition, VMTableImport,
};
use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable, Store};
use crate::{CompiledModuleId, ExportFunction, ExportGlobal, ExportMemory, ExportTable, Store};
use anyhow::Error;
use memoffset::offset_of;
use more_asserts::assert_lt;
Expand Down Expand Up @@ -54,6 +54,9 @@ pub(crate) struct Instance {
/// The `Module` this `Instance` was instantiated from.
module: Arc<Module>,

/// The unique ID for the `Module` this `Instance` was instantiated from.
unique_id: Option<CompiledModuleId>,

/// Offsets in the `vmctx` region, precomputed from the `module` above.
offsets: VMOffsets<HostPtr>,

Expand Down Expand Up @@ -100,13 +103,15 @@ impl Instance {
/// Helper for allocators; not a public API.
pub(crate) fn create_raw(
module: &Arc<Module>,
unique_id: Option<CompiledModuleId>,
wasm_data: &'static [u8],
memories: PrimaryMap<DefinedMemoryIndex, Memory>,
tables: PrimaryMap<DefinedTableIndex, Table>,
host_state: Box<dyn Any + Send + Sync>,
) -> Instance {
Instance {
module: module.clone(),
unique_id,
offsets: VMOffsets::new(HostPtr, &module),
memories,
tables,
Expand Down
15 changes: 12 additions & 3 deletions crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::vmcontext::{
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMGlobalDefinition, VMSharedSignatureIndex,
};
use crate::ModuleMemFds;
use crate::Store;
use crate::{CompiledModuleId, Store};
use anyhow::Result;
use std::alloc;
use std::any::Any;
Expand Down Expand Up @@ -35,6 +35,9 @@ pub struct InstanceAllocationRequest<'a> {
/// The module being instantiated.
pub module: Arc<Module>,

/// The unique ID of the module being allocated within this engine.
pub unique_id: Option<CompiledModuleId>,

/// The base address of where JIT functions are located.
pub image_base: usize,

Expand Down Expand Up @@ -726,8 +729,14 @@ unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
let host_state = std::mem::replace(&mut req.host_state, Box::new(()));

let mut handle = {
let instance =
Instance::create_raw(&req.module, &*req.wasm_data, memories, tables, host_state);
let instance = Instance::create_raw(
&req.module,
req.unique_id,
&*req.wasm_data,
memories,
tables,
host_state,
);
let layout = instance.alloc_layout();
let instance_ptr = alloc::alloc(layout) as *mut Instance;
if instance_ptr.is_null() {
Expand Down
Loading

0 comments on commit 5deb1f1

Please sign in to comment.