diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-18 15:03:55 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-18 15:54:27 +0900 |
| commit | 91692846909ed191addb7ec1c34aad11392ab88b (patch) | |
| tree | 7c477055e432fd43a98e5dddc016e07dcfc67f60 /crates/shirabe/src/downloader/git_downloader.rs | |
| parent | 4ae58baf8618f5fe916ba2a69faaca93514134ce (diff) | |
| download | php-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/downloader/git_downloader.rs')
| -rw-r--r-- | crates/shirabe/src/downloader/git_downloader.rs | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/crates/shirabe/src/downloader/git_downloader.rs b/crates/shirabe/src/downloader/git_downloader.rs index cbd429c9..52fd7b1b 100644 --- a/crates/shirabe/src/downloader/git_downloader.rs +++ b/crates/shirabe/src/downloader/git_downloader.rs @@ -19,7 +19,7 @@ use crate::util::Url; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - PhpMixed, RuntimeException, array_map, basename, dirname, implode, in_array, is_dir, + PhpMixed, RuntimeException, array_map, basename, dirname, implode, in_array, is_dir, php_regex, preg_quote, realpath, rtrim, strlen, strpos, substr, trim, version_compare, }; @@ -96,7 +96,11 @@ impl GitDownloader { let mut refs = trim(&output, None); let mut head_match: IndexMap<CaptureKey, String> = IndexMap::new(); - if !Preg::is_match3(r"{^([a-f0-9]+) HEAD$}mi", &refs, Some(&mut head_match)) { + if !Preg::is_match3( + php_regex!(r"{^([a-f0-9]+) HEAD$}mi"), + &refs, + Some(&mut head_match), + ) { // could not match the HEAD for some reason return Ok(None); } @@ -107,7 +111,7 @@ impl GitDownloader { let mut branches_match: IndexMap<CaptureKey, Vec<String>> = IndexMap::new(); if !Preg::is_match_all3( - &format!("{{^{} refs/heads/(.+)$}}mi", preg_quote(&head_ref, None)), + format!("{{^{} refs/heads/(.+)$}}mi", preg_quote(&head_ref, None)), &refs, Some(&mut branches_match), ) { @@ -132,7 +136,7 @@ impl GitDownloader { for candidate in &candidate_branches { let mut m: IndexMap<CaptureKey, Vec<String>> = IndexMap::new(); if Preg::is_match_all3( - &format!( + format!( "{{^[a-f0-9]+ refs/remotes/((?:[^/]+)/{})$}}mi", preg_quote(candidate, None) ), @@ -276,7 +280,11 @@ impl GitDownloader { // If the non-existent branch is actually the name of a file, the file // is checked out. - let mut branch = Preg::replace(r"{(?:^dev-|(?:\.x)?-dev$)}i", "", pretty_version); + let mut branch = Preg::replace( + php_regex!(r"{(?:^dev-|(?:\.x)?-dev$)}i"), + "", + pretty_version, + ); // Closure equivalent: $execute = function(array $command) use (&$output, $path) { ... }; // Inlined below at each call site. @@ -296,10 +304,10 @@ impl GitDownloader { // check whether non-commitish are branches or tags, and fetch branches with the remote name let git_ref = reference.to_string(); - if !Preg::is_match(r"{^[a-f0-9]{40}$}", reference) + if !Preg::is_match(php_regex!(r"{^[a-f0-9]{40}$}"), reference) && branches.is_some() && Preg::is_match( - &format!("{{^\\s+composer/{}$}}m", preg_quote(reference, None)), + format!("{{^\\s+composer/{}$}}m", preg_quote(reference, None)), branches.as_deref().unwrap_or(""), ) { @@ -342,15 +350,15 @@ impl GitDownloader { } // try to checkout branch by name and then reset it so it's on the proper branch name - if Preg::is_match(r"{^[a-f0-9]{40}$}", reference) { + if Preg::is_match(php_regex!(r"{^[a-f0-9]{40}$}"), reference) { // add 'v' in front of the branch if it was stripped when generating the pretty name if branches.is_some() && !Preg::is_match( - &format!("{{^\\s+composer/{}$}}m", preg_quote(&branch, None)), + format!("{{^\\s+composer/{}$}}m", preg_quote(&branch, None)), branches.as_deref().unwrap_or(""), ) && Preg::is_match( - &format!("{{^\\s+composer/v{}$}}m", preg_quote(&branch, None)), + format!("{{^\\s+composer/v{}$}}m", preg_quote(&branch, None)), branches.as_deref().unwrap_or(""), ) { @@ -502,7 +510,7 @@ impl GitDownloader { // set push url for github projects let mut match_: IndexMap<CaptureKey, String> = IndexMap::new(); if Preg::is_match3( - &format!( + format!( "{{^(?:https?|git)://{}/([^/]+)/([^/]+?)(?:\\.git)?$}}", GitUtil::get_github_domains_regex(&self.inner.config.borrow()) ), @@ -661,7 +669,8 @@ impl GitDownloader { } pub(crate) fn get_short_hash(&self, reference: &str) -> String { - if !self.inner.io.is_verbose() && Preg::is_match(r"{^[0-9a-f]{40}$}", reference) { + if !self.inner.io.is_verbose() && Preg::is_match(php_regex!(r"{^[0-9a-f]{40}$}"), reference) + { return substr(reference, 0, Some(10)); } @@ -1127,11 +1136,11 @@ impl VcsDownloader for GitDownloader { let mut origin_match: IndexMap<CaptureKey, String> = IndexMap::new(); let mut composer_match: IndexMap<CaptureKey, String> = IndexMap::new(); if Preg::is_match3( - r"{^origin\s+(?P<url>\S+)}m", + php_regex!(r"{^origin\s+(?P<url>\S+)}m"), &output, Some(&mut origin_match), ) && Preg::is_match3( - r"{^composer\s+(?P<url>\S+)}m", + php_regex!(r"{^composer\s+(?P<url>\S+)}m"), &output, Some(&mut composer_match), ) { @@ -1212,7 +1221,7 @@ impl VcsDownloader for GitDownloader { let changes: Vec<String> = array_map( |elem: &String| format!(" {}", elem), - &Preg::split(r"{\s*\r?\n\s*}", &changes), + &Preg::split(php_regex!(r"{\s*\r?\n\s*}"), &changes), ); self.inner.io.write_error3( &format!( |
