Open mraleph opened 4 months ago
@mraleph Do you see any potential performance improvements from merging the underlying regular UTF8 automaton with an automaton that lexes JSON and using that as the lexer for the JsonDecoder (to have a Utf8JsonDecoder) so that no fusing is needed?
The fusing creates a UTF-8 specialized JSON decoder, _JsonUtf8Decoder
.
Thank you for the hint @lrhn. It looks to me like the specialized UTF8-JSON decoder is using a table-driven UTF8 decoder and a non-table-driven JSON parser.
What I meant to say in this comment was, since the language of UTF8 appears to be regular, and the lexical structure of JSON seems to be regular too, it feels like there might be performance gains to be had by merging both automata into one and having the tokenization phase be based on a single automaton that does UTF8 decoding and JSON lexing at the same time.
I think the UTF-8 is a red herring here. A table based tokenizer would work equally well for both UTF-8 and string based JSON parsing, since everything outside of string values is ASCII anyway, and what's inside strings need to be interpreted if it contains escapes -- or non-ASCII characters in a UTF-8 source.
The reason the current implementation doesn't use a table based tokenizer is that it doesn't need to tokenize at all. The moment it sees the first character of a value, it can start interpreting it. It tries to collect the value of a number and the contents of a string while scanning, in one pass, so it doesn't have to first tokenize, and then interpret the token afterwards.
Maybe that process can be made more efficient, but doing an extra tokenization step first doesn't sound like an improvement. (Unless it can be done very efficiently using SIMD operations or using parallelism, or something.)
The current decoder generates maps that take a lot of memory. Decoding
[{"x":1,"y":2},{"x":3,"y":4}]
creates fresh strings for the repeated "x"
(and "y"
), and these strings are inserted into similar Maps that have their own index.
There is a cost to using a fresh string as a key. Other than memory footprint, the hashCode
needs to be computed for each instance. Perhaps this cost could be better used to identify a common index to be shared by similar JSON objects (marked as copy-on-write if used more than once).
Maps and strings can probably be optimized. Large JSON inputs trendy to have repetitions of the same map structure. Canonicalized map keys, maybe even some short values, can avoid doing as many allocations, and if it's possible to reuse the map structure, then there might be a saving there too. The cost of that is an overhead while using, for recognizing those existing values and structures. I have tried it before, and the overhead was visible.
It's much more efficient to build a custom parser for the known structures using a more pull-based JSON interpreter, than to try to recognize possibly repeated structures at parse time in a generic parser.
(That is: if you care about performance, why are you building maps and lists at all?)
The current decoder generates maps that take a lot of memory.
I have a prototype patch which canonicalizes keys when decoding JSON (this helps both with memory consumption and improves access speed as well, because accessing maps with literal keys can short-circuit string comparison later). You still need to compute hashCode
though.
I entertained the idea of using "predictive" decoding, where decoding an array of JSON objects makes an assumption about repeated shape and uses that to short-circuit both key decoding and Map
hashing and maybe even use specialized COW Map
implementation which allows sharing of the "index" part. This is what V8 JSON decoder effectively does. But I am not sure I want to go there - I feel the need to keep JSON decoder complexity down. My thinking here is aligned with @lrhn's: if code is doing bytes -> JSON objects (
Map's) -> proper Dart objects
then it is better to actually do bytes -> proper Dart objects
.
A pull-based JSON interpreter for going straight from bytes to proper Dart objects sounds great. Previous issue threads for that:
See also https://github.com/kevmoo/json_alt – although this code is VERY old
See also https://github.com/simc/crimson
Maybe we should look also at how other languages have dealt with this? I feel that if we only look at how js and dart solutions have being built so far, we might miss some clever solutions that might exist for other languages.
We sometimes hear complaints that our JSON decoder is slow compared to, for example, V8's. Usually we just shrug these complaints away saying that:
Developers are then encouraged to use high performance native JSON decoders if JSON decoding is a bottleneck for them.
This does not square very well with my belief that Dart should be considered a general purpose high-level programming language. So I would like to take a closer look at JSON's decoder performance and see if it can be considerably improved.
I have taken JSON files from https://github.com/simdjson/simdjson-data/tree/master/jsonexamples and run them through JS and Dart benchmarks.
Dart benchmark is measuring performance of JSON decoder fused with UTF8 decoder:
JS benchmark is measuring performance of decoding JSON from pre-decoded JS string as well as performance of coverting UTF8 bytes to a JS string:
decodeUtf8
is a helper function which I added tod8
shell which effectively does the following:String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, buffer_size)
.This was done to measure the cost of utf8 -> utf16 convertion which will otherwise be hidden.
The baseline measurement for V8 on my desktop looks like this:
Dart in AOT mode yields:
With the exceptions of
numbers.json
Dart's decoder is 1.5-3x slower.Good news is that it is not impossible to significantly improve its performance - I have done some experiments and got to the following preliminary numbers:
Which are significantly better.
I am going to use this issue as an umbrella issue to land my changes.