Box ShellError in Value::Error (#8375)

# Description

Our `ShellError` at the moment has a `std::mem::size_of<ShellError>` of
136 bytes (on AMD64). As a result `Value` directly storing the struct
also required 136 bytes (thanks to alignment requirements).

This change stores the `Value::Error` `ShellError` on the heap.

Pro:
- Value now needs just 80 bytes
- Should be 1 cacheline less (still at least 2 cachelines)

Con:
- More small heap allocations when dealing with `Value::Error`
  - More heap fragmentation
  - Potential for additional required memcopies

# Further code changes

Includes a small refactor of `try` due to a type mismatch in its large
match.

# User-Facing Changes

None for regular users.

Plugin authors may have to update their matches on `Value` if they use
`nu-protocol`

Needs benchmarking to see if there is a benefit in real world workloads.
**Update** small improvements in runtime for workloads with high volume
of values. Significant reduction in maximum resident set size, when many
values are held in memory.

# Tests + Formatting
This commit is contained in:
Stefan Holderbach
2023-03-12 09:57:27 +01:00
committed by GitHub
parent c26d91fb61
commit a52386e837
153 changed files with 648 additions and 520 deletions

View File

@ -145,7 +145,7 @@ impl PipelineData {
items.push(val);
}
Err(e) => {
return Value::Error { error: e };
return Value::Error { error: Box::new(e) };
}
}
}
@ -165,7 +165,9 @@ impl PipelineData {
output.extend(item);
}
Err(err) => {
return Value::Error { error: err };
return Value::Error {
error: Box::new(err),
};
}
}
}
@ -180,7 +182,9 @@ impl PipelineData {
match item.as_string() {
Ok(s) => output.push_str(&s),
Err(err) => {
return Value::Error { error: err };
return Value::Error {
error: Box::new(err),
};
}
}
}
@ -227,7 +231,7 @@ impl PipelineData {
Err(error) => Err(error),
},
// Propagate errors by explicitly matching them before the final case.
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
other => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "list, binary, raw data or range".into(),
wrong_type: other.get_type().to_string(),
@ -397,7 +401,7 @@ impl PipelineData {
.map(f)
.into_pipeline_data(ctrlc)),
PipelineData::Value(v, ..) => match f(v) {
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
v => Ok(v.into_pipeline_data()),
},
}
@ -770,7 +774,7 @@ impl PipelineData {
let working_set = StateWorkingSet::new(engine_state);
// Value::Errors must always go to stderr, not stdout.
is_err = true;
format_error(&working_set, &error)
format_error(&working_set, &*error)
} else if no_newline {
item.into_string("", config)
} else {
@ -821,7 +825,9 @@ impl IntoIterator for PipelineData {
)),
Err(error) => PipelineIterator(PipelineData::ListStream(
ListStream {
stream: Box::new(std::iter::once(Value::Error { error })),
stream: Box::new(std::iter::once(Value::Error {
error: Box::new(error),
})),
ctrlc: None,
},
metadata,
@ -863,7 +869,7 @@ pub fn print_if_stream(
let mut exit_codes: Vec<_> = exit_code.into_iter().collect();
return match exit_codes.pop() {
#[cfg(unix)]
Some(Value::Error { error }) => Err(error),
Some(Value::Error { error }) => Err(*error),
Some(Value::Int { val, .. }) => Ok(val),
_ => Ok(0),
};
@ -887,7 +893,9 @@ impl Iterator for PipelineIterator {
..
} => stream.next().map(|x| match x {
Ok(x) => x,
Err(err) => Value::Error { error: err },
Err(err) => Value::Error {
error: Box::new(err),
},
}),
}
}

View File

@ -93,7 +93,7 @@ pub enum Value {
span: Span,
},
Error {
error: ShellError,
error: Box<ShellError>,
},
Binary {
val: Vec<u8>,
@ -150,7 +150,9 @@ impl Clone for Value {
match val.collect() {
Ok(val) => val,
// this is a bit weird, but because clone() is infallible...
Err(error) => Value::Error { error },
Err(error) => Value::Error {
error: Box::new(error),
},
}
}
Value::List { vals, span } => Value::List {
@ -346,7 +348,7 @@ impl Value {
/// Get the span for the current value
pub fn span(&self) -> Result<Span, ShellError> {
match self {
Value::Error { error } => Err(error.clone()),
Value::Error { error } => Err(*error.clone()),
Value::Bool { span, .. } => Ok(*span),
Value::Int { span, .. } => Ok(*span),
Value::Float { span, .. } => Ok(*span),
@ -494,7 +496,7 @@ impl Value {
config: &Config,
) -> Result<String, ShellError> {
if let Value::Error { error } = self {
Err(error.to_owned())
Err(*error.to_owned())
} else {
Ok(self.into_string(separator, config))
}
@ -535,7 +537,9 @@ impl Value {
Value::LazyRecord { val, .. } => {
let collected = match val.collect() {
Ok(val) => val,
Err(error) => Value::Error { error },
Err(error) => Value::Error {
error: Box::new(error),
},
};
collected.into_string(separator, config)
}
@ -773,7 +777,7 @@ impl Value {
err_message: "Can't access record values with a row index. Try specifying a column name instead".into(),
span: *origin_span, }, *origin_span)
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
x => {
err_or_null!(
ShellError::IncompatiblePathAccess {
@ -880,11 +884,11 @@ impl Value {
Value::nothing(*origin_span)
} else {
Value::Error {
error: ShellError::CantFindColumn {
error: Box::new(ShellError::CantFindColumn {
col_name: column_name.to_string(),
span: *origin_span,
src_span: val.span().unwrap_or(*span),
},
}),
}
});
}
@ -894,11 +898,11 @@ impl Value {
Value::nothing(*origin_span)
} else {
Value::Error {
error: ShellError::CantFindColumn {
error: Box::new(ShellError::CantFindColumn {
col_name: column_name.to_string(),
span: *origin_span,
src_span: val.span().unwrap_or(*span),
},
}),
}
});
}
@ -922,7 +926,7 @@ impl Value {
Value::CustomValue { val, .. } => {
current = val.follow_path_string(column_name.clone(), *origin_span)?;
}
Value::Error { error } => err_or_null!(error.to_owned(), *origin_span),
Value::Error { error } => err_or_null!(*error.to_owned(), *origin_span),
x => {
err_or_null!(
ShellError::IncompatiblePathAccess {
@ -938,7 +942,7 @@ impl Value {
// If a single Value::Error was produced by the above (which won't happen if nullify_errors is true), unwrap it now.
// Note that Value::Errors inside Lists remain as they are, so that the rest of the list can still potentially be used.
if let Value::Error { error } = current {
Err(error)
Err(*error)
} else {
Ok(current)
}
@ -955,7 +959,7 @@ impl Value {
let new_val = callback(&orig.follow_cell_path(cell_path, false, false)?);
match new_val {
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
new_val => self.upsert_data_at_cell_path(cell_path, new_val),
}
}
@ -1005,7 +1009,7 @@ impl Value {
}
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.to_string(),
@ -1042,7 +1046,7 @@ impl Value {
}
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.to_string(),
@ -1066,7 +1070,7 @@ impl Value {
});
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::NotAList {
dst_span: *span,
@ -1093,7 +1097,8 @@ impl Value {
let new_val = callback(&orig.follow_cell_path(cell_path, false, false)?);
match new_val {
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
new_val => self.update_data_at_cell_path(cell_path, new_val),
}
}
@ -1135,7 +1140,7 @@ impl Value {
});
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.to_string(),
@ -1169,7 +1174,7 @@ impl Value {
});
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.to_string(),
@ -1191,7 +1196,7 @@ impl Value {
});
}
}
Value::Error { error } => return Err(error.to_owned()),
Value::Error { error } => return Err(*error.to_owned()),
v => {
return Err(ShellError::NotAList {
dst_span: *span,
@ -1436,7 +1441,7 @@ impl Value {
vals.push(new_val.clone());
}
// SIGH...
Value::Error { error } => return Err(error.clone()),
Value::Error { error } => return Err(*error.clone()),
_ => {
return Err(ShellError::UnsupportedInput(
"expected table or record".into(),

View File

@ -218,7 +218,7 @@ impl Iterator for RangeIterator {
} else {
self.done = true;
return Some(Value::Error {
error: ShellError::CannotCreateRange { span: self.span },
error: Box::new(ShellError::CannotCreateRange { span: self.span }),
});
};
@ -237,7 +237,9 @@ impl Iterator for RangeIterator {
Err(error) => {
self.done = true;
return Some(Value::Error { error });
return Some(Value::Error {
error: Box::new(error),
});
}
};
std::mem::swap(&mut self.curr, &mut next);