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/json/json_file.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'crates/shirabe/src/json/json_file.rs') diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index 075e1f64..0ce55e55 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -14,8 +14,8 @@ use shirabe_external_packages::seld::json_lint::{ParsingException, ParsingExcept use shirabe_php_shim::{ InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents, - file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, realpath, str_contains, - str_ends_with, str_repeat, strlen, strpos, usleep, + file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, php_regex, realpath, + str_contains, str_ends_with, str_repeat, strlen, strpos, usleep, }; #[derive(Debug, Clone)] @@ -113,7 +113,7 @@ impl JsonFile { http_downloader: Option>>, io: Option>>, ) -> anyhow::Result { - if http_downloader.is_none() && Preg::is_match(r"{^https?://}i", &path) { + if http_downloader.is_none() && Preg::is_match(php_regex!(r"{^https?://}i"), &path) { return Err(InvalidArgumentException { message: "http urls require a HttpDownloader instance to be passed".to_string(), code: 0, @@ -466,7 +466,7 @@ impl JsonFile { // Pretty printing and not using default indentation let indent_owned = options.indent.clone(); return Preg::replace_callback( - r"#^ {4,}#m", + php_regex!(r"#^ {4,}#m"), move |m: &indexmap::IndexMap< shirabe_external_packages::composer::pcre::CaptureKey, String, @@ -510,7 +510,9 @@ impl JsonFile { { let mut count: usize = 0; let replaced = Preg::replace5( - r#"{\r?\n<<<<<<< [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n(?:\|{7} [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n)?=======\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n>>>>>>> [^\r\n]+(\r?\n)}"#, + php_regex!( + r#"{\r?\n<<<<<<< [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n(?:\|{7} [^\r\n]+\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n)?=======\r?\n\s+"content-hash": *"[0-9a-f]+", *\r?\n>>>>>>> [^\r\n]+(\r?\n)}"# + ), " \"content-hash\": \"VCS merge conflict detected. Please run `composer update --lock`.\",$1", json, -1, @@ -574,7 +576,11 @@ impl JsonFile { pub fn detect_indenting(json: Option<&str>) -> String { let mut m: IndexMap = IndexMap::new(); - if Preg::is_match3(r##"#^([ \t]+)"#m"##, json.unwrap_or(""), Some(&mut m)) { + if Preg::is_match3( + php_regex!(r##"#^([ \t]+)"#m"##), + json.unwrap_or(""), + Some(&mut m), + ) { return m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); } -- cgit v1.3.1