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

@ -68,12 +68,12 @@ fn abs_helper(val: Value, head: Span) -> Value {
},
Value::Error { .. } => val,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -77,23 +77,23 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
Value::Float { val, span }
} else {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'arccos' undefined for values outside the closed interval [-1, 1].".into(),
"value originates from here".into(),
head,
span,
),
)),
}
}
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -67,23 +67,23 @@ fn operate(value: Value, head: Span) -> Value {
Value::Float { val, span }
} else {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'arccosh' undefined for values below 1.".into(),
"value originates from here".into(),
head,
span,
),
)),
}
}
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -78,23 +78,23 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
Value::Float { val, span }
} else {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'arcsin' undefined for values outside the closed interval [-1, 1].".into(),
"value originates from here".into(),
head,
span,
),
)),
}
}
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -67,12 +67,12 @@ fn operate(value: Value, head: Span) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -78,12 +78,12 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -67,23 +67,23 @@ fn operate(value: Value, head: Span) -> Value {
Value::Float { val, span }
} else {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'arctanh' undefined for values outside the open interval (-1, 1).".into(),
"value originates from here".into(),
head,
span,
),
)),
}
}
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -64,12 +64,12 @@ fn operate(value: Value, head: Span) -> Value {
},
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -88,12 +88,12 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -69,12 +69,12 @@ fn operate(value: Value, head: Span) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -64,12 +64,12 @@ fn operate(value: Value, head: Span) -> Value {
},
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -67,23 +67,23 @@ fn operate(value: Value, head: Span) -> Value {
Value::Float { val, span }
} else {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'ln' undefined for values outside the open interval (0, Inf).".into(),
"value originates from here".into(),
head,
span,
),
)),
}
}
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -96,13 +96,13 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
if val <= 0.0 {
return Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"'math log' undefined for values outside the open interval (0, Inf)."
.into(),
"value originates from here".into(),
head,
span,
),
)),
};
}
// Specialize for better precision/performance
@ -118,12 +118,12 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -132,7 +132,7 @@ pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellEr
Value::Filesize { val, .. } => {
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize))
}
Value::Error { error } => Err(error.clone()),
Value::Error { error } => Err(*error.clone()),
other => Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
"value originates from here".into(),

View File

@ -112,7 +112,7 @@ pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
| Value::Duration { .. } => {
acc = acc.add(head, value, head)?;
}
Value::Error { error } => return Err(error.clone()),
Value::Error { error } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput(
"Attempted to compute the sum of a value that cannot be summed".to_string(),
@ -145,7 +145,7 @@ pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellE
Value::Int { .. } | Value::Float { .. } => {
acc = acc.mul(head, value, head)?;
}
Value::Error { error } => return Err(error.clone()),
Value::Error { error } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput(
"Attempted to compute the product of a value that cannot be multiplied"

View File

@ -95,12 +95,12 @@ fn operate(value: Value, head: Span, precision: Option<i64>) -> Value {
Value::Int { .. } => value,
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -88,12 +88,12 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -69,12 +69,12 @@ fn operate(value: Value, head: Span) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -73,24 +73,24 @@ fn operate(value: Value, head: Span) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}
fn error_negative_sqrt(head: Span, span: Span) -> Value {
Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
String::from("Can't square root a negative number"),
"value originates from here".into(),
head,
span,
),
)),
}
}

View File

@ -86,12 +86,12 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -68,12 +68,12 @@ fn operate(value: Value, head: Span) -> Value {
}
Value::Error { .. } => value,
other => Value::Error {
error: ShellError::OnlySupportsThisInputType {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
},
}),
},
}
}

View File

@ -34,7 +34,7 @@ fn helper_for_tables(
.or_insert_with(|| vec![value.clone()]);
}
}
Value::Error { error } => return Err(error.clone()),
Value::Error { error } => return Err(*error.clone()),
_ => {
//Turns out we are not dealing with a table
return mf(values, val.expect_span(), &name);

View File

@ -64,7 +64,7 @@ fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
for value in values {
let v = match &value {
Value::Int { .. } | Value::Float { .. } => Ok(value),
Value::Error { error } => Err(error.clone()),
Value::Error { error } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput(
"Attempted to compute the sum of squares of a non-integer, non-float value"
.to_string(),