From 91692846909ed191addb7ec1c34aad11392ab88b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 18 Jul 2026 15:03:55 +0900 Subject: perf(regex): eliminate per-call clone overhead in preg_* dispatch regex::Regex::clone() does not share the underlying meta engine's search-cache pool, so every fresh clone pays a ~10us warmup cost on its first use. Two changes together eliminate this across nearly all preg_* call sites: - A php_regex! macro resolves PHP-style patterns to a per-call-site &'static regex::Regex (via regex-macro's LazyLock), applied at the majority of call sites throughout the codebase. - Call sites still passing dynamic pattern strings go through PATTERN_CACHE, which now stores Arc<(Regex, bool)> and hands out Arc::clone()s instead of cloning the Regex itself. PregPattern::resolve() returns a ResolvedPattern enum (Arc or 'static reference) rather than an owned Regex, so neither path ever clones the Regex proper. Co-Authored-By: Claude Sonnet 5 --- crates/shirabe/src/repository/vcs/svn_driver.rs | 34 ++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'crates/shirabe/src/repository/vcs/svn_driver.rs') diff --git a/crates/shirabe/src/repository/vcs/svn_driver.rs b/crates/shirabe/src/repository/vcs/svn_driver.rs index 50852aef..5f1faa65 100644 --- a/crates/shirabe/src/repository/vcs/svn_driver.rs +++ b/crates/shirabe/src/repository/vcs/svn_driver.rs @@ -15,7 +15,7 @@ use chrono::{DateTime, FixedOffset, Utc}; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - PhpMixed, RuntimeException, array_key_exists, stripos, strrpos, strtr, substr, trim, + PhpMixed, RuntimeException, array_key_exists, php_regex, stripos, strrpos, strtr, substr, trim, }; #[derive(Debug)] @@ -157,7 +157,7 @@ impl SvnDriver { } pub(crate) fn should_cache(&self, identifier: &str) -> bool { - self.inner.cache.is_some() && Preg::is_match(r"{@\d+$}", identifier) + self.inner.cache.is_some() && Preg::is_match(php_regex!(r"{@\d+$}"), identifier) } pub fn get_composer_information( @@ -262,7 +262,7 @@ impl SvnDriver { let identifier = format!("/{}/", trim(identifier, Some("/"))); let (path, rev) = if let Some(m) = - Preg::is_match_with_indexed_captures(r"{^(.+?)(@\d+)?/$}", &identifier) + Preg::is_match_with_indexed_captures(php_regex!(r"{^(.+?)(@\d+)?/$}"), &identifier) { if m.get(2).is_some() { ( @@ -302,7 +302,7 @@ impl SvnDriver { let identifier = format!("/{}/", trim(identifier, Some("/"))); let (path, rev) = if let Some(m) = - Preg::is_match_with_indexed_captures(r"{^(.+?)(@\d+)?/$}", &identifier) + Preg::is_match_with_indexed_captures(php_regex!(r"{^(.+?)(@\d+)?/$}"), &identifier) { if m.get(2).is_some() { ( @@ -323,7 +323,11 @@ impl SvnDriver { for line in self.inner.process.borrow().split_lines(&output) { if !line.is_empty() { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"{^Last Changed Date: ([^(]+)}", &line, Some(&mut m)) { + if Preg::is_match3( + php_regex!(r"{^Last Changed Date: ([^(]+)}"), + &line, + Some(&mut m), + ) { let date_str = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); return Ok(shirabe_php_shim::date_create::(date_str.trim()) .ok() @@ -351,7 +355,11 @@ impl SvnDriver { let line = trim(&line, None); if !line.is_empty() { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"{^\s*(\S+).*?(\S+)\s*$}", &line, Some(&mut m)) { + if Preg::is_match3( + php_regex!(r"{^\s*(\S+).*?(\S+)\s*$}"), + &line, + Some(&mut m), + ) { let rev: i64 = m .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) @@ -398,7 +406,11 @@ impl SvnDriver { let line = trim(&line, None); if !line.is_empty() { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"{^\s*(\S+).*?(\S+)\s*$}", &line, Some(&mut m)) { + if Preg::is_match3( + php_regex!(r"{^\s*(\S+).*?(\S+)\s*$}"), + &line, + Some(&mut m), + ) { let rev: i64 = m .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) @@ -436,7 +448,11 @@ impl SvnDriver { let line = trim(&line, None); if !line.is_empty() { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"{^\s*(\S+).*?(\S+)\s*$}", &line, Some(&mut m)) { + if Preg::is_match3( + php_regex!(r"{^\s*(\S+).*?(\S+)\s*$}"), + &line, + Some(&mut m), + ) { let rev: i64 = m .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) @@ -472,7 +488,7 @@ impl SvnDriver { deep: bool, ) -> anyhow::Result { let url = Self::normalize_url(url); - if Preg::is_match(r"#(^svn://|^svn\+ssh://|svn\.)#i", &url) { + if Preg::is_match(php_regex!(r"#(^svn://|^svn\+ssh://|svn\.)#i"), &url) { return Ok(true); } -- cgit v1.3.1