aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/remote_filesystem.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-07-18 15:03:55 +0900
committernsfisis <nsfisis@gmail.com>2026-07-18 15:54:27 +0900
commit91692846909ed191addb7ec1c34aad11392ab88b (patch)
tree7c477055e432fd43a98e5dddc016e07dcfc67f60 /crates/shirabe/src/util/remote_filesystem.rs
parent4ae58baf8618f5fe916ba2a69faaca93514134ce (diff)
downloadphp-shirabe-91692846909ed191addb7ec1c34aad11392ab88b.tar.gz
php-shirabe-91692846909ed191addb7ec1c34aad11392ab88b.tar.zst
php-shirabe-91692846909ed191addb7ec1c34aad11392ab88b.zip
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 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/util/remote_filesystem.rs')
-rw-r--r--crates/shirabe/src/util/remote_filesystem.rs21
1 files changed, 13 insertions, 8 deletions
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<i64> = None;
for header in headers {
let mut m: IndexMap<CaptureKey, String> = 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<String> {
let mut value: Option<String> = 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,
));