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/package/loader/root_package_loader.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/package/loader/root_package_loader.rs')
| -rw-r--r-- | crates/shirabe/src/package/loader/root_package_loader.rs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/crates/shirabe/src/package/loader/root_package_loader.rs b/crates/shirabe/src/package/loader/root_package_loader.rs index 83cda30b..b3ed8c41 100644 --- a/crates/shirabe/src/package/loader/root_package_loader.rs +++ b/crates/shirabe/src/package/loader/root_package_loader.rs @@ -16,7 +16,9 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; -use shirabe_php_shim::{PhpMixed, RuntimeException, UnexpectedValueException, strtolower}; +use shirabe_php_shim::{ + PhpMixed, RuntimeException, UnexpectedValueException, php_regex, strtolower, +}; #[derive(Debug)] pub struct RootPackageLoader { @@ -261,7 +263,7 @@ impl RootPackageLoader { for (req_name, req_version) in requires { let mut m: IndexMap<CaptureKey, String> = IndexMap::new(); if Preg::is_match3( - r"{(?:^|\| *|, *)([^,\s#|]+)(?:#[^ ]+)? +as +([^,\s|]+)(?:$| *\|| *,)}", + php_regex!(r"{(?:^|\| *|, *)([^,\s#|]+)(?:#[^ ]+)? +as +([^,\s|]+)(?:$| *\|| *,)}"), req_version, Some(&mut m), ) { @@ -315,7 +317,7 @@ impl RootPackageLoader { for (req_name, req_version) in requires { let mut constraints: Vec<String> = vec![]; - let or_split = Preg::split(r"{\s*\|\|?\s*}", req_version.trim()); + let or_split = Preg::split(php_regex!(r"{\s*\|\|?\s*}"), req_version.trim()); for or_constraint in &or_split { let and_split = shirabe_semver::split_and_constraints(or_constraint); for and_constraint in and_split { @@ -348,8 +350,9 @@ impl RootPackageLoader { } for constraint in &constraints { - let req_version_stripped = Preg::replace(r"{^([^,\s@]+) as .+$}", "$1", constraint); - if Preg::is_match(r"{^[^,\s@]+$}", &req_version_stripped) { + let req_version_stripped = + Preg::replace(php_regex!(r"{^([^,\s@]+) as .+$}"), "$1", constraint); + if Preg::is_match(php_regex!(r"{^[^,\s@]+$}"), &req_version_stripped) { let stability_name = VersionParser::parse_stability(&req_version_stripped); if stability_name != "stable" { let name = strtolower(req_name); @@ -373,10 +376,13 @@ impl RootPackageLoader { mut references: IndexMap<String, String>, ) -> IndexMap<String, String> { for (req_name, req_version) in requires { - let req_version = Preg::replace(r"{^([^,\s@]+) as .+$}", "$1", req_version); + let req_version = Preg::replace(php_regex!(r"{^([^,\s@]+) as .+$}"), "$1", req_version); let mut m: IndexMap<CaptureKey, String> = IndexMap::new(); - if Preg::is_match3(r"{^[^,\s@]+?#([a-f0-9]+)$}", &req_version, Some(&mut m)) - && VersionParser::parse_stability(&req_version) == "dev" + if Preg::is_match3( + php_regex!(r"{^[^,\s@]+?#([a-f0-9]+)$}"), + &req_version, + Some(&mut m), + ) && VersionParser::parse_stability(&req_version) == "dev" { let name = strtolower(req_name); references.insert( |
