Use smallvec for avoid allocations in the trampoline (#1965)

This commit is contained in:
Sergei Shulepov
2020-07-02 18:24:03 +02:00
committed by GitHub
parent 47a218f908
commit ce51995828
3 changed files with 13 additions and 3 deletions

1
Cargo.lock generated
View File

@@ -2329,6 +2329,7 @@ dependencies = [
"log",
"region",
"rustc-demangle",
"smallvec",
"target-lexicon",
"tempfile",
"wasmparser 0.58.0",

View File

@@ -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"

View File

@@ -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,