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:
parent
4c5d9fa356
commit
5f0f4ea374
15 changed files with 69 additions and 72 deletions
|
|
@ -91,7 +91,7 @@ fn leaf_value() -> impl Strategy<Value = Value> {
|
|||
any::<bool>().prop_map(Bool),
|
||||
any::<i64>().prop_map(Integer),
|
||||
any::<f64>().prop_map(Float),
|
||||
any::<NixString>().prop_map(String),
|
||||
any::<Box<NixString>>().prop_map(String),
|
||||
any::<OsString>().prop_map(|s| Path(PathBuf::from(s).into_boxed_path())),
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -360,7 +360,7 @@ impl NixAttrs {
|
|||
let key = stack_slice.pop().unwrap();
|
||||
|
||||
match key {
|
||||
Value::String(ks) => set_attr(&mut attrs, ks, value)?,
|
||||
Value::String(ks) => set_attr(&mut attrs, *ks, value)?,
|
||||
|
||||
Value::Null => {
|
||||
// This is in fact valid, but leads to the value
|
||||
|
|
@ -414,9 +414,13 @@ impl IntoIterator for NixAttrs {
|
|||
fn attempt_optimise_kv(slice: &mut [Value]) -> Option<NixAttrs> {
|
||||
let (name_idx, value_idx) = {
|
||||
match (&slice[2], &slice[0]) {
|
||||
(Value::String(s1), Value::String(s2)) if (*s1 == *NAME_S && *s2 == *VALUE_S) => (3, 1),
|
||||
(Value::String(s1), Value::String(s2)) if (**s1 == *NAME_S && **s2 == *VALUE_S) => {
|
||||
(3, 1)
|
||||
}
|
||||
|
||||
(Value::String(s1), Value::String(s2)) if (*s1 == *VALUE_S && *s2 == *NAME_S) => (1, 3),
|
||||
(Value::String(s1), Value::String(s2)) if (**s1 == *VALUE_S && **s2 == *NAME_S) => {
|
||||
(1, 3)
|
||||
}
|
||||
|
||||
// Technically this branch lets type errors pass,
|
||||
// but they will be caught during normal attribute
|
||||
|
|
|
|||
|
|
@ -14,11 +14,8 @@ fn test_empty_attrs() {
|
|||
|
||||
#[test]
|
||||
fn test_simple_attrs() {
|
||||
let attrs = NixAttrs::construct(
|
||||
1,
|
||||
vec![Value::String("key".into()), Value::String("value".into())],
|
||||
)
|
||||
.expect("simple attr construction should succeed");
|
||||
let attrs = NixAttrs::construct(1, vec![Value::from("key"), Value::from("value")])
|
||||
.expect("simple attr construction should succeed");
|
||||
|
||||
assert!(
|
||||
matches!(attrs, NixAttrs(AttrsRep::Im(_))),
|
||||
|
|
@ -28,9 +25,9 @@ fn test_simple_attrs() {
|
|||
|
||||
#[test]
|
||||
fn test_kv_attrs() {
|
||||
let name_val = Value::String("name".into());
|
||||
let value_val = Value::String("value".into());
|
||||
let meaning_val = Value::String("meaning".into());
|
||||
let name_val = Value::from("name");
|
||||
let value_val = Value::from("value");
|
||||
let meaning_val = Value::from("meaning");
|
||||
let forty_two_val = Value::Integer(42);
|
||||
|
||||
let kv_attrs = NixAttrs::construct(
|
||||
|
|
@ -64,9 +61,9 @@ fn test_empty_attrs_iter() {
|
|||
|
||||
#[test]
|
||||
fn test_kv_attrs_iter() {
|
||||
let name_val = Value::String("name".into());
|
||||
let value_val = Value::String("value".into());
|
||||
let meaning_val = Value::String("meaning".into());
|
||||
let name_val = Value::from("name");
|
||||
let value_val = Value::from("value");
|
||||
let meaning_val = Value::from("meaning");
|
||||
let forty_two_val = Value::Integer(42);
|
||||
|
||||
let kv_attrs = NixAttrs::construct(
|
||||
|
|
@ -92,11 +89,8 @@ fn test_kv_attrs_iter() {
|
|||
|
||||
#[test]
|
||||
fn test_map_attrs_iter() {
|
||||
let attrs = NixAttrs::construct(
|
||||
1,
|
||||
vec![Value::String("key".into()), Value::String("value".into())],
|
||||
)
|
||||
.expect("simple attr construction should succeed");
|
||||
let attrs = NixAttrs::construct(1, vec![Value::from("key"), Value::from("value")])
|
||||
.expect("simple attr construction should succeed");
|
||||
|
||||
let mut iter = attrs.iter().collect::<Vec<_>>().into_iter();
|
||||
let (k, v) = iter.next().unwrap();
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ pub enum Value {
|
|||
Bool(bool),
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(NixString),
|
||||
String(Box<NixString>),
|
||||
|
||||
#[serde(skip)]
|
||||
Path(Box<Path>),
|
||||
|
|
@ -186,7 +186,7 @@ where
|
|||
T: Into<NixString>,
|
||||
{
|
||||
fn from(t: T) -> Self {
|
||||
Self::String(t.into())
|
||||
Self::String(Box::new(t.into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -327,7 +327,9 @@ impl Value {
|
|||
let value = if let Some(v) = vals.pop() {
|
||||
v.force(co, span.clone()).await?
|
||||
} else {
|
||||
return Ok(Value::String(NixString::new_context_from(context, result)));
|
||||
return Ok(Value::String(Box::new(NixString::new_context_from(
|
||||
context, result,
|
||||
))));
|
||||
};
|
||||
let coerced: Result<BString, _> = match (value, kind) {
|
||||
// coercions that are always done
|
||||
|
|
@ -335,7 +337,7 @@ impl Value {
|
|||
if let Some(ctx) = s.context_mut() {
|
||||
context = context.join(ctx);
|
||||
}
|
||||
Ok(s.into())
|
||||
Ok((*s).into())
|
||||
}
|
||||
|
||||
// TODO(sterni): Think about proper encoding handling here. This needs
|
||||
|
|
@ -692,7 +694,7 @@ impl Value {
|
|||
/// everytime you want a string.
|
||||
pub fn to_str(&self) -> Result<NixString, ErrorKind> {
|
||||
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)),
|
||||
}
|
||||
|
|
@ -703,7 +705,7 @@ impl Value {
|
|||
NixString,
|
||||
"contextful string",
|
||||
Value::String(s),
|
||||
s.clone()
|
||||
(**s).clone()
|
||||
);
|
||||
gen_cast!(to_path, Box<Path>, "path", Value::Path(p), p.clone());
|
||||
gen_cast!(to_attrs, Box<NixAttrs>, "set", Value::Attrs(a), a.clone());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue