From 5e31096154d32bf128f8eb172f84ca3b7b968bc8 Mon Sep 17 00:00:00 2001 From: Aspen Smith Date: Tue, 13 Feb 2024 14:59:42 -0500 Subject: feat(tvix/eval): Store string context alongside data Previously, Nix strings were represented as a Box (within Value) pointing to a tuple of an optional context, and another Box pointing to the actual string allocation itself. This is pretty inefficient, both in terms of memory usage (we use 48 whole bytes for a None context!) and in terms of the extra indirection required to get at the actual data. It was necessary, however, because with native Rust DSTs if we had something like `struct NixString(Option, BStr)` we could only pass around *fat* pointers to that value (with the length in the pointer) and that'd make Value need to be bigger (which is a waste of both memory and cache space, since that memory would be unused for all other Values). Instead, this commit implements *manual* allocation of a packed string representation, with the length *in the allocation* as a field past the context. This requires a big old pile of unsafe Rust, but the payoff is clear: hello outpath time: [882.18 ms 897.16 ms 911.23 ms] change: [-15.143% -13.819% -12.500%] (p = 0.00 < 0.05) Performance has improved. Fortunately this change can be localized entirely within value/string.rs, since we were abstracting things out nicely. Change-Id: Ibf56dd16c9c503884f64facbb7f0ac596463efb6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10852 Tested-by: BuildkiteCI Reviewed-by: raitobezarius Autosubmit: aspen --- tvix/eval/src/value/mod.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'tvix/eval/src/value/mod.rs') diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index 095bc269d497..e8e27e5968de 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -47,7 +47,7 @@ pub enum Value { Bool(bool), Integer(i64), Float(f64), - String(Box), + String(NixString), #[serde(skip)] Path(Box), @@ -192,7 +192,7 @@ where T: Into, { fn from(t: T) -> Self { - Self::String(Box::new(t.into())) + Self::String(t.into()) } } @@ -333,9 +333,7 @@ impl Value { let value = if let Some(v) = vals.pop() { v.force(co, span.clone()).await? } else { - return Ok(Value::String(Box::new(NixString::new_context_from( - context, result, - )))); + return Ok(Value::String(NixString::new_context_from(context, result))); }; let coerced: Result = match (value, kind) { // coercions that are always done @@ -700,7 +698,7 @@ impl Value { /// everytime you want a string. pub fn to_str(&self) -> Result { match self { - Value::String(s) if !s.has_context() => Ok((**s).clone()), + Value::String(s) if !s.has_context() => Ok((*s).clone()), Value::Thunk(thunk) => Self::to_str(&thunk.value()), other => Err(type_error("contextless strings", other)), } @@ -711,7 +709,7 @@ impl Value { NixString, "contextful string", Value::String(s), - (**s).clone() + (*s).clone() ); gen_cast!(to_path, Box, "path", Value::Path(p), p.clone()); gen_cast!(to_attrs, Box, "set", Value::Attrs(a), a.clone()); -- cgit 1.4.1