From 03bff08d74e1001b035750237ba41821e5cbcbde Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 21 May 2026 09:52:11 +0000 Subject: [PATCH] Optimize native_replace string allocation Co-authored-by: logbie <1138960+logbie@users.noreply.github.com> --- src/stdlib/text.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/stdlib/text.rs b/src/stdlib/text.rs index c9591d6b..cdeb9098 100644 --- a/src/stdlib/text.rs +++ b/src/stdlib/text.rs @@ -279,6 +279,12 @@ pub fn native_replace(args: Vec) -> Result { let text = expect_text(&args[0])?; let old = expect_text(&args[1])?; let new = expect_text(&args[2])?; + + // Optimization: avoid string allocation if string doesn't contain old + if !text.contains(old.as_ref()) { + return Ok(Value::Text(Arc::clone(&text))); + } + let result = text.replace(old.as_ref(), new.as_ref()); Ok(Value::Text(Arc::from(result))) }