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:
Devyn Cairns 2024-07-10 19:14:05 -07:00 committed by GitHub
parent f87cf895c2
commit 801cfae279
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 7 deletions

View File

@ -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()
} }
} }

View File

@ -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,