Skip to content

Commit

Permalink
Cleanup/optimize the use of references vs clones when peeking, pushin…
Browse files Browse the repository at this point in the history
…g, updating values on stack, as well as jumping to labels in the activation.

10%ish improvement in tight loop benches when a lot of those kinds of operations are performed.
  • Loading branch information
rdaum committed Jan 14, 2024
1 parent e522cc2 commit a06d77c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 120 deletions.
2 changes: 1 addition & 1 deletion crates/kernel/src/builtins/bf_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ async fn bf_move<'a>(bf_args: &mut BfCallState<'a>) -> Result<BfRet, Error> {
// Accept verb has been called, and returned. Check the result. Should be on stack,
// unless short-circuited, in which case we assume *false*
let result = if !shortcircuit {
bf_args.exec_state.top().peek_top().unwrap()
bf_args.exec_state.top().peek_top().unwrap().clone()
} else {
v_int(0)
};
Expand Down
14 changes: 7 additions & 7 deletions crates/kernel/src/vm/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ impl Activation {
#[inline]
pub fn next_op(&mut self) -> Op {
assert!(self.pc < self.program.main_vector.len(), "pc out of bounds");
let op = self.program.main_vector[self.pc].clone();
let op = &self.program.main_vector[self.pc];
self.pc += 1;
op
op.clone()
}

#[inline]
Expand All @@ -347,8 +347,8 @@ impl Activation {
}

#[inline]
pub fn peek_top(&self) -> Option<Var> {
self.valstack.last().cloned()
pub fn peek_top(&self) -> Option<&Var> {
self.valstack.last()
}

#[inline]
Expand All @@ -363,10 +363,10 @@ impl Activation {
}

#[inline]
pub fn peek2(&self) -> (Var, Var) {
pub fn peek2(&self) -> (&Var, &Var) {
let l = self.valstack.len();
let (a, b) = (&self.valstack[l - 1], &self.valstack[l - 2]);
(a.clone(), b.clone())
(a, b)
}

#[inline]
Expand All @@ -376,7 +376,7 @@ impl Activation {
}

#[inline]
pub fn jump(&mut self, label_id: Label) {
pub fn jump(&mut self, label_id: &Label) {
let label = &self.program.jump_labels[label_id.0 as usize];
self.pc = label.position.0;
}
Expand Down
18 changes: 9 additions & 9 deletions crates/kernel/src/vm/exec_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ impl VMExecState {

/// Push a value onto the value stack
#[inline]
pub(crate) fn push(&mut self, v: &Var) {
self.top_mut().push(v.clone())
pub(crate) fn push(&mut self, v: Var) {
self.top_mut().push(v)
}

/// Non-destructively peek a range of values from the value stack.
Expand All @@ -168,19 +168,19 @@ impl VMExecState {

/// Return the top two values on the value stack.
#[inline]
pub(crate) fn peek2(&self) -> (Var, Var) {
pub(crate) fn peek2(&self) -> (&Var, &Var) {
self.top().peek2()
}

/// Update at a set offset in the value stack.
#[inline]
pub(crate) fn update(&mut self, amt: usize, v: &Var) {
self.top_mut().update(amt, v.clone())
pub(crate) fn update(&mut self, amt: usize, v: Var) {
self.top_mut().update(amt, v)
}

/// Return the top of the value stack.
#[inline]
pub(crate) fn peek_top(&self) -> Var {
pub(crate) fn peek_top(&self) -> &Var {
self.top().peek_top().expect("stack underflow")
}

Expand All @@ -192,19 +192,19 @@ impl VMExecState {

/// Jump to the given label.
#[inline]
pub(crate) fn jump(&mut self, label: Label) {
pub(crate) fn jump(&mut self, label: &Label) {
self.top_mut().jump(label)
}

/// Return the value of a local variable.
#[inline]
pub(crate) fn get_env(&self, id: Name) -> Option<&Var> {
pub(crate) fn get_env(&self, id: &Name) -> Option<&Var> {
self.top().environment.get(id.0 as usize)
}

/// Set the value of a local variable.
#[inline]
pub(crate) fn set_env(&mut self, id: Name, v: &Var) {
pub(crate) fn set_env(&mut self, id: &Name, v: Var) {
self.top_mut().environment.set(id.0 as usize, v.clone());
}
}
Loading

0 comments on commit a06d77c

Please sign in to comment.