benweidig opened a new pull request, #67: URL: https://github.com/apache/tapestry-5/pull/67
# Overview `JSONObject`/`JSONArray's toString()` and `toCompactString()` are slower than necessary due to synchronization overhead and per-String allocation in the print path. My sandbox implementation at https://github.com/benweidig/tapestry-json-improvements (non-breaking, verified byte-for-byte against current output) shows a ~4-6x speedup across payload sizes and both compact/pretty formatting, with no public API changes. # Changes * Deprecated `JSONStringer` and inlined its escaping loop directly into `JSONObject`. There is some code duplication, but it keeps the call sites monomorphic and should allow for better C2 optimization in the JVM. * Hex escaping is done with manual nibble extraction pls a lookup-table instead of `String.format` calls * Droped `CharArrayWriter`/`PrintWriter` and build directlty into `StringBuilder` * Pre-size output buffers # Results Graphs: [JMH Visalizer](https://jmh.morethan.io/?source=https://gist.githubusercontent.com/benweidig/e8d8424bec5c63823f3e8fc5782d4bc8/raw/7bb0009526b004e2459af8152a54850fec83dfa5/results.json) ## End-to-end cost: `JSONSerializationBenchmark` (`toString()` / `toCompactString()`) | Payload size | compact: baseline (µs/op) | compact: improved (µs/op) | speedup | pretty: baseline (µs/op) | pretty: improved (µs/op) | speedup | | -----------: | ------------------------: | ------------------------: | -------: | -----------------------: | -----------------------: | -------: | | 10 | 15.10 ± 0.34 | 3.57 ± 0.27 | **4.2×** | 33.30 ± 1.03 | 6.16 ± 0.07 | **5.4×** | | 100 | 186.83 ± 22.62 | 33.14 ± 0.52 | **5.6×** | 349.52 ± 4.62 | 57.95 ± 0.38 | **6.0×** | | 1000 | 1777.78 ± 34.98 | 388.31 ± 4.70 | **4.6×** | 3469.37 ± 120.15 | 629.63 ± 17.88 | **5.5×** | ## Isolated escaping cost: `StringQuotingBenchmark` (`JSONObject.quote(String)`) | Profile | Baseline (ns/op) | Improved (ns/op) | Speedup | | ------------------ | ----------------: | ----------------: | -------: | | PLAIN_ASCII | 175.74 | 81.08 | **2.2×** | | QUOTES_AND_SLASHES | 168.55 | 82.93 | **2.0×** | | CONTROL_CHARS | 182.26 | 80.45 | **2.3×** | | UNICODE_HEAVY | 611.93 | 105.65 | **5.8×** | -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
