brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

avoid allocations in attr_copyable for decent speedup #92

Closed breunigs closed 6 years ago

breunigs commented 6 years ago

The speedup mostly comes from moving repeated work out of the custom attribute reader into attr_copyable and only doing it once.

Timings:

# format: m:ss.milliseconds (from "time" real column)
1:27.66 1:24.10 1:25.08  # without patch
1:24.62 1:20.59 1:20.05  # just pulling out "need dup" check
1:00.02 0:59.08 0:56.66  # just pulling out :"@#{attr}"
1:24.98 1:24.95 1:22.78  # just replacing .dup with .class.new
0:54.67 0:56.95 0:55.93  # with patch (=all three)

Using Ruby 2.5 with bin/validate-schema schema.json data.json, so includes parsing.

Schema used: https://github.com/fge/sample-json-schemas/blob/master/json-patch/json-patch.json sample data:

[
 { "op": "test", "path": "/baz", "value": "qux" },
 { "op": "test", "path": "/foo/1", "value": 2 },
 # repeated to get an ~28 MB file
]

Moving [Array, Hash, Set] to the class and/or making it a Set/Hash yields no measurable improvement for this workload. It's also not faster to define different methods for the dup/non-dup cases.

Also, I'm not sure that class.new actually saves any time, since it's so close to the other result. I'd have to do more testing and proper statistics. I can change that back to .dup if desired.

brandur commented 6 years ago

Makes sense. Thanks!