Commit 0844ba04 authored by Stan Hu's avatar Stan Hu

Significantly cut memory usage and SQL queries when reloading diffs

By preloading certain models with the diff, we can eliminate many N+1
queries. For a push to the staging GitLab.com www-gitlab-com repository,
this eliminates over 3000 SQL queries and appears to bring down the RSS
usage by several gigabytes.

Relates to https://gitlab.com/gitlab-org/gitlab-ce/issues/49703
parent 98a504ec
...@@ -36,7 +36,10 @@ module MergeRequests ...@@ -36,7 +36,10 @@ module MergeRequests
# Remove cache for all diffs on this MR. Do not use the association on the # Remove cache for all diffs on this MR. Do not use the association on the
# model, as that will interfere with other actions happening when # model, as that will interfere with other actions happening when
# reloading the diff. # reloading the diff.
MergeRequestDiff.where(merge_request: merge_request).each do |merge_request_diff| MergeRequestDiff
.where(merge_request: merge_request)
.preload(:merge_request_diff_files, merge_request: :target_project)
.find_each do |merge_request_diff|
next if merge_request_diff == new_diff next if merge_request_diff == new_diff
cacheable_collection(merge_request_diff).clear_cache cacheable_collection(merge_request_diff).clear_cache
......
---
title: Significantly cut memory usage and SQL queries when reloading diffs
merge_request: 22725
author:
type: performance
...@@ -60,6 +60,17 @@ describe MergeRequests::ReloadDiffsService, :use_clean_rails_memory_store_cachin ...@@ -60,6 +60,17 @@ describe MergeRequests::ReloadDiffsService, :use_clean_rails_memory_store_cachin
subject.execute subject.execute
end end
it 'avoids N+1 queries', :request_store do
current_user
merge_request
control_count = ActiveRecord::QueryRecorder.new do
subject.execute
end.count
expect { subject.execute }.not_to exceed_query_limit(control_count)
end
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