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-external-packages/src/symfony/console | |
| 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-external-packages/src/symfony/console')
13 files changed, 57 insertions, 39 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/command/command.rs b/crates/shirabe-external-packages/src/symfony/console/command/command.rs index 81eb3c08..ef4a471a 100644 --- a/crates/shirabe-external-packages/src/symfony/console/command/command.rs +++ b/crates/shirabe-external-packages/src/symfony/console/command/command.rs @@ -11,7 +11,7 @@ use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::{self, OutputInterface}; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; use std::cell::{Cell, Ref}; /// The base-class state of the PHP `Command` class. @@ -136,7 +136,8 @@ impl CommandData { /// Throws InvalidArgumentException when the name is invalid. fn validate_name(&self, name: &str) -> anyhow::Result<Result<(), InvalidArgumentException>> { let mut matches: Vec<Option<String>> = Vec::new(); - if !shirabe_php_shim::preg_match(r"/^[^\:]++(\:[^\:]++)*$/", name, &mut matches) { + if !shirabe_php_shim::preg_match(php_regex!(r"/^[^\:]++(\:[^\:]++)*$/"), name, &mut matches) + { return Ok(Err(InvalidArgumentException( shirabe_php_shim::InvalidArgumentException { message: format!("Command name \"{}\" is invalid.", name), diff --git a/crates/shirabe-external-packages/src/symfony/console/completion/completion_input.rs b/crates/shirabe-external-packages/src/symfony/console/completion/completion_input.rs index e0b8e09d..d076f8a3 100644 --- a/crates/shirabe-external-packages/src/symfony/console/completion/completion_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/completion/completion_input.rs @@ -3,7 +3,7 @@ use crate::symfony::console::input::argv_input::ArgvInput; use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_option::InputOption; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; /// An input specialized for shell completion. /// @@ -30,7 +30,7 @@ impl CompletionInput { /// This is required for shell completions without COMP_WORDS support. pub fn from_string(input_str: &str, current_index: i64) -> anyhow::Result<Self> { let tokens = shirabe_php_shim::preg_match_all( - "/(?<=^|\\s)(['\"]?)(.+?)(?<!\\\\)\\1(?=$|\\s)/", + php_regex!("/(?<=^|\\s)(['\"]?)(.+?)(?<!\\\\)\\1(?=$|\\s)/"), input_str, ); diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs index 4f8e335f..6c1665e9 100644 --- a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs +++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs @@ -13,7 +13,7 @@ use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_option::InputOption; use crate::symfony::console::output::output_interface::OutputInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; /// JSON descriptor. /// @@ -163,7 +163,7 @@ impl JsonDescriptor { data.insert( "description".to_string(), PhpMixed::String(Preg::replace( - "/\\s*[\\r\\n]\\s*/", + php_regex!("/\\s*[\\r\\n]\\s*/"), " ", argument.get_description(), )), @@ -224,7 +224,7 @@ impl JsonDescriptor { data.insert( "description".to_string(), PhpMixed::String(Preg::replace( - "/\\s*[\\r\\n]\\s*/", + php_regex!("/\\s*[\\r\\n]\\s*/"), " ", option.get_description(), )), diff --git a/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs b/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs index 6736adac..6651d9cd 100644 --- a/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs +++ b/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs @@ -7,6 +7,7 @@ use crate::symfony::console::formatter::output_formatter_style_interface::Output use crate::symfony::console::formatter::output_formatter_style_stack::OutputFormatterStyleStack; use crate::symfony::console::formatter::wrappable_output_formatter_interface::WrappableOutputFormatterInterface; use crate::symfony::string::b; +use shirabe_php_shim::php_regex; /// Formatter class for console output. #[derive(Debug)] @@ -19,7 +20,8 @@ pub struct OutputFormatter { impl OutputFormatter { /// Escapes "<" and ">" special chars in given text. pub fn escape(text: &str) -> anyhow::Result<String> { - let text = shirabe_php_shim::preg_replace("/([^\\\\]|^)([<>])/", "$1\\\\$2", text); + let text = + shirabe_php_shim::preg_replace(php_regex!("/([^\\\\]|^)([<>])/"), "$1\\\\$2", text); Ok(Self::escape_trailing_backslash(&text)) } @@ -102,7 +104,7 @@ impl OutputFormatter { let mut matches: Vec<Vec<String>> = vec![]; if shirabe_php_shim::preg_match_all_set_order( - "/([^=]+)=([^;]+)(;|$)/", + php_regex!("/([^=]+)=([^;]+)(;|$)/"), string, &mut matches, ) == 0 @@ -121,11 +123,12 @@ impl OutputFormatter { } else if r#match[0] == "bg" { style.set_background(Some(&shirabe_php_shim::strtolower(&r#match[1]))); } else if r#match[0] == "href" { - let url = shirabe_php_shim::preg_replace("{\\\\([<>])}", "$1", &r#match[1]); + let url = + shirabe_php_shim::preg_replace(php_regex!("{\\\\([<>])}"), "$1", &r#match[1]); style.set_href(&url); } else if r#match[0] == "options" { let mut options = shirabe_php_shim::preg_match_all( - "([^,;]+)", + php_regex!("([^,;]+)"), &shirabe_php_shim::strtolower(&r#match[1]), ); let options = shirabe_php_shim::array_shift(&mut options).unwrap_or_default(); @@ -176,7 +179,7 @@ impl OutputFormatter { } let mut matches: Vec<Option<String>> = vec![]; - shirabe_php_shim::preg_match("~(\\n)$~", &text, &mut matches); + shirabe_php_shim::preg_match(php_regex!("~(\\n)$~"), &text, &mut matches); text = format!("{}{}", prefix, self.add_line_breaks(&text, width)); let trailing = matches.get(1).and_then(|m| m.clone()).unwrap_or_default(); text = format!("{}{}", shirabe_php_shim::rtrim(&text, Some("\n")), trailing); @@ -287,7 +290,7 @@ impl WrappableOutputFormatterInterface for OutputFormatter { let mut current_line_length: i64 = 0; let mut matches: shirabe_php_shim::PregOffsetCaptureMatches = Default::default(); shirabe_php_shim::preg_match_all_offset_capture( - &format!("#<(({open_tag_regex}) | /({close_tag_regex})?)>#ix"), + format!("#<(({open_tag_regex}) | /({close_tag_regex})?)>#ix"), message, &mut matches, ); diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/helper.rs index be5eb050..20103df8 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/helper.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/helper.rs @@ -3,6 +3,7 @@ use crate::symfony::console::formatter::output_formatter_interface::OutputFormatterInterface; use crate::symfony::console::helper::helper_set::HelperSet; use crate::symfony::string::unicode_string::UnicodeString; +use shirabe_php_shim::php_regex; /// Helper is the base class for all helper classes. #[derive(Debug, Default)] @@ -39,7 +40,7 @@ impl Helper { /// Returns the width of a string, using mb_strwidth if it is available. /// The width is how many characters positions the string will use. pub fn width(string: &str) -> i64 { - if shirabe_php_shim::preg_match("//u", string, &mut Vec::new()) { + if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) { return UnicodeString::new(string).width(false); } @@ -55,7 +56,7 @@ impl Helper { /// Returns the length of a string, using mb_strlen if it is available. /// The length is related to how many bytes the string will use. pub fn length(string: &str) -> i64 { - if shirabe_php_shim::preg_match("//u", string, &mut Vec::new()) { + if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) { return UnicodeString::new(string).length(); } @@ -147,10 +148,13 @@ impl Helper { // remove <...> formatting let string = formatter.format(Some(string)).unwrap().unwrap_or_default(); // remove already formatted characters - let string = shirabe_php_shim::preg_replace("/\u{1b}\\[[^m]*m/", "", &string); + let string = shirabe_php_shim::preg_replace(php_regex!("/\u{1b}\\[[^m]*m/"), "", &string); // remove terminal hyperlinks - let string = - shirabe_php_shim::preg_replace("/\u{1b}]8;[^;]*;[^\u{1b}]*\u{1b}\\\\/", "", &string); + let string = shirabe_php_shim::preg_replace( + php_regex!("/\u{1b}]8;[^;]*;[^\u{1b}]*\u{1b}\\\\/"), + "", + &string, + ); formatter.set_decorated(is_decorated); string diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/table.rs b/crates/shirabe-external-packages/src/symfony/console/helper/table.rs index cae3f896..c5d7e346 100644 --- a/crates/shirabe-external-packages/src/symfony/console/helper/table.rs +++ b/crates/shirabe-external-packages/src/symfony/console/helper/table.rs @@ -14,7 +14,7 @@ use crate::symfony::console::helper::table_style::TableStyle; use crate::symfony::console::output::console_section_output::ConsoleSectionOutput; use crate::symfony::console::output::output_interface::OutputInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; /// A single cell within a table row. /// @@ -847,7 +847,7 @@ impl Table { let mut pad_type = style.get_pad_type(); if cell.is_table_cell() && cell.style().is_some() { let is_not_styled_by_tag = !Preg::is_match( - "/^<(\\w+|(\\w+=[\\w,]+;?)*)>.+<\\/(\\w+|(\\w+=\\w+;?)*)?>$/", + php_regex!("/^<(\\w+|(\\w+=[\\w,]+;?)*)>.+<\\/(\\w+|(\\w+=\\w+;?)*)?>$/"), &cell_str, ); if is_not_styled_by_tag { diff --git a/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs index ad06da72..7c24da60 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/argv_input.rs @@ -6,7 +6,7 @@ use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::streamable_input_interface::StreamableInputInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; /// ArgvInput represents an input coming from the CLI arguments. /// @@ -512,7 +512,8 @@ impl std::fmt::Display for ArgvInput { .iter() .map(|token| { let mut r#match: Vec<Option<String>> = Vec::new(); - if shirabe_php_shim::preg_match("{^(-[^=]+=)(.+)}", token, &mut r#match) { + if shirabe_php_shim::preg_match(php_regex!("{^(-[^=]+=)(.+)}"), token, &mut r#match) + { return format!( "{}{}", r#match[1].as_deref().unwrap_or(""), diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input.rs b/crates/shirabe-external-packages/src/symfony/console/input/input.rs index 70b61851..e14ed354 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input.rs @@ -4,7 +4,7 @@ use crate::symfony::console::exception::invalid_argument_exception::InvalidArgum use crate::symfony::console::exception::runtime_exception::RuntimeException; use crate::symfony::console::input::input_definition::InputDefinition; use indexmap::IndexMap; -use shirabe_php_shim::{PhpMixed, PhpResource}; +use shirabe_php_shim::{PhpMixed, PhpResource, php_regex}; /// Input is the base class for all concrete Input classes. /// @@ -219,7 +219,7 @@ impl Input { /// Escapes a token through escapeshellarg if it contains unsafe chars. pub fn escape_token(&self, token: &str) -> String { let mut matches: Vec<Option<String>> = vec![]; - if shirabe_php_shim::preg_match("{^[\\w-]+$}", token, &mut matches) { + if shirabe_php_shim::preg_match(php_regex!("{^[\\w-]+$}"), token, &mut matches) { token.to_string() } else { shirabe_php_shim::escapeshellarg(token) diff --git a/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs b/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs index bd93f490..3f8fdeaf 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/input_option.rs @@ -2,7 +2,7 @@ use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; use crate::symfony::console::exception::logic_exception::LogicException; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; #[derive(Debug, Clone)] pub struct InputOption { @@ -110,7 +110,7 @@ impl InputOption { fn normalize_shortcut(s: String) -> anyhow::Result<Option<String>> { let stripped = shirabe_php_shim::ltrim(&s, Some("-")); - let parts = shirabe_php_shim::preg_split(r"{(\|)-?}", &stripped); + let parts = shirabe_php_shim::preg_split(php_regex!(r"{(\|)-?}"), &stripped); let filtered: Vec<String> = shirabe_php_shim::array_filter(&parts, |s: &String| !s.is_empty()); let result = shirabe_php_shim::implode("|", &filtered); diff --git a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs index 122e588e..c9871c34 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs @@ -6,7 +6,7 @@ use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_interface::InputInterface; use crate::symfony::console::input::streamable_input_interface::StreamableInputInterface; use indexmap::IndexMap; -use shirabe_php_shim::{CaptureKey, PhpMixed}; +use shirabe_php_shim::{CaptureKey, PhpMixed, php_regex}; /// StringInput represents an input provided as a string. /// @@ -58,14 +58,20 @@ impl StringInput { } let mut m: IndexMap<CaptureKey, Option<String>> = IndexMap::new(); - if shirabe_php_shim::preg_match2(r"/\s+/A", input, &mut m, 0, cursor as usize) { + if shirabe_php_shim::preg_match2( + php_regex!(r"/\s+/A"), + input, + &mut m, + 0, + cursor as usize, + ) { if token.is_some() { tokens.push(token.take().unwrap()); } cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); } else if shirabe_php_shim::preg_match2( - &format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING), + format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING), input, &mut m, 0, @@ -88,7 +94,7 @@ impl StringInput { cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); } else if shirabe_php_shim::preg_match2( - &format!(r"/{}/A", Self::REGEX_QUOTED_STRING), + format!(r"/{}/A", Self::REGEX_QUOTED_STRING), input, &mut m, 0, @@ -106,7 +112,7 @@ impl StringInput { cursor += shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); } else if shirabe_php_shim::preg_match2( - &format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING), + format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING), input, &mut m, 0, diff --git a/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs b/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs index ac85f3a4..4d8eb921 100644 --- a/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs +++ b/crates/shirabe-external-packages/src/symfony/console/output/stream_output.rs @@ -5,6 +5,7 @@ use crate::symfony::console::formatter::OutputFormatterInterface; use crate::symfony::console::output::OutputInterface; use crate::symfony::console::output::output::{DoWrite, Output}; use crate::symfony::console::output::output_interface::VERBOSITY_NORMAL; +use shirabe_php_shim::php_regex; /// StreamOutput writes the output to a given stream. /// @@ -127,7 +128,9 @@ impl StreamOutput { // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157 let mut matches: Vec<Option<String>> = Vec::new(); shirabe_php_shim::preg_match( - "/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/", + php_regex!( + "/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/" + ), &term, &mut matches, ) diff --git a/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs b/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs index a8cde916..848888a6 100644 --- a/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs +++ b/crates/shirabe-external-packages/src/symfony/console/question/choice_question.rs @@ -5,7 +5,7 @@ use crate::symfony::console::exception::logic_exception::LogicException; use crate::symfony::console::question::Question; use crate::symfony::console::question::QuestionInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; /// Represents a choice question. #[derive(Debug)] @@ -125,7 +125,7 @@ impl ChoiceQuestion { // Check for a separated comma values let mut matches: Vec<Option<String>> = Vec::new(); if !shirabe_php_shim::preg_match( - "/^[^,]+(?:,[^,]+)*$/", + php_regex!("/^[^,]+(?:,[^,]+)*$/"), &shirabe_php_shim::strval(&selected), &mut matches, ) { diff --git a/crates/shirabe-external-packages/src/symfony/console/terminal.rs b/crates/shirabe-external-packages/src/symfony/console/terminal.rs index cc8f8128..7fca27e8 100644 --- a/crates/shirabe-external-packages/src/symfony/console/terminal.rs +++ b/crates/shirabe-external-packages/src/symfony/console/terminal.rs @@ -1,6 +1,6 @@ //! ref: composer/vendor/symfony/console/Terminal.php -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{PhpMixed, php_regex}; use std::cell::Cell; thread_local! { @@ -86,7 +86,7 @@ impl Terminal { let mut matches: Vec<Option<String>> = Vec::new(); if let Some(ansicon) = &ansicon && shirabe_php_shim::preg_match( - "/^(\\d+)x(\\d+)(?: \\((\\d+)x(\\d+)\\))?$/", + php_regex!("/^(\\d+)x(\\d+)(?: \\((\\d+)x(\\d+)\\))?$/"), &shirabe_php_shim::trim(&ansicon.to_string_lossy(), None), &mut matches, ) @@ -143,7 +143,7 @@ impl Terminal { } let mut matches: Vec<Option<String>> = Vec::new(); if shirabe_php_shim::preg_match( - "/rows.(\\d+);.columns.(\\d+);/i", + php_regex!("/rows.(\\d+);.columns.(\\d+);/i"), &stty_string, &mut matches, ) { @@ -159,7 +159,7 @@ impl Terminal { )))) }); } else if shirabe_php_shim::preg_match( - "/;.(\\d+).rows;.(\\d+).columns/i", + php_regex!("/;.(\\d+).rows;.(\\d+).columns/i"), &stty_string, &mut matches, ) { @@ -187,7 +187,7 @@ impl Terminal { let info = info?; let mut matches: Vec<Option<String>> = Vec::new(); if !shirabe_php_shim::preg_match( - "/--------+\\r?\\n.+?(\\d+)\\r?\\n.+?(\\d+)\\r?\\n/", + php_regex!("/--------+\\r?\\n.+?(\\d+)\\r?\\n.+?(\\d+)\\r?\\n/"), &info, &mut matches, ) { |
