Skip to content

Commit

Permalink
8254078: DataOutputStream is very slow post-disabling of Biased Locking
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, shade, alanb
  • Loading branch information
Andrew Haley committed Nov 9, 2020
1 parent 79b7909 commit 17f04fc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/java.base/share/classes/java/io/DataInputStream.java
Expand Up @@ -33,9 +33,10 @@
* way. An application uses a data output stream to write data that
* can later be read by a data input stream.
* <p>
* DataInputStream is not necessarily safe for multithreaded access.
* Thread safety is optional and is the responsibility of users of
* methods in this class.
* A DataInputStream is not safe for use by multiple concurrent
* threads. If a DataInputStream is to be used by more than one
* thread then access to the data input stream should be controlled
* by appropriate synchronization.
*
* @author Arthur van Hoff
* @see java.io.DataOutputStream
Expand Down
24 changes: 16 additions & 8 deletions src/java.base/share/classes/java/io/DataOutputStream.java
Expand Up @@ -29,6 +29,11 @@
* A data output stream lets an application write primitive Java data
* types to an output stream in a portable way. An application can
* then use a data input stream to read the data back in.
* <p>
* A DataOutputStream is not safe for use by multiple concurrent
* threads. If a DataOutputStream is to be used by more than one
* thread then access to the data output stream should be controlled
* by appropriate synchronization.
*
* @see java.io.DataInputStream
* @since 1.0
Expand Down Expand Up @@ -162,8 +167,9 @@ public final void writeByte(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeShort(int v) throws IOException {
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
writeBuffer[0] = (byte)(v >>> 8);
writeBuffer[1] = (byte)(v >>> 0);
out.write(writeBuffer, 0, 2);
incCount(2);
}

Expand All @@ -177,8 +183,9 @@ public final void writeShort(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeChar(int v) throws IOException {
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
writeBuffer[0] = (byte)(v >>> 8);
writeBuffer[1] = (byte)(v >>> 0);
out.write(writeBuffer, 0, 2);
incCount(2);
}

Expand All @@ -192,10 +199,11 @@ public final void writeChar(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeInt(int v) throws IOException {
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
writeBuffer[0] = (byte)(v >>> 24);
writeBuffer[1] = (byte)(v >>> 16);
writeBuffer[2] = (byte)(v >>> 8);
writeBuffer[3] = (byte)(v >>> 0);
out.write(writeBuffer, 0, 4);
incCount(4);
}

Expand Down
123 changes: 123 additions & 0 deletions test/micro/org/openjdk/bench/java/io/DataOutputStreamTest.java
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2020, Red Hat Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package org.openjdk.bench.java.io;

import org.openjdk.jmh.annotations.*;

import java.io.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 1, warmups = 0)
@Measurement(iterations = 6, time = 1)
@Warmup(iterations=2, time = 2)
@State(Scope.Benchmark)
public class DataOutputStreamTest {

public enum BasicType {CHAR, SHORT, INT, STRING}
@Param({"CHAR", "SHORT", "INT", /* "STRING"*/}) BasicType basicType;

@Param({"4096"}) int size;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size);
File f;
String outputString;
FileOutputStream fileOutputStream;
DataOutput bufferedFileStream, rawFileStream, byteArrayStream;

@Setup(Level.Trial)
public void setup() throws Exception {
f = File.createTempFile("DataOutputStreamTest","out");
fileOutputStream = new FileOutputStream(f);
byteArrayStream = new DataOutputStream(byteArrayOutputStream);
rawFileStream = new DataOutputStream(fileOutputStream);
bufferedFileStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream));
outputString = new String(new byte[size]);
}

public void writeChars(DataOutput dataOutput)
throws Exception {
for (int i = 0; i < size; i += 2) {
dataOutput.writeChar(i);
}
}

public void writeShorts(DataOutput dataOutput)
throws Exception {
for (int i = 0; i < size; i += 2) {
dataOutput.writeShort(i);
}
}

public void writeInts(DataOutput dataOutput)
throws Exception {
for (int i = 0; i < size; i += 4) {
dataOutput.writeInt(i);
}
}

public void writeString(DataOutput dataOutput)
throws Exception {
dataOutput.writeChars(outputString);
}

public void write(DataOutput dataOutput)
throws Exception {
switch (basicType) {
case CHAR:
writeChars(dataOutput);
break;
case SHORT:
writeShorts(dataOutput);
break;
case INT:
writeInts(dataOutput);
break;
case STRING:
writeString(dataOutput);
break;
}
}

@Benchmark
public void dataOutputStreamOverByteArray() throws Exception {
byteArrayOutputStream.reset();
write(byteArrayStream);
byteArrayOutputStream.flush();
}

@Benchmark
public void dataOutputStreamOverRawFileStream() throws Exception {
fileOutputStream.getChannel().position(0);
write(rawFileStream);
fileOutputStream.flush();
}

@Benchmark
public void dataOutputStreamOverBufferedFileStream() throws Exception{
fileOutputStream.getChannel().position(0);
write(bufferedFileStream);
fileOutputStream.flush();
}
}

1 comment on commit 17f04fc

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.