RFR: 8361018: Re-examine buffering and encoding conversion in BufferedWriter [v5]
Brett Okken
duke at openjdk.org
Tue Jul 1 00:01:21 UTC 2025
On Mon, 30 Jun 2025 23:56:53 GMT, Shaojin Wen <swen at openjdk.org> wrote:
>> BufferedWriter -> OutputStreamWriter -> StreamEncoder
>>
>> In this call chain, BufferedWriter has a char[] buffer, and StreamEncoder has a ByteBuffer. There are two layers of cache here, or the BufferedWriter layer can be removed. And when charset is UTF8, if the content of write(String) is LATIN1, a conversion from LATIN1 to UTF16 and then to LATIN1 will occur here.
>>
>> LATIN1 -> UTF16 -> UTF8
>>
>> We can improve BufferedWriter. When the parameter Writer instanceof OutputStreamWriter is passed in, remove the cache and call it directly. In addition, improve write(String) in StreamEncoder to avoid unnecessary encoding conversion.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
>
> BufferedWriter buffer use StringBuilder
What about a benchmark which exercises the actual benefits of buffering?
So many very small writes to a backing OutputStream which is (artificially) slow.
Perhaps have an OutputStream which sleeps 1ms on every call.
The write out more than 8k of data in chunks of less than 8 chars at a time.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/26022#issuecomment-3021204155
More information about the nio-dev
mailing list