Data json specializes on `PrintWriter` when writing

Description

Since all writing fns involved in writing json expects a PrinterWriter to write to, we need to wrap our StringWriter in a PrinterWriter before emitting the strings. None of the fns involved in writing use any of the functionality provided by PrinterWriter which cannot simply be swapped for fns in Writer.

Approach:

Use writer instead of PrinterWriter

Benchmarks:

With code from applied

10b

100b

1k

10k

100k

631.871809 ns

2.575072 µs

13.343666 µs

182.061063 µs

1.720438 ms

Patch:

0001-DJSON-35-Use-writer-instead-of-printerwriter.patch

 

Approach2:

Use Appendable instead of PrinterWriter. This allows for using a StringBuilder directly, since both Writer and StringBuilder implements Appendable

Benchmarks:

With code from applied

10b

100b

1k

10k

100k

634.199728 ns

2.679949 µs

13.087295 µs

176.296328 µs

1.682320 ms

Patch:

0001-DJSON-35-Use-Appendable-instead-of-printerwriter.patch

Environment

None

Activity

Show:
Alex Miller
March 19, 2021, 4:08 AM

Applied Appendable patch (after updating to apply cleanly)

Alex Miller
March 12, 2021, 7:06 PM

Seems like there are two somewhat separate questions here:

1. what interface should write- methods take? (currently PrintWriter, could be Writer or Appendable)
2. what type should you construct and pass into these methods

For #1, just generally doing the most generic thing you can do seems best, shouldn't affect perf, so Appendable seems strictly better here.

For #2, not sure I understand the tradeoffs, seems like using StringBuilder directly is preferable? In both cases, have you looked at setting the initial buffer size to something bigger?

Fixed

Assignee

Alex Miller

Reporter

Erik Assum