This sub task of JDK-8356679 proposes implementation-only changes to java.io.Writer. No API is changed. Implementation behavior is slightly changed w.r.t to self-calls to non-private methods.
The target is to make the Writer::append(CharSequence) and Writer::append(CharSequence, int, int) methods *in the non-String* case be as efficient as they are already *in the String* case.
This is achived by now handling CharSequence in *the exact same* optimized way as String was specifically handled before. This generalization of the previously String-specific optimization is possible since Java 25, thanks to the new CharSequence::getChars(int, int, char[], int) bulk-read method.
Prior to the proposed change, the code was unefficient, as ontop of what the String-case did, the text content of other CharSequences (like StringBuilder and CharBuffer) had to be copied *once or multiply*, before it was finally processed *as* a String.
The changes in detail are:
* Extract the original implementation of Writer::write(String, int, int) to become a newly introduced, internal method Writer::implWrite(CharSequence, int, int). This allows making use of that exact original implementation by other methods, particularly CharSequence::append, but potentially also sub classes in the same package (for subsequent enhancement of specific Writers).
* Writer::append(CharSequence) prevents the previously implied creation of an intermediate String representation in the non-String case (which implied creating another temporary object on the heap, and duplicating full text content), ontop of what the String case did
* Writer::append(CharSequence, int, int), in addition to the optimization of the single-arg case, also prevents the previously implied creation of a sub sequence (which always meant to create another temporary object on the heap, and in many cases meant to duplicate partial text content).
* As a benefit, the JavaDocs of these methods can be simplified thanks to the reduced number of self-calls. While this changes the internal behavior slightly, it is finally clarifying that these JavaDoc sections were originally meant as explanatory notes what *the default code* does, but not as as mandatory specs what *subclasses* have to do.
This work is foundational to subsequent enhancements of specific Writers, as the new implWrite(CharSequence, int, int) method is to be called by them *instead* of the previously called write(String, int, int) method, to allow for the same efficiency optimiziation now found in the new default code. Due to that, no other Writers are changed in this first step; these Writers will follow in subsequent JBS / PRs.
The target is to make the Writer::append(CharSequence) and Writer::append(CharSequence, int, int) methods *in the non-String* case be as efficient as they are already *in the String* case.
This is achived by now handling CharSequence in *the exact same* optimized way as String was specifically handled before. This generalization of the previously String-specific optimization is possible since Java 25, thanks to the new CharSequence::getChars(int, int, char[], int) bulk-read method.
Prior to the proposed change, the code was unefficient, as ontop of what the String-case did, the text content of other CharSequences (like StringBuilder and CharBuffer) had to be copied *once or multiply*, before it was finally processed *as* a String.
The changes in detail are:
* Extract the original implementation of Writer::write(String, int, int) to become a newly introduced, internal method Writer::implWrite(CharSequence, int, int). This allows making use of that exact original implementation by other methods, particularly CharSequence::append, but potentially also sub classes in the same package (for subsequent enhancement of specific Writers).
* Writer::append(CharSequence) prevents the previously implied creation of an intermediate String representation in the non-String case (which implied creating another temporary object on the heap, and duplicating full text content), ontop of what the String case did
* Writer::append(CharSequence, int, int), in addition to the optimization of the single-arg case, also prevents the previously implied creation of a sub sequence (which always meant to create another temporary object on the heap, and in many cases meant to duplicate partial text content).
* As a benefit, the JavaDocs of these methods can be simplified thanks to the reduced number of self-calls. While this changes the internal behavior slightly, it is finally clarifying that these JavaDoc sections were originally meant as explanatory notes what *the default code* does, but not as as mandatory specs what *subclasses* have to do.
This work is foundational to subsequent enhancements of specific Writers, as the new implWrite(CharSequence, int, int) method is to be called by them *instead* of the previously called write(String, int, int) method, to allow for the same efficiency optimiziation now found in the new default code. Due to that, no other Writers are changed in this first step; these Writers will follow in subsequent JBS / PRs.