Reduce allocations on project type detection (#32818)

Joseph T. Lyons created

Release Notes:

- N/A

Change summary

crates/client/src/telemetry.rs | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)

Detailed changes

crates/client/src/telemetry.rs 🔗

@@ -374,22 +374,28 @@ impl Telemetry {
             return None;
         }
 
-        let mut project_types: HashSet<String> = HashSet::new();
+        let mut project_types: HashSet<&str> = HashSet::new();
 
         for (path, _, _) in updated_entries_set.iter() {
             let Some(file_name) = path.file_name().and_then(|f| f.to_str()) else {
                 continue;
             };
 
-            if file_name == "pnpm-lock.yaml" {
-                project_types.insert("pnpm".to_string());
+            let project_type = if file_name == "pnpm-lock.yaml" {
+                Some("pnpm")
             } else if file_name == "yarn.lock" {
-                project_types.insert("yarn".to_string());
+                Some("yarn")
             } else if file_name == "package.json" {
-                project_types.insert("node".to_string());
+                Some("node")
             } else if DOTNET_PROJECT_FILES_REGEX.is_match(file_name) {
-                project_types.insert("dotnet".to_string());
-            }
+                Some("dotnet")
+            } else {
+                None
+            };
+
+            if let Some(project_type) = project_type {
+                project_types.insert(project_type);
+            };
         }
 
         if !project_types.is_empty() {
@@ -398,7 +404,8 @@ impl Telemetry {
                 .insert(worktree_id);
         }
 
-        let mut project_names_vec: Vec<String> = project_types.into_iter().collect();
+        let mut project_names_vec: Vec<String> =
+            project_types.into_iter().map(String::from).collect();
         project_names_vec.sort();
         Some(project_names_vec)
     }