implement PLN-0033 vm string sharing hot path
This commit is contained in:
@@ -75,7 +75,7 @@ impl From<BytecodeModule> for ProgramImage {
|
||||
ConstantPoolEntry::Int64(v) => Value::Int64(*v),
|
||||
ConstantPoolEntry::Float64(v) => Value::Float(*v),
|
||||
ConstantPoolEntry::Boolean(v) => Value::Boolean(*v),
|
||||
ConstantPoolEntry::String(v) => Value::String(v.clone()),
|
||||
ConstantPoolEntry::String(v) => Value::String(v.clone().into()),
|
||||
ConstantPoolEntry::Int32(v) => Value::Int32(*v),
|
||||
})
|
||||
.collect();
|
||||
@@ -99,7 +99,7 @@ impl From<ProgramImage> for BytecodeModule {
|
||||
Value::Int64(v) => ConstantPoolEntry::Int64(*v),
|
||||
Value::Float(v) => ConstantPoolEntry::Float64(*v),
|
||||
Value::Boolean(v) => ConstantPoolEntry::Boolean(*v),
|
||||
Value::String(v) => ConstantPoolEntry::String(v.clone()),
|
||||
Value::String(v) => ConstantPoolEntry::String(v.to_string()),
|
||||
Value::Int32(v) => ConstantPoolEntry::Int32(*v),
|
||||
Value::HeapRef(_) => ConstantPoolEntry::Null,
|
||||
})
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Opaque handle that references an object stored in the VM heap.
|
||||
///
|
||||
@@ -26,7 +28,7 @@ pub enum Value {
|
||||
/// Boolean value (true/false).
|
||||
Boolean(bool),
|
||||
/// UTF-8 string. Strings are immutable and usually come from the Constant Pool.
|
||||
String(String),
|
||||
String(Arc<str>),
|
||||
/// A handle to an object on the heap (opaque reference).
|
||||
HeapRef(HeapRef),
|
||||
/// Represents the absence of a value (equivalent to `null` or `undefined`).
|
||||
@@ -92,17 +94,46 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn append_to_string(&self, out: &mut String) {
|
||||
match self {
|
||||
Value::Int32(i) => {
|
||||
let _ = write!(out, "{}", i);
|
||||
}
|
||||
Value::Int64(i) => {
|
||||
let _ = write!(out, "{}", i);
|
||||
}
|
||||
Value::Float(f) => {
|
||||
let _ = write!(out, "{}", f);
|
||||
}
|
||||
Value::Boolean(b) => {
|
||||
let _ = write!(out, "{}", b);
|
||||
}
|
||||
Value::String(s) => out.push_str(s),
|
||||
Value::HeapRef(r) => {
|
||||
let _ = write!(out, "[HeapRef {}]", r.0);
|
||||
}
|
||||
Value::Null => out.push_str("null"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn string_len_hint(&self) -> usize {
|
||||
match self {
|
||||
Value::String(s) => s.len(),
|
||||
Value::Null => 4,
|
||||
Value::Boolean(true) => 4,
|
||||
Value::Boolean(false) => 5,
|
||||
Value::Int32(i) => i.to_string().len(),
|
||||
Value::Int64(i) => i.to_string().len(),
|
||||
Value::Float(f) => f.to_string().len(),
|
||||
Value::HeapRef(r) => 11 + r.0.to_string().len(),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::inherent_to_string)]
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Value::Int32(i) => i.to_string(),
|
||||
Value::Int64(i) => i.to_string(),
|
||||
Value::Float(f) => f.to_string(),
|
||||
Value::Boolean(b) => b.to_string(),
|
||||
Value::String(s) => s.clone(),
|
||||
Value::HeapRef(r) => format!("[HeapRef {}]", r.0),
|
||||
Value::Null => "null".to_string(),
|
||||
}
|
||||
let mut out = String::with_capacity(self.string_len_hint());
|
||||
self.append_to_string(&mut out);
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user