Cache the placeholder regex for MCP hook argument expansion (#49956)

Use `LazyLock` to compile the placeholder regex once and reuse it across
argument strings, avoiding repeated compilation during template expansion.
Preserve regex error propagation and placeholder substitution behavior.

GitOrigin-RevId: 2ac8aec86de3430335c068e47d09422dcd276f7c
This commit is contained in:
jif
2026-10-01 11:01:37 +00:00
committed by copyberry
parent f70810bcd2
commit 8a400e7812
+4 -1
View File
@@ -1,3 +1,4 @@
use std::sync::LazyLock;
use std::time::Duration;
use std::time::Instant;
@@ -121,7 +122,9 @@ fn resolve_value(value: &Value, hook_event: &Value) -> Result<Value> {
}
fn resolve_string(text: &str, hook_event: &Value) -> Result<Value> {
let pattern = Regex::new(r"\$\{([^{}]+)\}")?;
static PLACEHOLDER_PATTERN: LazyLock<Result<Regex, regex::Error>> =
LazyLock::new(|| Regex::new(r"\$\{([^{}]+)\}"));
let pattern = PLACEHOLDER_PATTERN.as_ref().map_err(Clone::clone)?;
let captures = pattern.captures_iter(text).collect::<Vec<_>>();
if captures.is_empty() {
return Ok(Value::String(text.to_string()));