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/console/application.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/shirabe/src/console/application.rs') diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 55854f11..577d8704 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -99,8 +99,8 @@ use shirabe_php_shim::{ disk_free_space, extension_loaded, file_exists, file_get_contents, file_put_contents, function_exists, getcwd, getmypid, glob, in_array, ini_set, is_array, is_dir, is_file, is_string, is_subclass_of, json_decode, memory_get_peak_usage, memory_get_usage, microtime, - php_uname, posix_getuid, random_bytes, realpath, restore_error_handler, round, str_contains, - str_replace, strpos, strtoupper, sys_get_temp_dir, time, unlink, + php_regex, php_uname, posix_getuid, random_bytes, realpath, restore_error_handler, round, + str_contains, str_replace, strpos, strtoupper, sys_get_temp_dir, time, unlink, }; /// The PHP `Composer\Console\Application` and `Symfony\Component\Console\Application` are @@ -882,7 +882,7 @@ impl Application { .map(|p| shirabe_php_shim::preg_quote(&p, None)) .collect(); let expr = format!("{}{}", shirabe_php_shim::implode("[^:]*:", &parts), "[^:]*"); - let namespaces = shirabe_php_shim::preg_grep(&format!("{{^{}}}", expr), &all_namespaces); + let namespaces = shirabe_php_shim::preg_grep(format!("{{^{}}}", expr), &all_namespaces); if namespaces.is_empty() { let mut message = format!( @@ -983,15 +983,15 @@ impl Application { .map(|p| shirabe_php_shim::preg_quote(&p, None)) .collect(); let expr = format!("{}{}", shirabe_php_shim::implode("[^:]*:", &parts), "[^:]*"); - let mut commands = shirabe_php_shim::preg_grep(&format!("{{^{}}}", expr), &all_commands); + let mut commands = shirabe_php_shim::preg_grep(format!("{{^{}}}", expr), &all_commands); if commands.is_empty() { - commands = shirabe_php_shim::preg_grep(&format!("{{^{}}}i", expr), &all_commands); + commands = shirabe_php_shim::preg_grep(format!("{{^{}}}i", expr), &all_commands); } // if no commands matched or we just matched namespaces if commands.is_empty() - || shirabe_php_shim::preg_grep(&format!("{{^{}$}}i", expr), &commands).is_empty() + || shirabe_php_shim::preg_grep(format!("{{^{}$}}i", expr), &commands).is_empty() { if let Some(pos) = shirabe_php_shim::strrpos(name, ":") { // check if a namespace exists and contains commands @@ -1278,7 +1278,7 @@ impl Application { }; let mut lines: Vec<(String, i64)> = Vec::new(); let split = if !message.is_empty() { - shirabe_php_shim::preg_split(r"/\r?\n/", &message) + shirabe_php_shim::preg_split(php_regex!(r"/\r?\n/"), &message) } else { Vec::new() }; @@ -1723,7 +1723,7 @@ impl Application { let mut m: indexmap::IndexMap> = indexmap::IndexMap::new(); while shirabe_php_shim::preg_match2( - r"/.{1,10000}/u", + php_regex!(r"/.{1,10000}/u"), &utf8_string, &mut m, 0, -- cgit v1.3.1