JDK-8356679 : Using CharSequence::getChars internally
  • Type: Enhancement
  • Component: core-libs
  • Affected Version: 25
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2025-05-10
  • Updated: 2025-05-27
Related Reports
Relates :  
Sub Tasks
JDK-8357183 :  
Description
Recently OpenJDK adopted the new method CharSequence::getChars(int, int, char[], int) for inclusion in Java 25. As a bulk reader method, it allows potentially improved efficiency over the previously available char-by-char reader method CharSequence::charAt(int).

Chen Liang suggested on March 23rd on the core-lib-dev mailing list to use the new method within the internal source code of OpenJDK for the implementation of Appendables (see https://mail.openjdk.org/pipermail/core-libs-dev/2025-March/141521.html ). The idea behind this is that the implementations might be more efficient then.

A quick analysis of the OpenJDK source code identified (at least) the following classes which could potentially run more efficient when using CharSequence::getChars internally, thanks to bulk reading and / or prevention of internal copies / toString() conversions:
* java.io.Writer
* java.io.StringWriter
* java.io.PrintWriter
* java.io.BufferedWriter
* java.io.CharArrayWriter
* java.io.FileWriter
* java.io.OutputStreamWriter
* sun.nio.cs.StreamEncoder
* java.io.PrintStream
* java.nio.CharBuffer

In the sense of "eat your own dog food", it makes sense to implement Chen's idea in (at least) those classes. Possibly more classes could get identified when taking a deeper look. Besides the potential efficiency improvements, it would be a good show case for the usage of the new API.

The risk of this change should be low, as test coverage exists, and as the intended changes are solely internal to the implementation. No API will get changed. In some cases the JavaDocs will get slightly adapted where it currently exposes the actual implementation (to not lie in future).
Comments
Email discussion: https://mail.openjdk.org/pipermail/core-libs-dev/2025-May/145508.html
27-05-2025

To simplify discussion and review, splitting this JBS into several ones, each with its own PR.
17-05-2025

[~liach] Kindly asking for your consent on the core-libs mailing list for me filing a PR. I already prepared a proposed implementation in https://github.com/mkarg/jdk/commits/apply-charsequence-getchars/, so we could chime into a detailed code discussion on Github as soon as I have the official permission to file a PR by gaining "broad acceptance" on the mailing list (as per the latest Developer's Guide / Alan's request). Unfortunately my email to core-libs is currently withhold for approval by moderator.
11-05-2025