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/remote_filesystem.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'crates/shirabe/src/util/remote_filesystem.rs') diff --git a/crates/shirabe/src/util/remote_filesystem.rs b/crates/shirabe/src/util/remote_filesystem.rs index df47be20..9eaf8229 100644 --- a/crates/shirabe/src/util/remote_filesystem.rs +++ b/crates/shirabe/src/util/remote_filesystem.rs @@ -20,7 +20,7 @@ use shirabe_php_shim::{ array_replace_recursive, base64_encode, explode, extension_loaded, file_get_contents, file_get_contents5, file_put_contents, filter_var_boolean, gethostbyname, http_clear_last_response_headers, http_get_last_response_headers, ini_get, json_decode, - parse_url, preg_quote, strpos, strtolower, strtr, substr, trim, zlib_decode, + parse_url, php_regex, preg_quote, strpos, strtolower, strtr, substr, trim, zlib_decode, }; /// Result of `RemoteFilesystem::get` — string content, `true` (for copy), or `false`. @@ -149,7 +149,7 @@ impl RemoteFilesystem { let mut value: Option = None; for header in headers { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3("{^HTTP/\\S+ (\\d+)}i", header, Some(&mut m)) { + if Preg::is_match3(php_regex!("{^HTTP/\\S+ (\\d+)}i"), header, Some(&mut m)) { value = m .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) @@ -163,7 +163,7 @@ impl RemoteFilesystem { pub fn find_status_message(&self, headers: &[String]) -> Option { let mut value: Option = None; for header in headers { - if Preg::is_match("{^HTTP/\\S+ \\d+}i", header) { + if Preg::is_match(php_regex!("{^HTTP/\\S+ \\d+}i"), header) { value = Some(header.clone()); } } @@ -287,8 +287,10 @@ impl RemoteFilesystem { crate::io::DEBUG, ); - if (!Preg::is_match("{^http://(repo\\.)?packagist\\.org/p/}", &file_url) - || (strpos(&file_url, "$").is_none() && strpos(&file_url, "%24").is_none())) + if (!Preg::is_match( + php_regex!("{^http://(repo\\.)?packagist\\.org/p/}"), + &file_url, + ) || (strpos(&file_url, "$").is_none() && strpos(&file_url, "%24").is_none())) && !degraded_packagist { let _ = self.config.borrow_mut().prohibit_url_by_config( @@ -472,7 +474,10 @@ impl RemoteFilesystem { None, ) != ".zip") && content_type.is_some() - && Preg::is_match("{^text/html\\b}i", content_type.as_deref().unwrap_or("")); + && Preg::is_match( + php_regex!("{^text/html\\b}i"), + content_type.as_deref().unwrap_or(""), + ); if bitbucket_login_match { result = None; if retry_auth_failure { @@ -940,7 +945,7 @@ impl RemoteFilesystem { .to_string(); target_url = Some(Preg::replace( - &format!( + format!( "{{^(.+(?://|@){}(?::\\d+)?)(?:[/\\?].*)?$}}", preg_quote(&url_host, None) ), @@ -949,7 +954,7 @@ impl RemoteFilesystem { )); } else { target_url = Some(Preg::replace( - "{^(.+/)[^/?]*(?:\\?.*)?$}", + php_regex!("{^(.+/)[^/?]*(?:\\?.*)?$}"), &format!("\\1{}", location_header), &self.file_url, )); -- cgit v1.3.1