Skip to content

Commit

Permalink
Disable method replacement in avatar loading
Browse files Browse the repository at this point in the history
We've seen a significant performance penalty when using
`BatchLoader#__replace_with!`. This defines methods on the batch loader
that proxy to the 'real' object using send. The alternative is
`method_missing`, which is slower.  However, we've noticed that
`method_missing` can be faster if:

1. The objects being loaded have a large interface.
2. We don't call too many methods on the loaded object.

Avatar uploads meet both criteria above, so let's use the newly-released
feature in exAspArk/batch-loader#45.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/60903
  • Loading branch information
stanhu committed Apr 30, 2019
1 parent 7ae2107 commit 25818bd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ gem 'gettext_i18n_rails', '~> 1.8.0'
gem 'gettext_i18n_rails_js', '~> 1.3'
gem 'gettext', '~> 3.2.2', require: false, group: :development

gem 'batch-loader', '~> 1.2.2'
gem 'batch-loader', '~> 1.4.0'

# Perf bar
gem 'peek', '~> 1.0.1'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ GEM
thread_safe (~> 0.3, >= 0.3.1)
babosa (1.0.2)
base32 (0.3.2)
batch-loader (1.2.2)
batch-loader (1.4.0)
bcrypt (3.1.12)
bcrypt_pbkdf (1.0.0)
benchmark-ips (2.3.0)
Expand Down Expand Up @@ -999,7 +999,7 @@ DEPENDENCIES
awesome_print
babosa (~> 1.0.2)
base32 (~> 0.3.0)
batch-loader (~> 1.2.2)
batch-loader (~> 1.4.0)
bcrypt_pbkdf (~> 1.0)
benchmark-ips (~> 2.3.0)
better_errors (~> 2.5.0)
Expand Down
3 changes: 2 additions & 1 deletion app/models/concerns/avatarable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def upload_paths(identifier)
private

def retrieve_upload_from_batch(identifier)
BatchLoader.for(identifier: identifier, model: self).batch(key: self.class) do |upload_params, loader, args|
BatchLoader.for(identifier: identifier, model: self)
.batch(key: self.class, cache: true, replace_methods: false) do |upload_params, loader, args|
model_class = args[:key]
paths = upload_params.flat_map do |params|
params[:model].upload_paths(params[:identifier])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Disable method replacement in avatar loading
merge_request: 27866
author:
type: performance
8 changes: 8 additions & 0 deletions spec/uploaders/object_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,14 @@ def when_file_is_in_use
expect { avatars }.not_to exceed_query_limit(1)
end

it 'does not attempt to replace methods' do
models.each do |model|
expect(model.avatar.upload).to receive(:method_missing).and_call_original

model.avatar.upload.path
end
end

it 'fetches a unique upload for each model' do
expect(avatars.map(&:url).uniq).to eq(avatars.map(&:url))
expect(avatars.map(&:upload).uniq).to eq(avatars.map(&:upload))
Expand Down

0 comments on commit 25818bd

Please sign in to comment.