mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
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:
committed by
GitHub
parent
c26d91fb61
commit
a52386e837
@ -16,7 +16,7 @@ fn from_value_to_delimited_string(
|
||||
}
|
||||
Value::List { vals, span } => table_to_delimited(vals, span, separator, config, head),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { error } => Err(error.clone()),
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
v => Err(make_unsupported_input_error(v, head, v.expect_span())),
|
||||
}
|
||||
}
|
||||
@ -122,7 +122,7 @@ fn to_string_tagged_value(
|
||||
Value::Date { val, .. } => Ok(val.to_string()),
|
||||
Value::Nothing { .. } => Ok(String::new()),
|
||||
// Propagate existing errors
|
||||
Value::Error { error } => Err(error.clone()),
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
_ => Err(make_unsupported_input_error(v, head, span)),
|
||||
}
|
||||
}
|
||||
|
@ -70,12 +70,12 @@ impl Command for ToJson {
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert {
|
||||
error: Box::new(ShellError::CantConvert {
|
||||
to_type: "JSON".into(),
|
||||
from_type: value.get_type().to_string(),
|
||||
span,
|
||||
help: None,
|
||||
},
|
||||
}),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
@ -126,7 +126,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
|
||||
),
|
||||
|
||||
Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
|
||||
Value::Error { error } => return Err(error.clone()),
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
Value::Closure { .. } | Value::Block { .. } | Value::Range { .. } => nu_json::Value::Null,
|
||||
Value::Binary { val, .. } => {
|
||||
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
|
||||
|
@ -101,7 +101,7 @@ pub fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
||||
// FIXME: make durations use the shortest lossless representation.
|
||||
Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
|
||||
// Propagate existing errors
|
||||
Value::Error { error } => Err(error.clone()),
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
// FIXME: make filesizes use the shortest lossless representation.
|
||||
Value::Filesize { val, .. } => Ok(format!("{}b", *val)),
|
||||
Value::Float { val, .. } => {
|
||||
|
@ -77,7 +77,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
|
||||
toml::Value::String(code)
|
||||
}
|
||||
Value::Nothing { .. } => toml::Value::String("<Nothing>".to_string()),
|
||||
Value::Error { error } => return Err(error.clone()),
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
Value::Binary { val, .. } => toml::Value::Array(
|
||||
val.iter()
|
||||
.map(|x| toml::Value::Integer(*x as i64))
|
||||
@ -118,12 +118,12 @@ fn toml_into_pipeline_data(
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert {
|
||||
error: Box::new(ShellError::CantConvert {
|
||||
to_type: "TOML".into(),
|
||||
from_type: value_type.to_string(),
|
||||
span,
|
||||
help: None,
|
||||
},
|
||||
}),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
@ -137,7 +137,7 @@ fn value_to_toml_value(
|
||||
match v {
|
||||
Value::Record { .. } => helper(engine_state, v),
|
||||
// Propagate existing errors
|
||||
Value::Error { error } => Err(error.clone()),
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
format!("{:?} is not valid top-level TOML", v.get_type()),
|
||||
"value originates from here".into(),
|
||||
|
@ -79,7 +79,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
Value::Block { .. } => serde_yaml::Value::Null,
|
||||
Value::Closure { .. } => serde_yaml::Value::Null,
|
||||
Value::Nothing { .. } => serde_yaml::Value::Null,
|
||||
Value::Error { error } => return Err(error.clone()),
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
Value::Binary { val, .. } => serde_yaml::Value::Sequence(
|
||||
val.iter()
|
||||
.map(|x| serde_yaml::Value::Number(serde_yaml::Number::from(*x)))
|
||||
@ -111,12 +111,12 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert {
|
||||
error: Box::new(ShellError::CantConvert {
|
||||
to_type: "YAML".into(),
|
||||
from_type: value.get_type().to_string(),
|
||||
span: head,
|
||||
help: None,
|
||||
},
|
||||
}),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
|
Reference in New Issue
Block a user