From ad3d0585aa2d36e57b53781a5bd6e3dbe96cb71d Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 31 May 2016 19:35:33 -0400 Subject: Fix serious performance bug with rendering Markdown with InlineDiffFilter Nokogiri's `node.replace` was being unnecessarily called for every text node in the document due to a comparison bug. The code previously was comparing the HTML representation of the full document against the text node, which would always fail. Fix the comparison to just compare the modified text. Closes #18011 --- CHANGELOG | 1 + lib/banzai/filter/inline_diff_filter.rb | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d1cde40c1c7..9a5d1341126 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -30,6 +30,7 @@ v 8.9.0 (unreleased) v 8.8.3 - Fix incorrect links on pipeline page when merge request created from fork - Fix gitlab importer failing to import new projects due to missing credentials + - Fix serious performance bug with rendering Markdown with InlineDiffFilter - Fix import URL migration not rescuing with the correct Error - In search results, only show notes on confidential issues that the user has access to - Fix health check access token changing due to old application settings being used diff --git a/lib/banzai/filter/inline_diff_filter.rb b/lib/banzai/filter/inline_diff_filter.rb index 9e75edd4d4c..beb21b19ab3 100644 --- a/lib/banzai/filter/inline_diff_filter.rb +++ b/lib/banzai/filter/inline_diff_filter.rb @@ -8,15 +8,19 @@ module Banzai next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS) content = node.to_html - content = content.gsub(/(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})/, '\1\2') - content = content.gsub(/(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})/, '\1\2') + html_content = inline_diff_filter(content) - next if html == content + next if content == html_content - node.replace(content) + node.replace(html_content) end doc end + + def inline_diff_filter(text) + html_content = text.gsub(/(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})/, '\1\2') + html_content.gsub(/(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})/, '\1\2') + end end end end -- cgit v1.2.1