Skip to content

Commit

Permalink
Store memories and tables on Instance as PrimaryMap.
Browse files Browse the repository at this point in the history
This commit changes how memories and tables are stored in `Instance`.

Previously, the memories and tables were stored as a `BoxedSlice`. Storing it
this way requires an allocation to change the length of the memories and
tables, which is desirable for a pooling instance allocator that is reusing an
`Instance` structure for a new instantiation.

By storing it instead as `PrimaryMap`, the memories and tables can be resized
without any allocations (the capacity of these maps will always be the
configured limits of the pooling allocator).
  • Loading branch information
peterhuene committed Dec 9, 2020
1 parent c191ffd commit 6eec398
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
8 changes: 3 additions & 5 deletions crates/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use std::convert::TryFrom;
use std::ptr::NonNull;
use std::sync::Arc;
use std::{mem, ptr, slice};
use wasmtime_environ::entity::{
packed_option::ReservedValue, BoxedSlice, EntityRef, EntitySet, PrimaryMap,
};
use wasmtime_environ::entity::{packed_option::ReservedValue, EntityRef, EntitySet, PrimaryMap};
use wasmtime_environ::wasm::{
DataIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, ElemIndex, EntityIndex,
FuncIndex, GlobalIndex, InstanceIndex, MemoryIndex, ModuleIndex, TableElementType, TableIndex,
Expand All @@ -47,10 +45,10 @@ pub(crate) struct Instance {
offsets: VMOffsets,

/// WebAssembly linear memory data.
memories: BoxedSlice<DefinedMemoryIndex, Box<dyn RuntimeLinearMemory>>,
memories: PrimaryMap<DefinedMemoryIndex, Box<dyn RuntimeLinearMemory>>,

/// WebAssembly table data.
tables: BoxedSlice<DefinedTableIndex, Table>,
tables: PrimaryMap<DefinedTableIndex, Table>,

/// Instances our module defined and their handles.
instances: PrimaryMap<InstanceIndex, InstanceHandle>,
Expand Down
12 changes: 5 additions & 7 deletions crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::ptr::{self, NonNull};
use std::slice;
use std::sync::Arc;
use thiserror::Error;
use wasmtime_environ::entity::{
packed_option::ReservedValue, BoxedSlice, EntityRef, EntitySet, PrimaryMap,
};
use wasmtime_environ::entity::{packed_option::ReservedValue, EntityRef, EntitySet, PrimaryMap};
use wasmtime_environ::wasm::{
DefinedFuncIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalInit, SignatureIndex,
TableElementType, WasmType,
Expand Down Expand Up @@ -285,20 +283,20 @@ impl OnDemandInstanceAllocator {
Self { mem_creator }
}

fn create_tables(module: &Module) -> BoxedSlice<DefinedTableIndex, Table> {
fn create_tables(module: &Module) -> PrimaryMap<DefinedTableIndex, Table> {
let num_imports = module.num_imported_tables;
let mut tables: PrimaryMap<DefinedTableIndex, _> =
PrimaryMap::with_capacity(module.table_plans.len() - num_imports);
for table in &module.table_plans.values().as_slice()[num_imports..] {
tables.push(Table::new_dynamic(table));
}
tables.into_boxed_slice()
tables
}

fn create_memories(
&self,
module: &Module,
) -> Result<BoxedSlice<DefinedMemoryIndex, Box<dyn RuntimeLinearMemory>>, InstantiationError>
) -> Result<PrimaryMap<DefinedMemoryIndex, Box<dyn RuntimeLinearMemory>>, InstantiationError>
{
let creator = self
.mem_creator
Expand All @@ -314,7 +312,7 @@ impl OnDemandInstanceAllocator {
.map_err(InstantiationError::Resource)?,
);
}
Ok(memories.into_boxed_slice())
Ok(memories)
}

fn check_table_init_bounds(instance: &Instance) -> Result<(), InstantiationError> {
Expand Down

0 comments on commit 6eec398

Please sign in to comment.