summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-10-20 16:26:53 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-10-20 16:26:53 +0000
commit249a9476d44e89bf1ab12fbc40bf81478faafd12 (patch)
tree6e44d47264475a7ec411534758b2efded50253a3
parentf16bfa401f4f2acc11ff089df8ba0bb092df416a (diff)
parente1c3077e4bb718ce841fad175f708623d8375818 (diff)
downloadgitlab-ce-249a9476d44e89bf1ab12fbc40bf81478faafd12.tar.gz
Merge branch 'reference-filter-replace-text-nodes-performance' into 'master'
Speed up searching for text references a bit See merge request !1648
-rw-r--r--CHANGELOG1
-rw-r--r--lib/gitlab/markdown/reference_filter.rb8
-rw-r--r--spec/benchmarks/lib/gitlab/markdown/reference_filter_spec.rb41
3 files changed, 46 insertions, 4 deletions
diff --git a/CHANGELOG b/CHANGELOG
index c64017f75b9..e956d074d72 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date.
v 8.2.0 (unreleased)
+ - Improved performance of replacing references in comments
- Fix duplicate repositories in GitHub import page (Stan Hu)
- Redirect to a default path if HTTP_REFERER is not set (Stan Hu)
- Show last project commit to default branch on project home page
diff --git a/lib/gitlab/markdown/reference_filter.rb b/lib/gitlab/markdown/reference_filter.rb
index adaca78ba27..a4c560f578c 100644
--- a/lib/gitlab/markdown/reference_filter.rb
+++ b/lib/gitlab/markdown/reference_filter.rb
@@ -15,7 +15,7 @@ module Gitlab
LazyReference = Struct.new(:klass, :ids) do
def self.load(refs)
lazy_references, values = refs.partition { |ref| ref.is_a?(self) }
-
+
lazy_values = lazy_references.group_by(&:klass).flat_map do |klass, refs|
ids = refs.flat_map(&:ids)
klass.where(id: ids)
@@ -107,10 +107,10 @@ module Gitlab
return doc if project.nil?
search_text_nodes(doc).each do |node|
- content = node.to_html
-
- next unless content.match(pattern)
next if ignored_ancestry?(node)
+ next unless node.text =~ pattern
+
+ content = node.to_html
html = yield content
diff --git a/spec/benchmarks/lib/gitlab/markdown/reference_filter_spec.rb b/spec/benchmarks/lib/gitlab/markdown/reference_filter_spec.rb
new file mode 100644
index 00000000000..34cd9f7e4eb
--- /dev/null
+++ b/spec/benchmarks/lib/gitlab/markdown/reference_filter_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+describe Gitlab::Markdown::ReferenceFilter, benchmark: true do
+ let(:input) do
+ html = <<-EOF
+<p>Hello @alice and @bob, how are you doing today?</p>
+<p>This is simple @dummy text to see how the @ReferenceFilter class performs
+when @processing HTML.</p>
+ EOF
+
+ Nokogiri::HTML.fragment(html)
+ end
+
+ let(:project) { create(:empty_project) }
+
+ let(:filter) { described_class.new(input, project: project) }
+
+ describe '#replace_text_nodes_matching' do
+ let(:iterations) { 6000 }
+
+ describe 'with identical input and output HTML' do
+ benchmark_subject do
+ filter.replace_text_nodes_matching(User.reference_pattern) do |content|
+ content
+ end
+ end
+
+ it { is_expected.to iterate_per_second(iterations) }
+ end
+
+ describe 'with different input and output HTML' do
+ benchmark_subject do
+ filter.replace_text_nodes_matching(User.reference_pattern) do |content|
+ '@eve'
+ end
+ end
+
+ it { is_expected.to iterate_per_second(iterations) }
+ end
+ end
+end