8325340: Add ASCII fast-path to Data-/ObjectInputStream.readUTF

Reviewed-by: rgiulietti, bpb, rriggs
This commit is contained in:
Claes Redestad 2024-02-26 16:05:18 +00:00
parent 3780ad3133
commit 9a9cfbe0ba
4 changed files with 362 additions and 24 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, 2022, Red Hat Inc. All rights reserved.
* Copyright (c) 2020, Red Hat Inc. All rights reserved.
* Copyright (c) 2024, 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
@ -27,7 +28,10 @@ import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@ -38,22 +42,85 @@ import java.util.concurrent.TimeUnit;
@Warmup(iterations = 2, time = 2)
@State(Scope.Thread)
public class DataInputStreamTest {
private final int size = 1024;
private static final int SIZE = 1024;
private ByteArrayInputStream bais;
private ByteArrayInputStream utfDataAsciiMixed;
private ByteArrayInputStream utfDataMixed;
private ByteArrayInputStream utfDataAsciiSmall;
private ByteArrayInputStream utfDataSmall;
private ByteArrayInputStream utfDataAsciiLarge;
private ByteArrayInputStream utfDataLarge;
private static final int REPEATS = 20;
@Setup(Level.Iteration)
public void setup() {
byte[] bytes = new byte[size];
public void setup() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException {
byte[] bytes = new byte[SIZE];
ThreadLocalRandom.current().nextBytes(bytes);
bais = new ByteArrayInputStream(bytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("small");
dataOut.writeUTF("slightly longer string that is more likely to trigger use of simd intrinsics");
}
dataOut.flush();
utfDataAsciiMixed = new ByteArrayInputStream(baos.toByteArray());
baos = new ByteArrayOutputStream();
dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("slightly longer string that is more likely to trigger use of simd intrinsics");
dataOut.writeUTF("slightly longer string that is more likely to trigger use of simd intrinsics");
}
dataOut.flush();
utfDataAsciiLarge = new ByteArrayInputStream(baos.toByteArray());
baos = new ByteArrayOutputStream();
dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("smol");
dataOut.writeUTF("smally");
}
dataOut.flush();
utfDataAsciiSmall = new ByteArrayInputStream(baos.toByteArray());
baos = new ByteArrayOutputStream();
dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("sm\u00FFll");
dataOut.writeUTF("slightly longer string th\u01F3t is more likely to trigger use of simd intrinsics");
}
dataOut.flush();
utfDataMixed = new ByteArrayInputStream(baos.toByteArray());
baos = new ByteArrayOutputStream();
dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("sm\u00F3l");
dataOut.writeUTF("small\u0132");
}
dataOut.flush();
utfDataSmall = new ByteArrayInputStream(baos.toByteArray());
baos = new ByteArrayOutputStream();
dataOut = new DataOutputStream(baos);
for (int i = 0; i < REPEATS; i++) {
dataOut.writeUTF("slightly longer string that is more likely to trigg\u0131r use of simd intrinsics");
dataOut.writeUTF("slightly longer string th\u0131t is more likely to trigger use of simd intrinsics");
}
dataOut.flush();
utfDataLarge = new ByteArrayInputStream(baos.toByteArray());
}
@Benchmark
public void readChar(Blackhole bh) throws Exception {
bais.reset();
DataInputStream dis = new DataInputStream(bais);
for (int i = 0; i < size / 2; i++) {
for (int i = 0; i < SIZE / 2; i++) {
bh.consume(dis.readChar());
}
}
@ -62,8 +129,68 @@ public class DataInputStreamTest {
public void readInt(Blackhole bh) throws Exception {
bais.reset();
DataInputStream dis = new DataInputStream(bais);
for (int i = 0; i < size / 4; i++) {
for (int i = 0; i < SIZE / 4; i++) {
bh.consume(dis.readInt());
}
}
@Benchmark
public void readUTFAsciiMixed(Blackhole bh) throws Exception {
utfDataAsciiMixed.reset();
DataInputStream dis = new DataInputStream(utfDataAsciiMixed);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
@Benchmark
public void readUTFAsciiSmall(Blackhole bh) throws Exception {
utfDataAsciiSmall.reset();
DataInputStream dis = new DataInputStream(utfDataAsciiSmall);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
@Benchmark
public void readUTFAsciiLarge(Blackhole bh) throws Exception {
utfDataAsciiLarge.reset();
DataInputStream dis = new DataInputStream(utfDataAsciiLarge);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
@Benchmark
public void readUTFMixed(Blackhole bh) throws Exception {
utfDataMixed.reset();
DataInputStream dis = new DataInputStream(utfDataMixed);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
@Benchmark
public void readUTFSmall(Blackhole bh) throws Exception {
utfDataSmall.reset();
DataInputStream dis = new DataInputStream(utfDataSmall);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
@Benchmark
public void readUTFLarge(Blackhole bh) throws Exception {
utfDataLarge.reset();
DataInputStream dis = new DataInputStream(utfDataLarge);
for (int i = 0; i < REPEATS; i++) {
bh.consume(dis.readUTF());
bh.consume(dis.readUTF());
}
}
}