From fcc27bfe4401aefea8bd74bada6be77a220800d8 Mon Sep 17 00:00:00 2001 From: guy oron Date: Sun, 28 Jun 2026 15:27:45 +0300 Subject: [PATCH] perf(deps): move regex compilation to lazy_static Three Regex::new() calls in deps.rs used static patterns but were compiled inside function bodies, causing recompilation on every invocation of summarize_cargo_str and summarize_requirements_str. Move all three to a lazy_static! block at module scope so they compile once at first use. Fixes #2688 --- src/cmds/system/deps.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/cmds/system/deps.rs b/src/cmds/system/deps.rs index 055d8cb76f..76ceb60840 100644 --- a/src/cmds/system/deps.rs +++ b/src/cmds/system/deps.rs @@ -4,10 +4,19 @@ use crate::core::guard::never_worse; use crate::core::tracking; use crate::core::truncate::{reduced, CAP_WARNINGS}; use anyhow::Result; +use lazy_static::lazy_static; use regex::Regex; use std::fs; use std::path::Path; +lazy_static! { + static ref CARGO_DEP_RE: Regex = + Regex::new(r#"^([a-zA-Z0-9_-]+)\s*=\s*(?:"([^"]+)"|.*version\s*=\s*"([^"]+)")"#).unwrap(); + static ref CARGO_SECTION_RE: Regex = Regex::new(r"^\[([^\]]+)\]").unwrap(); + static ref REQUIREMENTS_DEP_RE: Regex = + Regex::new(r"^([a-zA-Z0-9_-]+)([=<>!~]+.*)?$").unwrap(); +} + const MAX_DEPS: usize = CAP_WARNINGS; // dev deps are secondary to prod — show fewer. const MAX_DEV_DEPS: usize = reduced(CAP_WARNINGS, 5); @@ -82,21 +91,18 @@ pub fn run(path: &Path, verbose: u8) -> Result<()> { fn summarize_cargo_str(path: &Path) -> Result { let content = fs::read_to_string(path)?; - let dep_re = - Regex::new(r#"^([a-zA-Z0-9_-]+)\s*=\s*(?:"([^"]+)"|.*version\s*=\s*"([^"]+)")"#).unwrap(); - let section_re = Regex::new(r"^\[([^\]]+)\]").unwrap(); let mut current_section = String::new(); let mut deps = Vec::new(); let mut dev_deps = Vec::new(); let mut out = String::new(); for line in content.lines() { - if let Some(caps) = section_re.captures(line) { + if let Some(caps) = CARGO_SECTION_RE.captures(line) { current_section = caps .get(1) .map(|m| m.as_str().to_string()) .unwrap_or_default(); - } else if let Some(caps) = dep_re.captures(line) { + } else if let Some(caps) = CARGO_DEP_RE.captures(line) { let name = caps.get(1).map(|m| m.as_str()).unwrap_or(""); let version = caps .get(2) @@ -171,7 +177,6 @@ fn summarize_package_json_str(path: &Path) -> Result { fn summarize_requirements_str(path: &Path) -> Result { let content = fs::read_to_string(path)?; - let dep_re = Regex::new(r"^([a-zA-Z0-9_-]+)([=<>!~]+.*)?$").unwrap(); let mut deps = Vec::new(); let mut out = String::new(); @@ -180,7 +185,7 @@ fn summarize_requirements_str(path: &Path) -> Result { if line.is_empty() || line.starts_with('#') { continue; } - if let Some(caps) = dep_re.captures(line) { + if let Some(caps) = REQUIREMENTS_DEP_RE.captures(line) { let name = caps.get(1).map(|m| m.as_str()).unwrap_or(""); let version = caps.get(2).map(|m| m.as_str()).unwrap_or(""); deps.push(format!("{}{}", name, version));