From d2ea44e37cc6509fb6c61f7563df20ec3c855357 Mon Sep 17 00:00:00 2001 From: adam-rhebo Date: Tue, 22 Oct 2019 17:23:25 +0200 Subject: [PATCH] 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. --- src/runner.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/runner.rs b/src/runner.rs index bcc1853..41e11b4 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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