Shrink the size of the anyfunc table in VMContext (#3850)

* Shrink the size of the anyfunc table in `VMContext`

This commit shrinks the size of the `VMCallerCheckedAnyfunc` table
allocated into a `VMContext` to be the size of the number of "escaped"
functions in a module rather than the number of functions in a module.
Escaped functions include exports, table elements, etc, and are
typically an order of magnitude smaller than the number of functions in
general. This should greatly shrink the `VMContext` for some modules
which while we aren't necessarily having any problems with that today
shouldn't cause any problems in the future.

The original motivation for this was that this came up during the recent
lazy-table-initialization work and while it no longer has a direct
performance benefit since tables aren't initialized at all on
instantiation it should still improve long-running instances
theoretically with smaller `VMContext` allocations as well as better
locality between anyfuncs.

* Fix some tests

* Remove redundant hash set

* Use a helper for pushing function type information

* Use a more descriptive `is_escaping` method

* Clarify a comment

* Fix condition
This commit is contained in:
Alex Crichton
2022-02-28 10:11:04 -06:00
committed by GitHub
parent b57dc5e334
commit 2a6969d2bd
8 changed files with 126 additions and 58 deletions

View File

@@ -6,7 +6,9 @@ use anyhow::Result;
use std::any::Any;
use std::panic::{self, AssertUnwindSafe};
use std::sync::Arc;
use wasmtime_environ::{EntityIndex, FunctionInfo, Module, ModuleType, SignatureIndex};
use wasmtime_environ::{
AnyfuncIndex, EntityIndex, FunctionInfo, Module, ModuleType, SignatureIndex,
};
use wasmtime_jit::{CodeMemory, ProfilingAgent};
use wasmtime_runtime::{
Imports, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,
@@ -152,7 +154,8 @@ pub unsafe fn create_raw_function(
let sig_id = SignatureIndex::from_u32(u32::max_value() - 1);
module.types.push(ModuleType::Function(sig_id));
let func_id = module.functions.push(sig_id);
let func_id = module.push_escaped_function(sig_id, AnyfuncIndex::from_u32(0));
module.num_escaped_funcs = 1;
module
.exports
.insert(String::new(), EntityIndex::Function(func_id));

View File

@@ -2,7 +2,9 @@ use crate::store::{InstanceId, StoreOpaque};
use crate::trampoline::create_handle;
use crate::{GlobalType, Mutability, Val};
use anyhow::Result;
use wasmtime_environ::{EntityIndex, Global, GlobalInit, Module, ModuleType, SignatureIndex};
use wasmtime_environ::{
AnyfuncIndex, EntityIndex, Global, GlobalInit, Module, ModuleType, SignatureIndex,
};
use wasmtime_runtime::VMFunctionImport;
pub fn create_global(store: &mut StoreOpaque, gt: &GlobalType, val: Val) -> Result<InstanceId> {
@@ -40,8 +42,9 @@ pub fn create_global(store: &mut StoreOpaque, gt: &GlobalType, val: Val) -> Resu
let sig_id = SignatureIndex::from_u32(u32::max_value() - 1);
one_signature = Some((sig_id, f.type_index));
module.types.push(ModuleType::Function(sig_id));
let func_index = module.functions.push(sig_id);
let func_index = module.push_escaped_function(sig_id, AnyfuncIndex::from_u32(0));
module.num_imported_funcs = 1;
module.num_escaped_funcs = 1;
module
.initializers
.push(wasmtime_environ::Initializer::Import {