xeipuuv / gojsonschema

An implementation of JSON Schema, draft v4 v6 & v7 - Go language
2.56k stars 358 forks source link

Relatively poor validation performance #226

Open UltraNemesis opened 5 years ago

UltraNemesis commented 5 years ago

In the quest for a highly performant json-schema library, I have done a performance comparison with another package github.com/santhosh-tekuri/jsonschema found that gojsonschema is about 3.5 times slower compared to it.

Test involved creating a schema instance before hand and then a routine performs 500 validations on a hard coded JSON string in a loop and 20,000 go-routines are spawned from this for a total of 500 x 20000 = 10 million validations. gojsonschema took 3m18.0692147s compared to 56.6310405s of the later.

I cannot share the schema file itself as its company property, but it basically contains a few if-then-else blocks and in each, property presence, data type and content validations are done.

johandorland commented 5 years ago

I must admit performance is not a big focus at the moment and I'm sure there are a few places where validation performance could be improved. For 10 million evaluations the time it takes doesn't seem that outrageous to me, so I'm not too concerned about that initially.

It is surprising though that it is 3.5x slower than the santhosh-tekuri/jsonschema library. I am a bit familiar with that library and generally it works in the same way as this one, so I don't see why there should be any large differences in performance.

Could you rerun your tests using the Go profiler and upload the images of the profile reports here? That would provide useful information in order to spot where the hotspots are in the code compared to santhosh-tekuri/jsonschema. Right now I can't say whether gojsonschema just happens to have some specific checks which are implemented poorly that your schema uses or if there are some more fundamental parts of the validation code that could use a speedup.