From 06b5c6c06f22cf87706def3389b6f60f22f26664 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 15 Jul 2023 21:25:12 -0400 Subject: [PATCH] Use `SmallVec#extend_from_slice` in lieu of `SmallVec#extend` (#5793) ## Summary There's a note in the docs that suggests this can be faster, and in the benchmarks it... seems like it is? Might just be noise but held up over a few runs. Before: Screen Shot 2023-07-15 at 9 10 06 PM After: Screen Shot 2023-07-15 at 9 10 09 PM --- crates/ruff_python_semantic/src/model.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 38bd3c4783..53138dbf5d 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -5,7 +5,7 @@ use bitflags::bitflags; use nohash_hasher::{BuildNoHashHasher, IntMap}; use ruff_text_size::TextRange; use rustpython_parser::ast::{Expr, Ranged, Stmt}; -use smallvec::smallvec; +use smallvec::SmallVec; use ruff_python_ast::call_path::{collect_call_path, from_unqualified_name, CallPath}; use ruff_python_ast::helpers::from_relative_import; @@ -526,7 +526,7 @@ impl<'a> SemanticModel<'a> { /// ...then `resolve_call_path(${python_version})` will resolve to `sys.version_info`. pub fn resolve_call_path(&'a self, value: &'a Expr) -> Option> { let call_path = collect_call_path(value)?; - let head = call_path.first()?; + let (head, tail) = call_path.split_first()?; let binding = self.find_binding(head)?; match &binding.kind { BindingKind::Import(Import { @@ -537,12 +537,12 @@ impl<'a> SemanticModel<'a> { if source_path.is_empty() { None } else { - source_path.extend(call_path.into_iter().skip(1)); + source_path.extend_from_slice(tail); Some(source_path) } } else { let mut source_path: CallPath = from_unqualified_name(name); - source_path.extend(call_path.into_iter().skip(1)); + source_path.extend_from_slice(tail); Some(source_path) } } @@ -551,7 +551,7 @@ impl<'a> SemanticModel<'a> { }) => { let name = name.split('.').next().unwrap_or(name); let mut source_path: CallPath = from_unqualified_name(name); - source_path.extend(call_path.into_iter().skip(1)); + source_path.extend_from_slice(tail); Some(source_path) } BindingKind::FromImport(FromImport { @@ -562,19 +562,19 @@ impl<'a> SemanticModel<'a> { if source_path.is_empty() { None } else { - source_path.extend(call_path.into_iter().skip(1)); + source_path.extend_from_slice(tail); Some(source_path) } } else { let mut source_path: CallPath = from_unqualified_name(name); - source_path.extend(call_path.into_iter().skip(1)); + source_path.extend_from_slice(tail); Some(source_path) } } BindingKind::Builtin => { - let mut source_path: CallPath = smallvec![]; + let mut source_path: CallPath = SmallVec::with_capacity(1 + call_path.len()); source_path.push(""); - source_path.extend(call_path); + source_path.extend_from_slice(call_path.as_slice()); Some(source_path) } _ => None,