8292698: Improve performance of DataInputStream
Reviewed-by: dfuchs
This commit is contained in:
parent
d3781ac8a3
commit
74a51ccc86
@ -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
|
||||
@ -57,8 +57,8 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
/**
|
||||
* 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
|
||||
@ -245,10 +245,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -268,10 +265,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -315,11 +309,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -340,6 +330,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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)
|
||||
@ -365,11 +356,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -390,6 +377,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
* @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();
|
||||
@ -399,7 +387,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
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}
|
||||
@ -474,7 +462,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
return Double.longBitsToDouble(readLong());
|
||||
}
|
||||
|
||||
private char lineBuffer[];
|
||||
private char[] lineBuffer;
|
||||
|
||||
/**
|
||||
* See the general contract of the {@code readLine}
|
||||
@ -505,7 +493,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||
*/
|
||||
@Deprecated
|
||||
public final String readLine() throws IOException {
|
||||
char buf[] = lineBuffer;
|
||||
char[] buf = lineBuffer;
|
||||
|
||||
if (buf == null) {
|
||||
buf = lineBuffer = new char[128];
|
||||
@ -634,7 +622,7 @@ loop: while (true) {
|
||||
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);
|
||||
@ -647,8 +635,8 @@ loop: while (true) {
|
||||
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));
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user