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/util/process_executor.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'crates/shirabe/src/util/process_executor.rs') diff --git a/crates/shirabe/src/util/process_executor.rs b/crates/shirabe/src/util/process_executor.rs index 463e5115..e2b1422d 100644 --- a/crates/shirabe/src/util/process_executor.rs +++ b/crates/shirabe/src/util/process_executor.rs @@ -14,7 +14,7 @@ use shirabe_external_packages::symfony::process::exception::ProcessSignaledExcep use shirabe_external_packages::symfony::process::exception::RuntimeException as SymfonyProcessRuntimeException; use shirabe_php_shim::{ LogicException, PHP_EOL, PhpMixed, array_intersect, array_map, call_user_func, escapeshellarg, - explode, implode, in_array, is_array, is_dir, is_numeric, is_string, rtrim, sprintf, + explode, implode, in_array, is_array, is_dir, is_numeric, is_string, php_regex, rtrim, sprintf, str_replace, strcspn, strlen, strpbrk, strtolower, strtr_array, substr_replace, trim, usleep, }; use std::sync::{LazyLock, Mutex}; @@ -236,7 +236,7 @@ impl ProcessExecutor { let mut command_str = command.as_string().unwrap_or("").to_string(); if Platform::is_windows() { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r"{^([^:/\\]++) }", &command_str, Some(&mut m)) { + if Preg::is_match3(php_regex!(r"{^([^:/\\]++) }"), &command_str, Some(&mut m)) { let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); command_str = substr_replace( &command_str, @@ -870,7 +870,7 @@ impl ProcessExecutor { if output.is_empty() { vec![] } else { - Preg::split(r"{\r?\n}", &output) + Preg::split(php_regex!(r"{\r?\n}"), &output) } } @@ -912,7 +912,7 @@ impl ProcessExecutor { String::new() }; let safe_command = Preg::replace_callback( - r"{://(?P[^:/\s]+):(?P[^@\s/]+)@}i", + php_regex!(r"{://(?P[^:/\s]+):(?P[^@\s/]+)@}i"), |m: &IndexMap| -> String { let user_key = CaptureKey::ByName("user".to_string()); // if the username looks like a long (12char+) hex string, or a modern github token (e.g. ghp_xxx, github_pat_xxx) we obfuscate that @@ -934,7 +934,7 @@ impl ProcessExecutor { &command_string, ); let safe_command = Preg::replace( - r"{--password (.*[^\\]') }", + php_regex!(r"{--password (.*[^\\]') }"), "--password '***' ", &safe_command, ); @@ -980,8 +980,14 @@ impl ProcessExecutor { let mut quote = strpbrk(&argument, " \t,").is_some(); let mut dquotes: usize = 0; // PHP: Preg::replace('/(\\\\*)"/', '$1$1\\"', $argument, -1, $dquotes) - argument = Preg::replace5(r#"/(\\*)"/"#, r#"$1$1\""#, &argument, -1, &mut dquotes); - let meta = dquotes > 0 || Preg::is_match(r"/%[^%]+%|![^!]+!/", &argument); + argument = Preg::replace5( + php_regex!(r#"/(\\*)"/"#), + r#"$1$1\""#, + &argument, + -1, + &mut dquotes, + ); + let meta = dquotes > 0 || Preg::is_match(php_regex!(r"/%[^%]+%|![^!]+!/"), &argument); if !meta && !quote { quote = strpbrk(&argument, "^&|<>()").is_some(); @@ -992,8 +998,8 @@ impl ProcessExecutor { } if meta { - argument = Preg::replace(r#"/(["^&|<>()%])/"#, "^$1", &argument); - argument = Preg::replace(r"/(!)/", "^^$1", &argument); + argument = Preg::replace(php_regex!(r#"/(["^&|<>()%])/"#), "^$1", &argument); + argument = Preg::replace(php_regex!(r"/(!)/"), "^^$1", &argument); } argument -- cgit v1.3.1