From 0b3db024516ad7e04f78a9a4d052e084fbdba1db Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 27 Jan 2024 09:48:02 +0200 Subject: [PATCH] core: Make Value::Text reference counted Avoids lots of memory copies as values get passed through the different layers. --- core/sqlite3_ondisk.rs | 2 +- core/types.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/sqlite3_ondisk.rs b/core/sqlite3_ondisk.rs index 1d5434e16..194eaf75a 100644 --- a/core/sqlite3_ondisk.rs +++ b/core/sqlite3_ondisk.rs @@ -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)) } } } diff --git a/core/types.rs b/core/types.rs index b4f64fb7a..706bfd151 100644 --- a/core/types.rs +++ b/core/types.rs @@ -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), Blob(Vec), } @@ -27,7 +29,7 @@ impl FromValue for i64 { impl FromValue for String { fn from_value(value: &Value) -> Result { match value { - Value::Text(s) => Ok(s.clone()), + Value::Text(s) => Ok(s.to_string()), _ => anyhow::bail!("Expected text value"), } }