* 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
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
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
module ArvadosTestSupport
def json_response
- ActiveSupport::JSON.decode @response.body
+ Oj.load response.body
end
def api_token(api_client_auth_name)