Slide 1

Slide 1 text

*NQSPWF+40/ 1FSGPSNBODF Shizuo Fujita

Slide 2

Slide 2 text

Self @watson1978 Ubiregi Inc. Ruby committer

Slide 3

Slide 3 text

JSON.parse()

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

Hash freezes string key Duplicate and freeze the string if it isn’t frozen…

Slide 7

Slide 7 text

Remove redundant duplication Hash#[]= with frozen string key, you can remove redundant duplication of string in internally

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

KTPO CFGPSF ZBKM KTPO BGUFS PK VQ Before After

Slide 10

Slide 10 text

JSON.generate() (i.e. Object#to_json)

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

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…

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

4. Convert encoding only when needed If String object has only ASCII characters, it might not need to convert encoding.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

KTPO CFGPSF ZBKM KTPO BGUFS PK YVQ Before After

Slide 18

Slide 18 text

Slide 19

Slide 19 text

Not merged, No reaction Can you help me? https://github.com/flori/json/pull/345 https://github.com/flori/json/pull/346

Slide 20

Slide 20 text

Thank you !!!