diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2018-06-08 20:00:23 -0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2018-06-08 20:00:23 -0300 |
commit | 75290ce06bbb79e779f90dda75d30222d77b61c0 (patch) | |
tree | 0a79d1718710d43ae56821d8870a176ca25669d5 | |
parent | ad96d64158f8a2ea98ec8f850379831791b57ded (diff) | |
download | gitlab-ce-75290ce06bbb79e779f90dda75d30222d77b61c0.tar.gz |
Improve lazy loading and cache usage for hidden content blobs
-rw-r--r-- | app/models/blobs_service.rb | 25 | ||||
-rw-r--r-- | lib/gitlab/diff/file.rb | 16 |
2 files changed, 28 insertions, 13 deletions
diff --git a/app/models/blobs_service.rb b/app/models/blobs_service.rb index a7c3b3e7bfa..e35417638bb 100644 --- a/app/models/blobs_service.rb +++ b/app/models/blobs_service.rb @@ -5,17 +5,19 @@ class BlobsService CACHED_METHODS = %i(id raw_size size readable_text? name raw_binary? binary? path external_storage_error? stored_externally? total_lines).freeze - def initialize(project, content_sha, path) + def initialize(project, content_sha, path, hidden_content:, highlighted:) @project = project @content_sha = content_sha @path = path + @hidden_content = hidden_content + @highlighted = highlighted end def lazy_load_uncached_blob return unless content_sha - return if cache_exists? + return if hidden_content && cache_exists? - uncached_blob + lazy_uncached_blob end # We need blobs data (content) in order to highlight diffs (see @@ -24,11 +26,12 @@ class BlobsService # # Therefore, in this scenario (no highlight yet) we use the uncached blob # version. - def fetch(highlighted:) + def fetch return unless content_sha + return cached_blob if hidden_content && cache_exists? return uncached_blob unless highlighted - cache_exists? ? cached_blob : uncached_blob&.itself + cache_exists? ? cached_blob : uncached_blob end @@ -46,7 +49,7 @@ class BlobsService private - attr_reader :content_sha, :project, :path + attr_reader :content_sha, :project, :path, :hidden_content, :highlighted def cacheable_blob_hash CACHED_METHODS.each_with_object({}) do |_method, hash| @@ -54,14 +57,18 @@ class BlobsService end end - def cached_blob - CachedBlob.new(read_cache) + def uncached_blob + lazy_uncached_blob&.itself end - def uncached_blob + def lazy_uncached_blob Blob.lazy(project, content_sha, path) end + def cached_blob + CachedBlob.new(read_cache) + end + def cache @cache ||= Rails.cache end diff --git a/lib/gitlab/diff/file.rb b/lib/gitlab/diff/file.rb index 105f67de839..ac8b1334dda 100644 --- a/lib/gitlab/diff/file.rb +++ b/lib/gitlab/diff/file.rb @@ -107,19 +107,23 @@ module Gitlab end def new_blob_service - BlobsService.new(repository.project, new_content_sha, file_path) + BlobsService.new(repository.project, new_content_sha, file_path, + hidden_content: hidden_content?, + highlighted: highlighted?) end def old_blob_service - BlobsService.new(repository.project, old_content_sha, file_path) + BlobsService.new(repository.project, old_content_sha, file_path, + hidden_content: hidden_content?, + highlighted: highlighted?) end def new_blob - new_blob_service.fetch(highlighted: highlighted?) + new_blob_service.fetch end def old_blob - old_blob_service.fetch(highlighted: highlighted?) + old_blob_service.fetch end def content_sha @@ -244,6 +248,10 @@ module Gitlab private + def hidden_content? + collapsed? || too_large? || raw_diff.empty? + end + def highlighted? @highlighted_diff_lines.present? end |