Skip to content

Commit

Permalink
Avoid using an instance variable for results.
Browse files Browse the repository at this point in the history
This was a performance related change.
My belief is that the serializer has to do a method call for every single DOI in the result set and as the size is increased, this adds a significant overhead of 2-3 seconds overall.
  • Loading branch information
richardhallett committed Apr 12, 2019
1 parent de5f1ec commit 9234064
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def index

respond_to do |format|
format.json do
@dois = results
options = {}
options[:meta] = {
total: total,
Expand All @@ -161,11 +160,11 @@ def index

options[:links] = {
self: request.original_url,
next: @dois.size < page[:size] ? nil : request.base_url + "/dois?" + {
next: results.size < page[:size] ? nil : request.base_url + "/dois?" + {
query: params[:query],
"provider-id" => params[:provider_id],
"client-id" => params[:client_id],
"page[cursor]" => page[:cursor].present? ? Array.wrap(@dois.to_a.last[:sort]).first : nil,
"page[cursor]" => page[:cursor].present? ? Array.wrap(results.to_a.last[:sort]).first : nil,
"page[number]" => page[:cursor].blank? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand All @@ -176,7 +175,7 @@ def index
}

logger.info "[Benchmark] render " + Benchmark.ms {
render json: DoiSerializer.new(@dois, options).serialized_json, status: :ok
render json: DoiSerializer.new(results, options).serialized_json, status: :ok
}.to_s + " ms"
end
format.citation do
Expand All @@ -187,7 +186,7 @@ def index
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
Expand Down Expand Up @@ -395,7 +394,7 @@ def get_dois
head :no_content and return unless client_prefix.present?

dois = Doi.get_dois(prefix: client_prefix.prefix, username: current_user.uid.upcase, password: current_user.password)
if dois.length > 0
if dois.length > 0
render json: { dois: dois }.to_json, status: :ok
else
head :no_content
Expand Down

0 comments on commit 9234064

Please sign in to comment.