From dfb32cdf10522f1979bab5a84d1316400e93d80a Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Fri, 21 Jul 2023 17:10:42 +0900 Subject: [PATCH] Optimize `DepKindInfo` -> `DepKind` conversion We don't need to allocate a `Vec`, nor do we need sorting. --- crates/project-model/src/cargo_workspace.rs | 23 +++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/project-model/src/cargo_workspace.rs b/crates/project-model/src/cargo_workspace.rs index e1117ac464b..e47808a2cc9 100644 --- a/crates/project-model/src/cargo_workspace.rs +++ b/crates/project-model/src/cargo_workspace.rs @@ -145,7 +145,7 @@ pub struct PackageDependency { pub kind: DepKind, } -#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum DepKind { /// Available to the library, binary, and dev targets in the package (but not the build script). Normal, @@ -156,23 +156,20 @@ pub enum DepKind { } impl DepKind { - fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator + '_ { - let mut dep_kinds = Vec::new(); + fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator { + let mut dep_kinds = [None; 3]; if list.is_empty() { - dep_kinds.push(Self::Normal); + dep_kinds[0] = Some(Self::Normal); } for info in list { - let kind = match info.kind { - cargo_metadata::DependencyKind::Normal => Self::Normal, - cargo_metadata::DependencyKind::Development => Self::Dev, - cargo_metadata::DependencyKind::Build => Self::Build, + match info.kind { + cargo_metadata::DependencyKind::Normal => dep_kinds[0] = Some(Self::Normal), + cargo_metadata::DependencyKind::Development => dep_kinds[1] = Some(Self::Dev), + cargo_metadata::DependencyKind::Build => dep_kinds[2] = Some(Self::Build), cargo_metadata::DependencyKind::Unknown => continue, - }; - dep_kinds.push(kind); + } } - dep_kinds.sort_unstable(); - dep_kinds.dedup(); - dep_kinds.into_iter() + dep_kinds.into_iter().flatten() } }