Skip to content

Change JSON handling to oj

Robert May requested to merge faster-json-handling into master

What does this MR do?

Swap the JSON serializer used by Grape for rendering JSON to oj, which is notably faster. I've added an initializer which overrides how Grape dumps JSON objects, and enabled the :rails mode for Oj which takes .as_json into account when dumping objects.

Grape actually defines JSON here: https://github.com/ruby-grape/grape/blob/d58dc0ab7a0b51625217deedd8110d1030be7cf7/lib/grape/util/json.rb

But the default method I'm overriding in this initializer will ignore that class if the object responds to .to_json, which would mean we still wouldn't be using Oj if I only overrode that.

Screenshots

Some benchmarking on http://localhost:3001/api/v4/merge_requests

json  0.202972   0.000000   0.202972 (  0.205862)
oj  0.190346   0.004079   0.194425 (  0.194427)
       user     system      total        real
json  0.421077   0.000063   0.421140 (  0.421756)
oj  0.124736   0.000000   0.124736 (  0.124739)
       user     system      total        real
json  0.214097   0.004107   0.218204 (  0.218831)
oj  0.139805   0.000000   0.139805 (  0.139807)
       user     system      total        real
json  0.258781   0.000143   0.258924 (  0.259588)
oj  0.162006   0.000000   0.162006 (  0.162009)
       user     system      total        real
json  0.233887   0.000093   0.233980 (  0.234937)
oj  0.144202   0.004010   0.148212 (  0.148214)
       user     system      total        real
json  0.186218   0.000110   0.186328 (  0.186835)
oj  0.124846   0.000000   0.124846 (  0.124847)
       user     system      total        real
json  0.186856   0.003946   0.190802 (  0.191002)
oj  0.193830   0.000093   0.193923 (  0.193926)
       user     system      total        real
json  0.191650   0.000000   0.191650 (  0.191935)
oj  0.129208   0.000045   0.129253 (  0.129255)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related #33150 (closed) Closes #25341

Edited by Robert May

Merge request reports

Loading