Skip to content

Commit 81d9fc5

Browse files
committed
optimize CallStack instance caching
1 parent 391f94c commit 81d9fc5

File tree

1 file changed

+13
-20
lines changed
  • crates/wasmi/src/engine/executor/handler

1 file changed

+13
-20
lines changed

crates/wasmi/src/engine/executor/handler/state.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{
2-
collections::HeadVec,
32
core::{ReadAs, UntypedVal, WriteAs},
43
engine::{
54
executor::{handler::utils::extract_mem0, CodeMap},
@@ -296,15 +295,15 @@ impl ValueStack {
296295
#[derive(Debug)]
297296
pub struct CallStack {
298297
frames: Vec<Frame>,
299-
instances: HeadVec<NonNull<InstanceEntity>>,
298+
instance: Option<NonNull<InstanceEntity>>,
300299
max_height: usize,
301300
}
302301

303302
impl CallStack {
304303
fn new(max_height: usize) -> Self {
305304
Self {
306305
frames: Vec::new(),
307-
instances: HeadVec::default(),
306+
instance: None,
308307
max_height,
309308
}
310309
}
@@ -315,7 +314,7 @@ impl CallStack {
315314

316315
fn reset(&mut self) {
317316
self.frames.clear();
318-
self.instances.clear();
317+
self.instance = None;
319318
}
320319

321320
fn top_start(&self) -> usize {
@@ -348,14 +347,14 @@ impl CallStack {
348347
Some(caller_ip) => self.sync_ip(caller_ip),
349348
None => debug_assert!(self.frames.is_empty()),
350349
}
351-
let changes_instance = instance.is_some();
352-
if let Some(instance) = instance {
353-
self.instances.push(instance);
354-
}
350+
let prev_instance = match instance {
351+
Some(instance) => self.instance.replace(instance),
352+
None => self.instance,
353+
};
355354
self.frames.push(Frame {
356355
ip: callee_ip,
357356
start,
358-
changes_instance,
357+
instance: prev_instance,
359358
});
360359
Ok(())
361360
}
@@ -367,22 +366,16 @@ impl CallStack {
367366
let top = self.top()?;
368367
let ip = top.ip;
369368
let start = top.start;
370-
let instance = popped.changes_instance.then(|| {
371-
self.instances
372-
.pop()
373-
.expect("must have an instance if changed");
374-
self.instances
375-
.last()
376-
.copied()
377-
.expect("must have another instance since frame stack is non-empty")
378-
});
379-
Some((ip, start, instance))
369+
if let Some(instance) = popped.instance {
370+
self.instance = Some(instance);
371+
}
372+
Some((ip, start, popped.instance))
380373
}
381374
}
382375

383376
#[derive(Debug)]
384377
pub struct Frame {
385378
pub ip: Ip,
386379
start: usize,
387-
changes_instance: bool,
380+
instance: Option<NonNull<InstanceEntity>>,
388381
}

0 commit comments

Comments
 (0)