Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8215422

Optimize PrintStream.println methods

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • None
    • core-libs
    • None
    • behavioral
    • low
    • The risk is that implementations exist that override some of the print methods, expecting them to be invoked when calling println. This would bypass those overridden implementations.
    • Java API

      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.

            redestad Claes Redestad
            redestad Claes Redestad
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: