core: Make Value::Text reference counted

Avoids lots of memory copies as values get passed through the different
layers.
This commit is contained in:
Pekka Enberg
2024-01-27 09:48:02 +02:00
parent bb56ba0736
commit 0b3db02451
2 changed files with 5 additions and 3 deletions

View File

@@ -403,7 +403,7 @@ pub fn read_value(buf: &[u8], serial_type: &SerialType) -> Result<(Value, usize)
}
let bytes = buf[0..n].to_vec();
let value = unsafe { String::from_utf8_unchecked(bytes) };
Ok((Value::Text(value), n))
Ok((Value::Text(Arc::new(value)), n))
}
}
}

View File

@@ -1,3 +1,5 @@
use std::sync::Arc;
use anyhow::Result;
#[derive(Debug, Clone, PartialEq)]
@@ -5,7 +7,7 @@ pub enum Value {
Null,
Integer(i64),
Float(f64),
Text(String),
Text(Arc<String>),
Blob(Vec<u8>),
}
@@ -27,7 +29,7 @@ impl FromValue for i64 {
impl FromValue for String {
fn from_value(value: &Value) -> Result<Self> {
match value {
Value::Text(s) => Ok(s.clone()),
Value::Text(s) => Ok(s.to_string()),
_ => anyhow::bail!("Expected text value"),
}
}