aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/helper
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-07-18 15:03:55 +0900
committernsfisis <nsfisis@gmail.com>2026-07-18 15:54:27 +0900
commit91692846909ed191addb7ec1c34aad11392ab88b (patch)
tree7c477055e432fd43a98e5dddc016e07dcfc67f60 /crates/shirabe-external-packages/src/symfony/console/helper
parent4ae58baf8618f5fe916ba2a69faaca93514134ce (diff)
downloadphp-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/helper')
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/helper.rs14
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/table.rs4
2 files changed, 11 insertions, 7 deletions
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 {