Summary
Reimplementing the PrintStream.println methods to not actually call print() enables a relatively large optimization for certain use cases.
Problem
The println(String/int/...) methods are specified to behave "as though" they invoke print(String/int/...) and then println(). In practice they actually do invoke print(String/int/...). but not println()
Example:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
By implementing an internal write method that doesn't invoke print() we would avoid nested synchronization, multiple flushes and scanning the String for newlines, resulting in a sizeable speed-up for some use cases.
Not calling print would be an observable behavioral change for subclasses, however, and there might be implementations out there that override print, expecting println to call it.
Solution
Assess compatibility risk and accept the change.
There are already several cases where methods in PrintStream define their behavior "as though" they invoke some method, but then don't.
An alternative would be to evaluate at construction time whether the PrintStream is overridden and if not, use an optimized implementation. This adds code complexity and minimal runtime overhead.
Specification
No specification changes.
- csr of
-
JDK-8215412 Optimize PrintStream.println methods
-
- Resolved
-