Improve render performance of large wiki pages

Compile often used regexps only once outside of the loop.

This commit improves the performance of large wiki pages with many, many references.
This commit is contained in:
Peter Leitzen 2018-07-07 13:53:45 +02:00
parent d32d8bae67
commit 997d76fe94

View file

@ -100,6 +100,11 @@ module Banzai
ref_pattern = object_class.reference_pattern
link_pattern = object_class.link_reference_pattern
# Compile often used regexps only once outside of the loop
ref_pattern_anchor = /\A#{ref_pattern}\z/
link_pattern_start = /\A#{link_pattern}/
link_pattern_anchor = /\A#{link_pattern}\z/
nodes.each do |node|
if text_node?(node) && ref_pattern
replace_text_when_pattern_matches(node, ref_pattern) do |content|
@ -108,7 +113,7 @@ module Banzai
elsif element_node?(node)
yield_valid_link(node) do |link, inner_html|
if ref_pattern && link =~ /\A#{ref_pattern}\z/
if ref_pattern && link =~ ref_pattern_anchor
replace_link_node_with_href(node, link) do
object_link_filter(link, ref_pattern, link_content: inner_html)
end
@ -118,7 +123,7 @@ module Banzai
next unless link_pattern
if link == inner_html && inner_html =~ /\A#{link_pattern}/
if link == inner_html && inner_html =~ link_pattern_start
replace_link_node_with_text(node, link) do
object_link_filter(inner_html, link_pattern, link_reference: true)
end
@ -126,7 +131,7 @@ module Banzai
next
end
if link =~ /\A#{link_pattern}\z/
if link =~ link_pattern_anchor
replace_link_node_with_href(node, link) do
object_link_filter(link, link_pattern, link_content: inner_html, link_reference: true)
end