Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Improve JSON Performance

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Improve JSON Performance

Avatar for Watson

Watson

May 31, 2018
Tweet

More Decks by Watson

Other Decks in Technology

Transcript

  1. Parsed Hash object require 'json' json =<<'END' { "id": 1,

    "uuid": "ede84c89-3e5a-4025-a8d4-8c66cfed1820", "created_at": "2018-05-27 22:54:55 +0900" } END JSON.parse(json) # => {"id"=>1, # "uuid"=>"ede84c89-3e5a-4025-a8d4-8c66cfed1820", # "created_at"=>"2018-05-27 22:54:55 +0900"} Hash object has String keys.
  2. Remove redundant duplication Hash#[]= with frozen string key, you can

    remove redundant duplication of string in internally
  3. Benchmark obj = [] 1000.times do |i| obj << {

    "id": i, "uuid": SecureRandom.uuid, "created_at": Time.now } end json = obj.to_json Benchmark.ips do |x| x.report("json") { JSON.parse(json) } x.report("yajl") { Yajl::Parser.parse(json) } x.report("oj") { Oj.load(json) } end
  4.      KTPO CFGPSF ZBKM KTPO BGUFS

    PK     VQ Before After
  5. 1. Remove redundant converting array = hash.keys array.each do |key|

    value = hash[key] ... end hash.each do |key, value| ... end Before After Here is pseudo code…
  6. 2. Add shortcut for converting to String for Hash key

    Before it always convert Object to String using #to_s via rb_funcall(). After It convert with appropriate CRuby-API if necessary.
  7. 3. Call CRuby-API instead of rb_funcall rb_funcall() is slightly heavy

    to call the Ruby method. It is better performance to use rb_str_encode() API instead.
  8. 4. Convert encoding only when needed If String object has

    only ASCII characters, it might not need to convert encoding.
  9. Benchmark obj = [] 1000.times do |i| obj << {

    "id" => i, :age => 42, :name => "Foo Bar Baz" } end Benchmark.ips do |x| x.report("json") { JSON.generate(obj) } x.report("yajl") { Yajl::Encoder.encode(obj, nil) } x.report("oj") { Oj.dump(obj) } end
  10.      KTPO CFGPSF ZBKM KTPO BGUFS

    PK     YVQ Before After