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-class-map-generator/src/class_map_generator.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-class-map-generator/src/class_map_generator.rs')
| -rw-r--r-- | crates/shirabe-class-map-generator/src/class_map_generator.rs | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/crates/shirabe-class-map-generator/src/class_map_generator.rs b/crates/shirabe-class-map-generator/src/class_map_generator.rs index cb49487b..ba30d1ca 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -9,8 +9,8 @@ use shirabe_external_packages::symfony::finder::Finder; use shirabe_php_shim::{ DIRECTORY_SEPARATOR, InvalidArgumentException, LogicException, PATHINFO_EXTENSION, PHP_INT_MAX, PhpMixed, RuntimeException, explode, getcwd, implode, in_array, is_dir, is_file, is_string, - pathinfo, preg_quote, realpath, str_replace, str_starts_with, stream_get_wrappers, strlen, - strpos, strrpos, strtr, substr, + pathinfo, php_regex, preg_quote, realpath, str_replace, str_starts_with, stream_get_wrappers, + strlen, strpos, strrpos, strtr, substr, }; use std::path::PathBuf; @@ -187,7 +187,7 @@ impl ClassMapGenerator { // optional leading group `(^|[^:])` that is re-emitted in the replacement. Slash runs // are always separated by path-segment characters, so consuming the single preceding // char never prevents an adjacent run from matching. - file_path = Preg::replace(r"{(^|[^:])[\\/]{2,}}", "${1}/", &file_path); + file_path = Preg::replace(php_regex!(r"{(^|[^:])[\\/]{2,}}"), "${1}/", &file_path); } if file_path.is_empty() { @@ -340,12 +340,12 @@ impl ClassMapGenerator { }; let cwd = Self::normalize_path(&cwd); let short_path = Preg::replace( - &format!("{{^{}}}", preg_quote(&cwd, None)), + format!("{{^{}}}", preg_quote(&cwd, None)), ".", &Self::normalize_path(file_path), ); let short_base_path = Preg::replace( - &format!("{{^{}}}", preg_quote(&cwd, None)), + format!("{{^{}}}", preg_quote(&cwd, None)), ".", &Self::normalize_path(base_path), ); @@ -391,7 +391,7 @@ impl ClassMapGenerator { // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: let mut r#match: indexmap::IndexMap<_, _> = indexmap![]; if Preg::is_match3( - r"{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix", + php_regex!(r"{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix"), &path, Some(&mut r#match), ) { @@ -420,7 +420,7 @@ impl ClassMapGenerator { // ensure c: is normalized to C: let prefix = Preg::replace_callback( - r"{(?:^|://)[a-z]:$}i", + php_regex!(r"{(?:^|://)[a-z]:$}i"), |m| { m.get(&CaptureKey::ByIndex(0)) .cloned() |
