ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
736 stars 185 forks source link

Improve performance of writing JsonObject and JsonArray #70

Closed matrixx567 closed 8 years ago

matrixx567 commented 8 years ago

Hello Ralf,

I improved the performance of writing JsonObjects and JsonArray a little bit. Therefore I handle the first element of the array before the loop and this allows to remove the IF statement within the WHILE-loop.

BR Martin

bernardosulzbach commented 8 years ago

Spacing is inconsistent (compare 459 and 462).

There is no benchmarking, just a promise. After fixing the spacing, the change seems worth merging, as I would also say that this is the right way of doing it. But at least a microbenchmarking is expected if you are going to claim a performance improvement.

sbandara commented 8 years ago

Interesting! Did you get a chance to measure how much this increases throughput?

matrixx567 commented 8 years ago

I executed the benchmarks on my machine this morning and here are the results:

minimal-json-benchmark1 minimal-json-benchmark2 minimal-json-benchmark3

matrixx567 commented 8 years ago

Another slight improvement to the original code of the Array's and Object's write method is to put the first=false; statement into the IF statement. So it will be only called once for the loop.

bernardosulzbach commented 8 years ago

Another slight improvement to the original code of the Array's and Object's write method is to put the first=false; statement into the IF statement.

@matrixx567 Sorry, do we even need it? What you proposed for void write(JsonWriter) seems better.

ralfstx commented 8 years ago

Thanks for the change @matrixx567! Performance aside, the code is clearer and better.

Fixed the formatting in https://github.com/ralfstx/minimal-json/commit/0ff90d9a73630495f26522aa81e047004362c73c

bernardosulzbach commented 8 years ago

Performance aside, the code is clearer and better.

I agree.

Fixed the formatting in https://github.com/ralfstx/minimal-json/commit/0ff90d9a73630495f26522aa81e047004362c73c

Thank you for also caring about this.