refactor(tvix/eval): Box Value::String

NixString is *quite* large - like 80 bytes - because of the extra
capacity value for BString and because of the context. We want to keep
Value small since we're passing it around a lot, so let's box the
NixString inside Value::String to save on some memory, and make cloning
ostensibly a little cheaper

Change-Id: I343c8b4e7f61dc3dcbbaba4382efb3b3e5bbabb2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10729
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
Aspen Smith 2024-02-01 12:28:29 -05:00 committed by aspen
parent 4c5d9fa356
commit 5f0f4ea374
15 changed files with 69 additions and 72 deletions

View file

@ -987,9 +987,10 @@ where
}
}
// FIXME: consume immediately here the String.
self.stack
.push(Value::String(NixString::new_context_from(context, out)));
.push(Value::String(Box::new(NixString::new_context_from(
context, out,
))));
Ok(())
}
@ -1250,7 +1251,7 @@ async fn add_values(co: GenCo, a: Value, b: Value) -> Result<Value, ErrorKind> {
Err(c) => Value::Catchable(c),
}
}
(Value::String(s1), Value::String(s2)) => Value::String(s1.concat(&s2)),
(Value::String(s1), Value::String(s2)) => Value::String(Box::new(s1.concat(&s2))),
(Value::String(s1), v) => generators::request_string_coerce(
&co,
v,
@ -1261,7 +1262,7 @@ async fn add_values(co: GenCo, a: Value, b: Value) -> Result<Value, ErrorKind> {
},
)
.await
.map(|s2| Value::String(s1.concat(&s2)))
.map(|s2| Value::String(Box::new(s1.concat(&s2))))
.into(),
(a @ Value::Integer(_), b) | (a @ Value::Float(_), b) => arithmetic_op!(&a, &b, +)?,
(a, b) => {
@ -1284,7 +1285,7 @@ async fn add_values(co: GenCo, a: Value, b: Value) -> Result<Value, ErrorKind> {
)
.await;
match (r1, r2) {
(Ok(s1), Ok(s2)) => Value::String(s1.concat(&s2)),
(Ok(s1), Ok(s2)) => Value::String(Box::new(s1.concat(&s2))),
(Err(c), _) => return Ok(Value::Catchable(c)),
(_, Err(c)) => return Ok(Value::Catchable(c)),
}