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/config.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/shirabe/src/config.rs') diff --git a/crates/shirabe/src/config.rs b/crates/shirabe/src/config.rs index d1cd4212..bc172bd2 100644 --- a/crates/shirabe/src/config.rs +++ b/crates/shirabe/src/config.rs @@ -12,8 +12,8 @@ use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ E_USER_DEPRECATED, PHP_URL_HOST, PHP_URL_SCHEME, PhpMixed, RuntimeException, array_key_exists, array_merge, array_search_mixed, array_unique, empty, filter_var_url, implode, in_array, - is_array, is_string, parse_url, php_to_string, rtrim, strtolower, strtoupper, strtr, substr, - trigger_error, + is_array, is_string, parse_url, php_regex, php_to_string, rtrim, strtolower, strtoupper, strtr, + substr, trigger_error, }; use crate::advisory::Auditor; @@ -497,7 +497,7 @@ impl Config { .to_string(); if is_composer && Preg::is_match( - r"{^https?://(?:[a-z0-9-.]+\.)?packagist.org(/|$)}", + php_regex!(r"{^https?://(?:[a-z0-9-.]+\.)?packagist.org(/|$)}"), &repo_url, ) { @@ -676,7 +676,7 @@ impl Config { .to_string(); let mut matches: IndexMap = IndexMap::new(); if !Preg::is_match3( - r"/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i", + php_regex!(r"/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i"), &raw, Some(&mut matches), ) { @@ -1046,7 +1046,7 @@ impl Config { let value_str = value.as_string().unwrap_or("").to_string(); let mut error = None; let result = Preg::replace_callback( - r"#\{\$(.+)\}#", + php_regex!(r"#\{\$(.+)\}#"), |m: &IndexMap| -> String { let key_match = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); match self.get_with_flags(&key_match, flags) { @@ -1069,7 +1069,7 @@ impl Config { /// /// Since the dirs might not exist yet we can not call realpath or it will fail. fn realpath(&self, path: &str) -> String { - if Preg::is_match(r"{^(?:/|[a-z]:|[a-z0-9.]+://|\\\\\\\\)}i", path) { + if Preg::is_match(php_regex!(r"{^(?:/|[a-z]:|[a-z0-9.]+://|\\\\\\\\)}i"), path) { return path.to_string(); } @@ -1115,7 +1115,7 @@ impl Config { repo_options: &IndexMap, ) -> anyhow::Result<()> { // Return right away if the URL is malformed or custom (see issue #5173), but only for non-HTTP(S) URLs - if !filter_var_url(url) && !Preg::is_match(r"{^https?://}", url) { + if !filter_var_url(url) && !Preg::is_match(php_regex!(r"{^https?://}"), url) { return Ok(()); } -- cgit v1.3.1