Skip to content

Commit

Permalink
lib/vm: move copy closer to where it happens
Browse files Browse the repository at this point in the history
The method Program.string_at() won't return a copy of its internal
string anymore.  It will instead return a reference to it, so when
we're simply printing a program, it doesn't unnecessarily copy
strings.
  • Loading branch information
clarete committed Oct 3, 2023
1 parent 5de56a1 commit 2a2b688
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions langlang_lib/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl Program {
}
}

pub fn string_at(&self, id: usize) -> String {
self.strings[id].clone()
pub fn string_at(&self, id: usize) -> &String {
&self.strings[id]
}
}

Expand Down Expand Up @@ -571,7 +571,7 @@ impl<'a> VM<'a> {
}
let expected = self.program.string_at(id);
match &self.source[self.cursor] {
Value::String(ref s) if s.value == expected => {
Value::String(ref s) if &s.value == expected => {
self.capture(self.source[self.cursor].clone())?;
self.advance_cursor()?;
continue;
Expand All @@ -588,17 +588,18 @@ impl<'a> VM<'a> {
break Err(Error::EOF);
}
match &self.source[self.cursor] {
Value::Char(ref current) if current.value == current_char => {}
Value::Char(ref current) if current.value == current_char => {
self.advance_cursor()?;
}
_ => {
break Err(Error::Matching(self.ffp, expected.clone()));
}
};
self.advance_cursor()?;
} {
Err(e) => self.fail(e)?,
Ok(()) => self.capture(value::String::new_val(
Span::new(start, self.pos()),
expected,
expected.clone(),
))?,
}
}
Expand Down

0 comments on commit 2a2b688

Please sign in to comment.