Skip to content

Commit

Permalink
8292698: Improve performance of DataInputStream
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs
  • Loading branch information
stsypanov authored and jaikiran committed Oct 17, 2022
1 parent d3781ac commit 74a51cc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/java.base/share/classes/java/io/DataInputStream.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. 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
Expand Down Expand Up @@ -57,8 +57,8 @@ public DataInputStream(InputStream in) {
/**
* working arrays initialized on demand by readUTF
*/
private byte bytearr[] = new byte[80];
private char chararr[] = new char[80];
private byte[] bytearr = new byte[80];
private char[] chararr = new char[80];

/**
* Reads some number of bytes from the contained input stream and
Expand Down Expand Up @@ -245,10 +245,7 @@ public final int skipBytes(int n) throws IOException {
* @see java.io.FilterInputStream#in
*/
public final boolean readBoolean() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (ch != 0);
return readUnsignedByte() != 0;
}

/**
Expand All @@ -268,10 +265,7 @@ public final boolean readBoolean() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final byte readByte() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (byte)(ch);
return (byte) readUnsignedByte();
}

/**
Expand Down Expand Up @@ -315,11 +309,7 @@ public final int readUnsignedByte() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final short readShort() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (short)((ch1 << 8) + (ch2 << 0));
return (short) readUnsignedShort();
}

/**
Expand All @@ -340,6 +330,7 @@ public final short readShort() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final int readUnsignedShort() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
Expand All @@ -365,11 +356,7 @@ public final int readUnsignedShort() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final char readChar() throws IOException {
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (char)((ch1 << 8) + (ch2 << 0));
return (char) readUnsignedShort();
}

/**
Expand All @@ -390,6 +377,7 @@ public final char readChar() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final int readInt() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
Expand All @@ -399,7 +387,7 @@ public final int readInt() throws IOException {
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

private byte readBuffer[] = new byte[8];
private final byte[] readBuffer = new byte[8];

/**
* See the general contract of the {@code readLong}
Expand Down Expand Up @@ -474,7 +462,7 @@ public final double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}

private char lineBuffer[];
private char[] lineBuffer;

/**
* See the general contract of the {@code readLine}
Expand Down Expand Up @@ -505,7 +493,7 @@ public final double readDouble() throws IOException {
*/
@Deprecated
public final String readLine() throws IOException {
char buf[] = lineBuffer;
char[] buf = lineBuffer;

if (buf == null) {
buf = lineBuffer = new char[128];
Expand Down Expand Up @@ -634,7 +622,7 @@ public static final String readUTF(DataInput in) throws IOException {
if (count > utflen)
throw new UTFDataFormatException(
"malformed input: partial character at end");
char2 = (int) bytearr[count-1];
char2 = bytearr[count-1];
if ((char2 & 0xC0) != 0x80)
throw new UTFDataFormatException(
"malformed input around byte " + count);
Expand All @@ -647,8 +635,8 @@ public static final String readUTF(DataInput in) throws IOException {
if (count > utflen)
throw new UTFDataFormatException(
"malformed input: partial character at end");
char2 = (int) bytearr[count-2];
char3 = (int) bytearr[count-1];
char2 = bytearr[count-2];
char3 = bytearr[count-1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new UTFDataFormatException(
"malformed input around byte " + (count-1));
Expand Down
69 changes: 69 additions & 0 deletions test/micro/org/openjdk/bench/java/io/DataInputStreamTest.java
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, 2022, 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 micro.org.openjdk.bench.java.io;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 4, warmups = 0)
@Measurement(iterations = 5, time = 1)
@Warmup(iterations = 2, time = 2)
@State(Scope.Benchmark)
public class DataInputStreamTest {
private final int size = 1024;

private ByteArrayInputStream bais;

@Setup(Level.Iteration)
public void setup() {
byte[] bytes = new byte[size];
ThreadLocalRandom.current().nextBytes(bytes);
bais = new ByteArrayInputStream(bytes);
}

@Benchmark
public void readChar(Blackhole bh) throws Exception {
bais.reset();
DataInputStream dis = new DataInputStream(bais);
for (int i = 0; i < size / 2; i++) {
bh.consume(dis.readChar());
}
}

@Benchmark
public void readInt(Blackhole bh) throws Exception {
bais.reset();
DataInputStream dis = new DataInputStream(bais);
for (int i = 0; i < size / 4; i++) {
bh.consume(dis.readInt());
}
}
}

0 comments on commit 74a51cc

Please sign in to comment.