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:
parent
f19e1c27fc
commit
d2ea44e37c
|
@ -1289,14 +1289,8 @@ impl FunctionContext {
|
|||
debug_assert!(!self.is_initialized);
|
||||
|
||||
let num_locals = locals.iter().map(|l| l.count() as usize).sum();
|
||||
let locals = vec![Default::default(); num_locals];
|
||||
|
||||
// TODO: Replace with extend.
|
||||
for local in locals {
|
||||
value_stack
|
||||
.push(local)
|
||||
.map_err(|_| TrapKind::StackOverflow)?;
|
||||
}
|
||||
value_stack.extend(num_locals)?;
|
||||
|
||||
self.is_initialized = true;
|
||||
Ok(())
|
||||
|
@ -1442,6 +1436,18 @@ impl ValueStack {
|
|||
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]
|
||||
fn len(&self) -> usize {
|
||||
self.sp
|
||||
|
|
Loading…
Reference in New Issue