Skip to content

Projects::MergeRequestsController#show is calling Gitaly n+1 times per request

Projects::MergeRequestsController#show is calling Gitaly n+1 times per request.

This code needs to be refactored to fetch all required information in a single call.

See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13996/diffs#e7197d6de2f2c6cccdd658294e682a61f5611ca2_22_18

Known Problem Methods

  • app/controllers/projects/merge_requests_controller.rb#show -> HAML -> app/models/diff_note.rb#diff_file -> app/models/repository.rb#compare_source_branch -> Gitlab::Git::Repository#compare_source_branch

Failing Tests

rspec ./spec/features/merge_requests/discussion_spec.rb:34 # Merge Request Discussions Diff discussions active discussions shows a link to the diff
rspec ./spec/features/merge_requests/discussion_spec.rb:43 # Merge Request Discussions Diff discussions outdated discussions shows a link to the outdated diff

Stacks

lib/gitlab/gitaly_client.rb:143:in `migrate'
lib/gitlab/git/repository.rb:103:in `exists?'
lib/gitlab/git/repository.rb:1067:in `empty_repo?'
lib/gitlab/git/repository.rb:967:in `with_repo_branch_commit'
lib/gitlab/git/repository.rb:1013:in `compare_source_branch'
app/models/repository.rb:988:in `compare_source_branch'
app/services/compare_service.rb:14:in `execute'
lib/gitlab/diff/diff_refs.rb:47:in `compare_in'
lib/gitlab/diff/position.rb:126:in `find_diff_file'
lib/gitlab/diff/position.rb:106:in `block in diff_file'
lib/gitlab/diff/position.rb:106:in `diff_file'
app/models/diff_note.rb:55:in `diff_file'
app/models/concerns/discussion_on_diff.rb:8:in `diff_file'
app/views/discussions/_discussion.html.haml:43:in `_app_views_discussions__discussion_html_haml___1820256011886992906_102654060'
app/views/shared/notes/_notes.html.haml:6:in `block in _app_views_shared_notes__notes_html_haml__3837511508606839836_103580720'
app/views/shared/notes/_notes.html.haml:2:in `each'
app/views/shared/notes/_notes.html.haml:2:in `_app_views_shared_notes__notes_html_haml__3837511508606839836_103580720'
app/views/shared/notes/_notes_with_form.html.haml:5:in `_app_views_shared_notes__notes_with_form_html_haml___3467108505439082615_96897620'
app/views/projects/merge_requests/_discussion.html.haml:11:in `_app_views_projects_merge_requests__discussion_html_haml__3153862007585751578_106192240'
app/views/projects/merge_requests/show.html.haml:79:in `_app_views_projects_merge_requests_show_html_haml__1641883456595104111_199038700'
app/controllers/projects/merge_requests_controller.rb:79:in `block (3 levels) in show'
lib/gitlab/gitaly_client.rb:195:in `allow_n_plus_1_calls'
app/controllers/projects/merge_requests_controller.rb:78:in `block (2 levels) in show'
app/controllers/projects/merge_requests_controller.rb:62:in `show'
Edited by Andrew Newdigate