From ce51995828d6040c371dd78e187a7a98aeec6266 Mon Sep 17 00:00:00 2001 From: Sergei Shulepov Date: Thu, 2 Jul 2020 18:24:03 +0200 Subject: [PATCH] Use smallvec for avoid allocations in the trampoline (#1965) --- Cargo.lock | 1 + crates/wasmtime/Cargo.toml | 1 + crates/wasmtime/src/func.rs | 14 +++++++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 260306058a..9692147912 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2329,6 +2329,7 @@ dependencies = [ "log", "region", "rustc-demangle", + "smallvec", "target-lexicon", "tempfile", "wasmparser 0.58.0", diff --git a/crates/wasmtime/Cargo.toml b/crates/wasmtime/Cargo.toml index 9973906c70..88c6a822c5 100644 --- a/crates/wasmtime/Cargo.toml +++ b/crates/wasmtime/Cargo.toml @@ -25,6 +25,7 @@ rustc-demangle = "0.1.16" lazy_static = "1.4" log = "0.4.8" wat = { version = "1.0.18", optional = true } +smallvec = "1.4.0" [target.'cfg(target_os = "windows")'.dependencies] winapi = "0.3.7" diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index 9c2081846f..e9e7833569 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -2,6 +2,7 @@ use crate::runtime::StoreInner; use crate::trampoline::StoreInstanceHandle; use crate::{Extern, FuncType, Memory, Store, Trap, Val, ValType}; use anyhow::{bail, ensure, Context as _, Result}; +use smallvec::{smallvec, SmallVec}; use std::cmp::max; use std::fmt; use std::mem; @@ -247,14 +248,21 @@ impl Func { // We have a dynamic guarantee that `values_vec` has the right // number of arguments and the right types of arguments. As a result // we should be able to safely run through them all and read them. - let mut args = Vec::with_capacity(ty_clone.params().len()); + const STACK_ARGS: usize = 4; + const STACK_RETURNS: usize = 2; + let mut args: SmallVec<[Val; STACK_ARGS]> = + SmallVec::with_capacity(ty_clone.params().len()); let store = Store::upgrade(&store_weak).unwrap(); for (i, ty) in ty_clone.params().iter().enumerate() { unsafe { - args.push(Val::read_value_from(&store, values_vec.add(i), ty)); + let val = Val::read_value_from(&store, values_vec.add(i), ty); + args.push(val); } } - let mut returns = vec![Val::null(); ty_clone.results().len()]; + + let mut returns: SmallVec<[Val; STACK_RETURNS]> = + smallvec![Val::null(); ty_clone.results().len()]; + func( Caller { store: &store_weak,