Commit 336d3ccc authored by Kerri Miller's avatar Kerri Miller Committed by Douwe Maan

Initial commit of WIP code for consideration

Squash this commit and reword before merging..
parent 35e49d3b
---
title: Batch processing of commit refs in markdown processing
merge_request: 31037
author:
type: performance
...@@ -337,6 +337,24 @@ module Banzai ...@@ -337,6 +337,24 @@ module Banzai
@current_project_namespace_path ||= project&.namespace&.full_path @current_project_namespace_path ||= project&.namespace&.full_path
end end
def records_per_parent
@_records_per_project ||= {}
@_records_per_project[object_class.to_s.underscore] ||= begin
hash = Hash.new { |h, k| h[k] = {} }
parent_per_reference.each do |path, parent|
record_ids = references_per_parent[path]
parent_records(parent, record_ids).each do |record|
hash[parent][record_identifier(record)] = record
end
end
hash
end
end
private private
def full_project_path(namespace, project_ref) def full_project_path(namespace, project_ref)
......
...@@ -19,12 +19,11 @@ module Banzai ...@@ -19,12 +19,11 @@ module Banzai
end end
def find_object(project, id) def find_object(project, id)
return unless project.is_a?(Project) return unless project.is_a?(Project) && project.valid_repo?
if project && project.valid_repo? _, record = records_per_parent[project].detect { |k, _v| Gitlab::Git.shas_eql?(k, id) }
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/43894
Gitlab::GitalyClient.allow_n_plus_1_calls { project.commit(id) } record
end
end end
def referenced_merge_request_commit_shas def referenced_merge_request_commit_shas
...@@ -66,6 +65,14 @@ module Banzai ...@@ -66,6 +65,14 @@ module Banzai
private private
def record_identifier(record)
record.id
end
def parent_records(parent, ids)
parent.commits_by(oids: ids.to_a)
end
def noteable def noteable
context[:noteable] context[:noteable]
end end
......
...@@ -3,22 +3,8 @@ ...@@ -3,22 +3,8 @@
module Banzai module Banzai
module Filter module Filter
class IssuableReferenceFilter < AbstractReferenceFilter class IssuableReferenceFilter < AbstractReferenceFilter
def records_per_parent def record_identifier(record)
@records_per_project ||= {} record.iid.to_i
@records_per_project[object_class.to_s.underscore] ||= begin
hash = Hash.new { |h, k| h[k] = {} }
parent_per_reference.each do |path, parent|
record_ids = references_per_parent[path]
parent_records(parent, record_ids).each do |record|
hash[parent][record.iid.to_i] = record
end
end
hash
end
end end
def find_object(parent, iid) def find_object(parent, iid)
......
...@@ -105,6 +105,17 @@ describe Banzai::Filter::CommitReferenceFilter do ...@@ -105,6 +105,17 @@ describe Banzai::Filter::CommitReferenceFilter do
expect(doc.css('a').first[:href]).to eq(url) expect(doc.css('a').first[:href]).to eq(url)
end end
context "a doc with many (29) strings that could be SHAs" do
let!(:oids) { noteable.commits.collect(&:id) }
it 'makes only a single request to Gitaly' do
expect(Gitlab::GitalyClient).to receive(:allow_n_plus_1_calls).exactly(0).times
expect(Gitlab::Git::Commit).to receive(:batch_by_oid).once.and_call_original
reference_filter("A big list of SHAs #{oids.join(", ")}", noteable: noteable)
end
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment