Skip to content

Commit

Permalink
8215412: Optimize PrintStream.println methods
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, dfuchs, forax
  • Loading branch information
cl4es committed Jan 4, 2019
1 parent c3eb2e9 commit 3460182
Showing 1 changed file with 116 additions and 30 deletions.
146 changes: 116 additions & 30 deletions src/java.base/share/classes/java/io/PrintStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public void write(byte buf[], int off, int len) {
* stream occur as promptly as with the original PrintStream.
*/

private void write(char buf[]) {
private void write(char[] buf) {
try {
synchronized (this) {
ensureOpen();
Expand All @@ -584,10 +584,34 @@ private void write(char buf[]) {
charOut.flushBuffer();
if (autoFlush) {
for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n')
if (buf[i] == '\n') {
out.flush();
break;
}
}
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
}

// Used to optimize away back-to-back flushing and synchronization when
// using println, but since subclasses could exist which depend on
// observing a call to print followed by newLine() we only use this if
// getClass() == PrintStream.class to avoid compatibility issues.
private void writeln(char[] buf) {
try {
synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
Expand Down Expand Up @@ -616,6 +640,30 @@ private void write(String s) {
}
}

// Used to optimize away back-to-back flushing and synchronization when
// using println, but since subclasses could exist which depend on
// observing a call to print followed by newLine we only use this if
// getClass() == PrintStream.class to avoid compatibility issues.
private void writeln(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}

private void newLine() {
try {
synchronized (this) {
Expand Down Expand Up @@ -780,9 +828,13 @@ public void println() {
* @param x The {@code boolean} to be printed
*/
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -794,9 +846,13 @@ public void println(boolean x) {
* @param x The {@code char} to be printed.
*/
public void println(char x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -808,9 +864,13 @@ public void println(char x) {
* @param x The {@code int} to be printed.
*/
public void println(int x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -822,9 +882,13 @@ public void println(int x) {
* @param x a The {@code long} to be printed.
*/
public void println(long x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -836,9 +900,13 @@ public void println(long x) {
* @param x The {@code float} to be printed.
*/
public void println(float x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -850,9 +918,13 @@ public void println(float x) {
* @param x The {@code double} to be printed.
*/
public void println(double x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -863,10 +935,14 @@ public void println(double x) {
*
* @param x an array of chars to print.
*/
public void println(char x[]) {
synchronized (this) {
print(x);
newLine();
public void println(char[] x) {
if (getClass() == PrintStream.class) {
writeln(x);
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -878,9 +954,13 @@ public void println(char x[]) {
* @param x The {@code String} to be printed.
*/
public void println(String x) {
synchronized (this) {
print(x);
newLine();
if (getClass() == PrintStream.class) {
writeln(String.valueOf(x));
} else {
synchronized (this) {
print(x);
newLine();
}
}
}

Expand All @@ -895,9 +975,15 @@ public void println(String x) {
*/
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
if (getClass() == PrintStream.class) {
// need to apply String.valueOf again since first invocation
// might return null
writeln(String.valueOf(s));
} else {
synchronized (this) {
print(s);
newLine();
}
}
}

Expand Down

0 comments on commit 3460182

Please sign in to comment.