mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 03:44:19 +01:00
Avoid clone in Signature::get_positional()
(#13338)
# Description `Signature::get_positional()` was returning an owned `PositionalArg`, which contains a bunch of strings. `ClosureEval` uses this in `try_add_arg`, making all of that unnecessary cloning a little bit hot. # User-Facing Changes Slightly better performance
This commit is contained in:
parent
f87cf895c2
commit
801cfae279
@ -485,15 +485,14 @@ impl Signature {
|
|||||||
(name, s)
|
(name, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_positional(&self, position: usize) -> Option<PositionalArg> {
|
pub fn get_positional(&self, position: usize) -> Option<&PositionalArg> {
|
||||||
if position < self.required_positional.len() {
|
if position < self.required_positional.len() {
|
||||||
self.required_positional.get(position).cloned()
|
self.required_positional.get(position)
|
||||||
} else if position < (self.required_positional.len() + self.optional_positional.len()) {
|
} else if position < (self.required_positional.len() + self.optional_positional.len()) {
|
||||||
self.optional_positional
|
self.optional_positional
|
||||||
.get(position - self.required_positional.len())
|
.get(position - self.required_positional.len())
|
||||||
.cloned()
|
|
||||||
} else {
|
} else {
|
||||||
self.rest_positional.clone()
|
self.rest_positional.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ fn test_signature_chained() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
signature.get_positional(0),
|
signature.get_positional(0),
|
||||||
Some(PositionalArg {
|
Some(&PositionalArg {
|
||||||
name: "required".to_string(),
|
name: "required".to_string(),
|
||||||
desc: "required description".to_string(),
|
desc: "required description".to_string(),
|
||||||
shape: SyntaxShape::String,
|
shape: SyntaxShape::String,
|
||||||
@ -49,7 +49,7 @@ fn test_signature_chained() {
|
|||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
signature.get_positional(1),
|
signature.get_positional(1),
|
||||||
Some(PositionalArg {
|
Some(&PositionalArg {
|
||||||
name: "optional".to_string(),
|
name: "optional".to_string(),
|
||||||
desc: "optional description".to_string(),
|
desc: "optional description".to_string(),
|
||||||
shape: SyntaxShape::String,
|
shape: SyntaxShape::String,
|
||||||
@ -59,7 +59,7 @@ fn test_signature_chained() {
|
|||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
signature.get_positional(2),
|
signature.get_positional(2),
|
||||||
Some(PositionalArg {
|
Some(&PositionalArg {
|
||||||
name: "rest".to_string(),
|
name: "rest".to_string(),
|
||||||
desc: "rest description".to_string(),
|
desc: "rest description".to_string(),
|
||||||
shape: SyntaxShape::String,
|
shape: SyntaxShape::String,
|
||||||
|
Loading…
Reference in New Issue
Block a user