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

@ -90,7 +90,8 @@ fn action(
"binhex" => GeneralPurpose::new(&alphabet::BIN_HEX, NO_PAD),
"crypt" => GeneralPurpose::new(&alphabet::CRYPT, NO_PAD),
"mutf7" => GeneralPurpose::new(&alphabet::IMAP_MUTF7, NO_PAD),
not_valid => return Value::Error { error:ShellError::GenericError(
not_valid => return Value::Error { error:
Box::new(ShellError::GenericError(
"value is not an accepted character set".to_string(),
format!(
"{not_valid} is not a valid character-set.\nPlease use `help encode base64` to see a list of valid character sets."
@ -98,7 +99,7 @@ fn action(
Some(config_character_set.span),
None,
Vec::new(),
)}
))}
};
match input {
// Propagate existing errors.
@ -111,13 +112,13 @@ fn action(
Ok(bytes_written) => bytes_written,
Err(err) => {
return Value::Error {
error: ShellError::GenericError(
error: Box::new(ShellError::GenericError(
"Error encoding data".into(),
err.to_string(),
Some(Span::unknown()),
None,
Vec::new(),
),
)),
}
}
};
@ -125,13 +126,13 @@ fn action(
Value::string(std::str::from_utf8(&enc_vec).unwrap_or(""), command_span)
}
ActionType::Decode => Value::Error {
error: ShellError::UnsupportedInput(
error: Box::new(ShellError::UnsupportedInput(
"Binary data can only be encoded".to_string(),
"value originates from here".into(),
command_span,
// This line requires the Value::Error {} match above.
input.expect_span(),
),
)),
},
},
Value::String {
@ -158,20 +159,20 @@ fn action(
match String::from_utf8(decoded_value) {
Ok(string_value) => Value::string(string_value, command_span),
Err(e) => Value::Error {
error: ShellError::GenericError(
error: Box::new(ShellError::GenericError(
"base64 payload isn't a valid utf-8 sequence"
.to_owned(),
e.to_string(),
Some(*value_span),
Some("consider using the `--binary` flag".to_owned()),
Vec::new(),
),
)),
},
}
}
}
Err(_) => Value::Error {
error: ShellError::GenericError(
error: Box::new(ShellError::GenericError(
"value could not be base64 decoded".to_string(),
format!(
"invalid base64 input for character set {}",
@ -180,17 +181,17 @@ fn action(
Some(command_span),
None,
Vec::new(),
),
)),
},
}
}
}
}
other => Value::Error {
error: ShellError::TypeMismatch {
error: Box::new(ShellError::TypeMismatch {
err_message: format!("string or binary, not {}", other.get_type()),
span: other.span().unwrap_or(command_span),
},
}),
},
}
}

View File

@ -77,7 +77,7 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
PipelineData::Value(v, ..) => match v {
Value::Binary { val: bytes, .. } => super::encoding::decode(head, encoding, &bytes)
.map(|val| val.into_pipeline_data()),
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
_ => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: v.get_type().to_string(),

View File

@ -100,7 +100,7 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
super::encoding::encode(head, encoding, &s, span, ignore_errors)
.map(|val| val.into_pipeline_data())
}
Value::Error { error } => Err(error),
Value::Error { error } => Err(*error),
_ => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: v.get_type().to_string(),