From 748847edbcdb74f01c9c4ed0d8f37c321e18328e Mon Sep 17 00:00:00 2001 From: Daniel O'Connor Date: Sun, 20 Sep 2026 14:41:13 +0930 Subject: [PATCH] Optimize markdownify allocations with in-place gsub! and combined regex (#4805) * Optimize markdownify allocations with in-place gsub and combined regex Avoid chaining multiple full-string copies during markdown parsing. Duplicate the input string once in ApplicationHelper#markdownify and modify it in-place using gsub! in GrowstuffMarkdown#expand_crops! and #expand_members!. Combine MEMBER_REGEX and MEMBER_AT_REGEX with Regexp.union to parse member mentions in a single pass. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> * Apply suggestion from @CloCkWeRX * Apply suggestion from @CloCkWeRX * Optimize markdownify allocations with in-place gsub and combined regex Avoid chaining multiple full-string copies during markdown parsing. Duplicate the input string once in ApplicationHelper#markdownify and modify it in-place using gsub! in GrowstuffMarkdown#expand_crops! and #expand_members!. Combine MEMBER_REGEX and MEMBER_AT_REGEX with Regexp.union to parse member mentions in a single pass. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- app/helpers/application_helper.rb | 5 ++++- lib/haml/filters/growstuff_markdown.rb | 17 +++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1922d321f..1f432ca35 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -59,7 +59,10 @@ module ApplicationHelper # of HAML, Tilt, and dynamic compilation with interpolated ruby. def markdownify(text) translator = Haml::Filters::GrowstuffMarkdown.new - translator.expand_members!(translator.expand_crops!(text.to_s)) + text = text.to_s.dup + translator.expand_crops!(text) + translator.expand_members!(text) + text end # diff --git a/lib/haml/filters/growstuff_markdown.rb b/lib/haml/filters/growstuff_markdown.rb index 0ce797eee..7ba0a38fe 100644 --- a/lib/haml/filters/growstuff_markdown.rb +++ b/lib/haml/filters/growstuff_markdown.rb @@ -6,31 +6,32 @@ class Haml::Filters CROP_REGEX = /(?