<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.E-MailFormatvorlage17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=DE link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Dear Sirs,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>for performance reasons, hereby I like to propose the new public class java.io.CharSequenceReader. Before sharing a pull request, I'd kindly like to request for comments.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Since Java 1.1 we have the StringReader class. Since Java 1.4 we have the CharSequence class. StringBuilder, StringBuffer and CharBuffer are first-class implementations of it in the JDK, and there might exist third-party implementations of non-String character streams. Until today, however, we do not have a Reader for CharSequences, but need to go costly detours.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>To process non-String character streams, the typical detour today is to turn a CharSequence into a temporary String (hence duplicating its full contents), which needs time and memory (and eventually GC), for the sole sake of being processable by a StringReader. As StringReader is synchronized, each single access is synthetically slowed down. In many cases the synchronization has no use at all, as in real-world applications, least Readers are actually accessed concurrently. As a result, today the major benefit of StringBuilder over StringBuffer (being non-synchronized) vanishes as soon as a StringReader is used to access it. This means, "new StringReader(stringBuffer.toString());" imposes slower performance than essentially needed, in two ways: toString, synchronized.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>In an attempt to improve performance of this rather typical use case, I like to contribute a pull request providing the new public class java.io.CharSequenceReader. My idea is to mostly copy the existing code of StringReader, but wrap CharSequence instead of String; then strip synchronization; then add optimized access for the String, StringBuffer and StringBuilder implementations (in the sense of ::getChars(char[], int, int) to prevent a char-by-char loop in these cases). The idea mostly is covered by Apache Commons IO's CharSequenceReader, which nicely serves as a PoC: <a href="https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/input/CharSequenceReader.java">https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/input/CharSequenceReader.java</a>.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Alternatives:<o:p></o:p></p><p class=MsoNormal>- Applications could use Apache Commons IO's CharSequenceReader. As it is an open-source third-party dependency, some authors might not be allowed to use it, or may not want to carry this additional burden just for the sake of this single performance improvement. In addition, this library is not very actively maintained; its Java baseline still is Java 8. There is no commercial support.<o:p></o:p></p><p class=MsoNormal>- Applications could write their own Reader implementation. Given the assumption that this is a rather common use case, this imposes unjustified additional work for the authors of thousands of applications. It is hard to justify why there is a StringReader but not a CharSequenceReader.<o:p></o:p></p><p class=MsoNormal>- Instead of writing a new CharSequenceReader class we could slightly modify StringReader, so it accepts CharSequences (not only Strings). This does not remove the synchronization overhead unless we decide to remove the synchronization from StringReader's implementation, and it would be confusing / surprising (in the negative sense) that a class named "StringReader" actually is a "CharSequenceReader".<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Options:<o:p></o:p></p><p class=MsoNormal>- Instead of adding special cases for "String/StringBuilder/StringBuffer::getChars()", we could add "getChars(char[], int, int)" as a new default method to the CharSequence interface, essentially providing a char-by-char loop for all CharSequence implementations not already having an optimized getChars method. This makes the implementation of CharSequenceReader simpler, as not "switch over instanceof" is needed to benefit from optimized "getChars" implementations.<o:p></o:p></p><p class=MsoNormal>- Once we have CharSequenceReader, we could replace the full implementation of StringReader by synchronized calls to CharSequenceReader. This would reduce duplicate code.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Kindly requesting comments.<o:p></o:p></p><p class=MsoNormal>-Markus Karg<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>