stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.54k stars 2.56k forks source link

Ticket #863 rewrite toString with StringBuilder for better performance #864

Closed Simulant87 closed 9 months ago

Simulant87 commented 9 months ago

Implementation for https://github.com/stleary/JSON-java/issues/863

Using a StringBuilder to improve the performance for the default toString methods without indent.

This duplicates a lot of code, as I assume you want to keep the existing public method interfaces accepting and returning a Writer for backwards compatibility. But is results in ~25% faster toString method, mostly used for serialisation.

I validated the performance with this project: https://github.com/fabienrenaud/java-json-benchmark ./run ser --libs ORGJSON

on my local machine result for the benchmark (thoughput measured in operations per second, higher is better):

currently latest library version 20240205:

Benchmark               Mode  Cnt       Score       Error  Units
Serialization.orgjson  thrpt   20  719375,045 ▒ 63677,634  ops/s

Improved implementation from this PR:

Benchmark               Mode  Cnt       Score       Error  Units
Serialization.orgjson  thrpt   20  905794,579 ▒ 23428,330  ops/s
Simulant87 commented 9 months ago

Closed in favour of https://github.com/stleary/JSON-java/pull/867 as it has comparable or even better performance with way less code changes and no code duplication.