回复:Eliminate unnecessary buffering and encoding conversion in BufferedWriter

wenshao shaojin.wensj at alibaba-inc.com
Mon Jun 30 07:25:55 UTC 2025


The BufferedWriterBench I added in PR 26022 can be configured to see the performance of different encodings.
We can run the following command to see the performance of different encodings on different content. The test numbers show that the performance of BufferedWriter::write(String) is improved by about 30%~70% in UTF8. The performance of non-UTF8 encodings is the same as before.
```sh
git remote add wenshao git at github.com:wenshao/jdk.git
git fetch wenshao
#baseline
git checkout 2758d6ad7767832db004d28f10cc764f33fa438e
make test TEST="micro:java.io.BufferedWriterBench" MICRO="OPTIONS=-p charset=UTF8,UTF16,GB18060"
# current
git checkout e1e9e25b3d2f99192fc2706dd7df846016452bae
make test TEST="micro:java.io.BufferedWriterBench" MICRO="OPTIONS=-p charset=UTF8,UTF16,GB18060"
```
------------------------------------------------------------------
发件人:Alan Bateman <alan.bateman at oracle.com>
发送时间:2025年6月30日(周一) 13:38
收件人:"温绍锦(高铁)"<shaojin.wensj at alibaba-inc.com>; "core-libs-dev"<core-libs-dev at openjdk.org>
主 题:Re: Eliminate unnecessary buffering and encoding conversion in BufferedWriter
On 29/06/2025 17:03, wenshao 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. 
LATIN1 (byte[]) -> UTF16 (char[]) -> UTF8 (byte[])
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.
 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.
 I see you've already proposed a PR. Most of this code goes back to JDK 1.4 so we need to be very careful, any changes will require a lot of Reviewer cycles.
 Have you surveyed the tests to ensure that there are good tests with different charsets and usage patterns? I think we need to be confidence in the tests before touching anything.
 -Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250630/bedfddb4/attachment.htm>


More information about the core-libs-dev mailing list