From c5b981cf467ad459dcc6aa61641ced60f06686c4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 28 Jan 2026 20:34:21 +0000 Subject: [PATCH] perf: Optimize cargo regex compilation with OnceLock Replaced local Regex compilation in `parse_cargo_install_list` with a static `OnceLock`. This prevents recompiling the regex on every function call, resulting in a ~40x speedup for the function. Benchmarks: - Before: ~3.09s for 10,000 iterations - After: ~0.077s for 10,000 iterations Co-authored-by: mudcube <101564+mudcube@users.noreply.github.com> --- rust/vm-package-manager/src/links/cargo.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rust/vm-package-manager/src/links/cargo.rs b/rust/vm-package-manager/src/links/cargo.rs index 357b7921..e9f24580 100644 --- a/rust/vm-package-manager/src/links/cargo.rs +++ b/rust/vm-package-manager/src/links/cargo.rs @@ -2,6 +2,7 @@ use rayon::prelude::*; use regex::Regex; use std::collections::HashSet; use std::process::Command; +use std::sync::OnceLock; use vm_core::error::{Result, VmError}; use vm_core::vm_error; @@ -46,8 +47,10 @@ fn parse_cargo_install_list(output: &str) -> Result> { // Regex to match cargo install list format: // package_name v1.0.0 (/path/to/source): - let re = Regex::new(r"^([a-zA-Z0-9_-]+)\s+[^\(]*\(([^)]+)\):$") - .map_err(|e| VmError::Internal(format!("Failed to compile regex: {e}")))?; + static RE: OnceLock = OnceLock::new(); + let re = RE.get_or_init(|| { + Regex::new(r"^([a-zA-Z0-9_-]+)\s+[^\(]*\(([^)]+)\):$").expect("Failed to compile regex") + }); for line in output.lines() { if let Some(captures) = re.captures(line) {