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/tests | |
| 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/tests')
6 files changed, 25 insertions, 17 deletions
diff --git a/crates/shirabe/tests/all_functional_test.rs b/crates/shirabe/tests/all_functional_test.rs index b1717d08..e540ca15 100644 --- a/crates/shirabe/tests/all_functional_test.rs +++ b/crates/shirabe/tests/all_functional_test.rs @@ -9,7 +9,7 @@ use indexmap::IndexMap; use serial_test::serial; use shirabe::util::filesystem::Filesystem; use shirabe_external_packages::composer::pcre::preg::Preg; -use shirabe_php_shim::{CaptureKey, PREG_SPLIT_DELIM_CAPTURE, PhpMixed, intval}; +use shirabe_php_shim::{CaptureKey, PREG_SPLIT_DELIM_CAPTURE, PhpMixed, intval, php_regex}; use std::path::{Path, PathBuf}; /// ref: AllFunctionalTest's `$oldcwd` / `$testDir` instance state plus its `setUp`/`tearDown`. @@ -67,7 +67,7 @@ fn unique_tmp_directory() -> PathBuf { fn parse_test_file(file: &Path) -> IndexMap<String, String> { let contents = std::fs::read_to_string(file).unwrap(); let tokens = Preg::split4( - r"#(?:^|\n*)--([A-Z-]+)--\n#", + php_regex!(r"#(?:^|\n*)--([A-Z-]+)--\n#"), &contents, -1, PREG_SPLIT_DELIM_CAPTURE, @@ -147,7 +147,7 @@ fn expect_matches(expected: &str, output: &str) { } if eb[i] == b'%' { let mut m: IndexMap<CaptureKey, String> = IndexMap::new(); - if !Preg::is_match3("{%(.+?)%}", &expected[i..], Some(&mut m)) { + if !Preg::is_match3(php_regex!("{%(.+?)%}"), &expected[i..], Some(&mut m)) { panic!("Failed to match %...% in {}", &expected[i..]); } let regex = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap(); diff --git a/crates/shirabe/tests/common/io_mock.rs b/crates/shirabe/tests/common/io_mock.rs index e54c45b9..47f4b66b 100644 --- a/crates/shirabe/tests/common/io_mock.rs +++ b/crates/shirabe/tests/common/io_mock.rs @@ -7,7 +7,7 @@ use shirabe::io::{IOInterface, IOInterfaceImmutable, IOInterfaceMutable}; use shirabe::util::platform::Platform; use shirabe_external_packages::composer::pcre::Preg; use shirabe_external_packages::symfony::console::output::output_interface; -use shirabe_php_shim::{PHP_EOL, PhpMixed, preg_quote}; +use shirabe_php_shim::{PHP_EOL, PhpMixed, php_regex, preg_quote}; use std::collections::VecDeque; // A single entry of the IO expectation list. PHP models these as associative @@ -126,7 +126,7 @@ impl IOMock { }; if !expectations.is_empty() { - let mut lines: VecDeque<String> = Preg::split("{\r?\n}", &output).into(); + let mut lines: VecDeque<String> = Preg::split(php_regex!("{\r?\n}"), &output).into(); let mut auth_log: VecDeque<(String, String, Option<String>)> = self.auth_log.clone().into(); diff --git a/crates/shirabe/tests/dependency_resolver/pool_builder_test.rs b/crates/shirabe/tests/dependency_resolver/pool_builder_test.rs index e6897ff7..659f5e48 100644 --- a/crates/shirabe/tests/dependency_resolver/pool_builder_test.rs +++ b/crates/shirabe/tests/dependency_resolver/pool_builder_test.rs @@ -20,8 +20,8 @@ use shirabe::repository::lock_array_repository::LockArrayRepository; use shirabe::repository::repository_factory::RepositoryFactory; use shirabe::repository::repository_set::{RepositorySet, RootAliasInput}; use shirabe_external_packages::composer::pcre::preg::Preg; -use shirabe_php_shim::PREG_SPLIT_DELIM_CAPTURE; use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PREG_SPLIT_DELIM_CAPTURE, php_regex}; use std::path::PathBuf; /// Maps the PHP `$loadPackage` closure: pops the optional `id` from the data, loads the @@ -58,7 +58,7 @@ fn load_package( fn read_test_file(file: &str, fixtures_dir: &str) -> IndexMap<String, String> { let contents = shirabe_php_shim::file_get_contents(file).unwrap(); let tokens = Preg::split4( - r"#(?:^|\n*)--([A-Z-]+)--\n#", + php_regex!(r"#(?:^|\n*)--([A-Z-]+)--\n#"), &contents, -1, PREG_SPLIT_DELIM_CAPTURE, @@ -144,7 +144,7 @@ fn get_integration_tests(fixtures_dir: &std::path::Path) -> IndexMap<String, Int for file in files { let file = file.to_str().unwrap().to_string(); - if !Preg::is_match(r"/\.test$/", &file) { + if !Preg::is_match(php_regex!(r"/\.test$/"), &file) { continue; } diff --git a/crates/shirabe/tests/dependency_resolver/pool_optimizer_test.rs b/crates/shirabe/tests/dependency_resolver/pool_optimizer_test.rs index 08d5f989..d5cdbc78 100644 --- a/crates/shirabe/tests/dependency_resolver/pool_optimizer_test.rs +++ b/crates/shirabe/tests/dependency_resolver/pool_optimizer_test.rs @@ -12,8 +12,8 @@ use shirabe::package::version::version_parser::VersionParser; use shirabe::repository::handle::LockArrayRepositoryHandle; use shirabe::repository::lock_array_repository::LockArrayRepository; use shirabe_external_packages::composer::pcre::preg::Preg; -use shirabe_php_shim::PREG_SPLIT_DELIM_CAPTURE; use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PREG_SPLIT_DELIM_CAPTURE, php_regex}; use std::path::PathBuf; fn load_package(package_data: &PhpMixed) -> BasePackageHandle { @@ -62,7 +62,7 @@ fn reduce_packages_info_for_comparison(packages: &[BasePackageHandle]) -> Vec<St fn read_test_file(file: &str, fixtures_dir: &str) -> IndexMap<String, String> { let contents = shirabe_php_shim::file_get_contents(file).unwrap(); let tokens = Preg::split4( - r"#(?:^|\n*)--([A-Z-]+)--\n#", + php_regex!(r"#(?:^|\n*)--([A-Z-]+)--\n#"), &contents, -1, PREG_SPLIT_DELIM_CAPTURE, @@ -147,7 +147,7 @@ fn provide_integration_tests() -> IndexMap< for file in files { let file = file.to_str().unwrap().to_string(); - if !Preg::is_match(r"/\.test$/", &file) { + if !Preg::is_match(php_regex!(r"/\.test$/"), &file) { continue; } diff --git a/crates/shirabe/tests/installer_test.rs b/crates/shirabe/tests/installer_test.rs index e5006b86..ba3c0e86 100644 --- a/crates/shirabe/tests/installer_test.rs +++ b/crates/shirabe/tests/installer_test.rs @@ -50,7 +50,7 @@ use shirabe_external_packages::symfony::console::output::output_interface::{ OutputInterface, VERBOSITY_NORMAL, }; use shirabe_external_packages::symfony::console::output::stream_output::StreamOutput; -use shirabe_php_shim::{PREG_SPLIT_DELIM_CAPTURE, PhpMixed}; +use shirabe_php_shim::{PREG_SPLIT_DELIM_CAPTURE, PhpMixed, php_regex}; use shirabe_semver::VersionParser; use shirabe_semver::constraint::AnyConstraint; @@ -550,7 +550,7 @@ fn read_test_file( ) -> IndexMap<String, String> { let contents = std::fs::read_to_string(file).unwrap(); let tokens = Preg::split4( - r"#(?:^|\n*)--([A-Z-]+)--\n#", + php_regex!(r"#(?:^|\n*)--([A-Z-]+)--\n#"), &contents, -1, PREG_SPLIT_DELIM_CAPTURE, @@ -654,7 +654,7 @@ fn load_integration_tests(path: &str) -> Vec<IntegrationCase> { return; } if let Some(url) = repo.get("url").and_then(|u| u.as_str()) - && Preg::is_match(r"{^file://[^/]}", url) + && Preg::is_match(php_regex!(r"{^file://[^/]}"), url) { let new_url = format!("file://{}/{}", fixtures_str, &url[7..]); repo["url"] = serde_json::Value::String(new_url); @@ -1204,8 +1204,16 @@ fn do_test_integration(case: &IntegrationCase, expect_output: Option<&str>) { if let Some(expect_output) = expect_output && !expect_output.is_empty() { - let output = Preg::replace(r"{^ - .*?\.ini$}m", "__inilist__", &output_string); - let output = Preg::replace(r"{(__inilist__\r?\n)+}", "__inilist__\n", &output); + let output = Preg::replace( + php_regex!(r"{^ - .*?\.ini$}m"), + "__inilist__", + &output_string, + ); + let output = Preg::replace( + php_regex!(r"{(__inilist__\r?\n)+}"), + "__inilist__\n", + &output, + ); assert_string_matches_format(expect_output.trim_end(), output.trim_end(), &output_string); } } diff --git a/crates/shirabe/tests/package/archiver/archivable_files_finder_test.rs b/crates/shirabe/tests/package/archiver/archivable_files_finder_test.rs index e4c1416f..ae9784ee 100644 --- a/crates/shirabe/tests/package/archiver/archivable_files_finder_test.rs +++ b/crates/shirabe/tests/package/archiver/archivable_files_finder_test.rs @@ -86,7 +86,7 @@ fn get_archivable_files(set_up: &SetUp, finder: ArchivableFilesFinder) -> Vec<St if !file.is_dir() { let real_path = file.canonicalize().unwrap(); files.push(Preg::replace( - &format!("#^{}#", preg_quote(&set_up.sources, Some('#'))), + format!("#^{}#", preg_quote(&set_up.sources, Some('#'))), "", &set_up.fs.normalize_path(&real_path.to_string_lossy()), )); |
