implement PLN-0033 vm string sharing hot path
This commit is contained in:
@@ -639,7 +639,10 @@ impl VirtualMachine {
|
||||
}
|
||||
OpCode::Add => self.binary_op(opcode, start_pc as u32, |a, b| match (&a, &b) {
|
||||
(Value::String(_), _) | (_, Value::String(_)) => {
|
||||
Ok(Value::String(format!("{}{}", a.to_string(), b.to_string())))
|
||||
let mut out = String::with_capacity(a.string_len_hint() + b.string_len_hint());
|
||||
a.append_to_string(&mut out);
|
||||
b.append_to_string(&mut out);
|
||||
Ok(Value::String(out.into()))
|
||||
}
|
||||
(Value::Int32(a), Value::Int32(b)) => Ok(Value::Int32(a.wrapping_add(*b))),
|
||||
(Value::Int64(a), Value::Int64(b)) => Ok(Value::Int64(a.wrapping_add(*b))),
|
||||
@@ -1213,6 +1216,7 @@ impl VirtualMachine {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn new_test_vm(rom: Vec<u8>, constant_pool: Vec<Value>) -> VirtualMachine {
|
||||
let rom_len = rom.len() as u32;
|
||||
@@ -1642,6 +1646,19 @@ mod tests {
|
||||
assert_eq!(vm.peek().unwrap(), &Value::String("hello".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shared_string_clone_reuses_materialized_payload() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
let payload: Arc<str> = "shared-global".into();
|
||||
vm.globals.push(Value::String(payload.clone()));
|
||||
|
||||
let cloned = vm.globals[0].clone();
|
||||
match cloned {
|
||||
Value::String(cloned_payload) => assert!(Arc::ptr_eq(&payload, &cloned_payload)),
|
||||
other => panic!("expected string clone from globals, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_const_invalid_index_traps_oob() {
|
||||
let mut rom = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user