From 1974e0e333395ee3eccedc2410441a7b018187da Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Sat, 10 Jan 2015 02:55:55 -0500 Subject: [PATCH] 3021: Use Oj to encode API responses, and to decode them in tests. * We use the Oj and multi_json gems, which makes Oj the default JSON parser. However, Rails' ActiveRecord::Base overrides this and uses the native JSON parser, which is slow. In our case we have two render() calls that represent nearly all cases where we ask ActiveRecord to serialize for us. In both cases we already have a hash (not a model object), and we always want JSON responses. So we can fix the performance problem simply by calling Oj.dump() ourselves, and passing the resulting JSON (instead of the hash) to render(). More gory details: * "ActiveRecord::Base.extend kills JSON performance": https://github.com/rails/rails/issues/9212 * "when freedom patches fight, nobody wins": https://github.com/intridea/multi_json/pull/138#issuecomment-24468223 --- services/api/app/controllers/application_controller.rb | 7 +++++-- services/api/test/test_helper.rb | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb index 54d5adb439..a771042e52 100644 --- a/services/api/app/controllers/application_controller.rb +++ b/services/api/app/controllers/application_controller.rb @@ -77,7 +77,9 @@ class ApplicationController < ActionController::Base end def show - render json: @object.as_api_response(nil, select: @select) + render(text: Oj.dump(@object.as_api_response(nil, select: @select), + mode: :compat).html_safe, + content_type: 'application/json') end def create @@ -441,7 +443,8 @@ class ApplicationController < ActionController::Base except(:limit).except(:offset). count(:id, distinct: true) end - render json: @object_list + render(text: Oj.dump(@object_list, mode: :compat).html_safe, + content_type: 'application/json') end def remote_ip diff --git a/services/api/test/test_helper.rb b/services/api/test/test_helper.rb index 216dd2d02d..5ea6e62bfa 100644 --- a/services/api/test/test_helper.rb +++ b/services/api/test/test_helper.rb @@ -25,7 +25,7 @@ require 'rails/test_help' module ArvadosTestSupport def json_response - ActiveSupport::JSON.decode @response.body + Oj.load response.body end def api_token(api_client_auth_name) -- 2.30.2