Avoid temporary allocations during function context initialization (#217)

* Avoid temporary allocation when push locals during function invocation.

* Extend value stack for all locals at once.
This commit is contained in:
adam-rhebo 2019-10-22 17:23:25 +02:00 committed by Sergei Pepyakin
parent f19e1c27fc
commit d2ea44e37c
1 changed files with 13 additions and 7 deletions

View File

@ -1289,14 +1289,8 @@ impl FunctionContext {
debug_assert!(!self.is_initialized); debug_assert!(!self.is_initialized);
let num_locals = locals.iter().map(|l| l.count() as usize).sum(); let num_locals = locals.iter().map(|l| l.count() as usize).sum();
let locals = vec![Default::default(); num_locals];
// TODO: Replace with extend. value_stack.extend(num_locals)?;
for local in locals {
value_stack
.push(local)
.map_err(|_| TrapKind::StackOverflow)?;
}
self.is_initialized = true; self.is_initialized = true;
Ok(()) Ok(())
@ -1442,6 +1436,18 @@ impl ValueStack {
Ok(()) Ok(())
} }
fn extend(&mut self, len: usize) -> Result<(), TrapKind> {
let cells = self
.buf
.get_mut(self.sp..self.sp + len)
.ok_or_else(|| TrapKind::StackOverflow)?;
for cell in cells {
*cell = Default::default();
}
self.sp += len;
Ok(())
}
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
self.sp self.sp