RFR: 8312164: Refactor Arrays.hashCode for long, boolean, double, float, and Object arrays [v3]
Glavo
duke at openjdk.org
Thu Jul 20 00:37:55 UTC 2023
On Wed, 19 Jul 2023 13:14:23 GMT, Pavel Rappo <prappo at openjdk.org> wrote:
>> Please review this PR to refactor Arrays.hashCode for long[], boolean[], and Object[]. I've been told elsewhere that it shouldn't have significant performance implications.
>
> Pavel Rappo has updated the pull request incrementally with one additional commit since the last revision:
>
> Fix hashCode(float[])
I ran a JMH benchmark for this PR:
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 5, time = 2)
@Fork(value = 1, jvmArgsAppend = {"-XX:+UseG1GC", "-Xms8g", "-Xmx8g"})
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class HashCode {
@Param({"1", "10", "100", "10000"})
private int size;
private long[] longs;
private boolean[] booleans;
private float[] floats;
private double[] doubles;
private Object[] objects;
@Setup
public void setup() {
longs = new long[size];
booleans = new boolean[size];
floats = new float[size];
doubles = new double[size];
objects = new Object[size];
Random rnd = new Random(0);
for (int i = 0; i < size; i++) {
long next = rnd.nextLong();
longs[i] = next;
booleans[i] = next % 2 == 0;
floats[i] = Float.intBitsToFloat((int) next);
doubles[i] = Double.longBitsToDouble(next);
objects[i] = (int) next;
}
}
@Benchmark
public int longs() {
return Arrays.hashCode(longs);
}
@Benchmark
public int booleans() {
return Arrays.hashCode(booleans);
}
@Benchmark
public int floats() {
return Arrays.hashCode(floats);
}
@Benchmark
public int doubles() {
return Arrays.hashCode(doubles);
}
@Benchmark
public int objects() {
return Arrays.hashCode(objects);
}
}
Result:
(This PR) (Baseline)
Benchmark (size) Mode Cnt Score Error Units Score Error Units
HashCode.booleans 1 thrpt 5 472358.594 ± 5037.260 ops/ms 473356.084 ± 1780.600 ops/ms
HashCode.booleans 10 thrpt 5 118457.276 ± 848.886 ops/ms 118470.818 ± 342.023 ops/ms
HashCode.booleans 100 thrpt 5 13140.346 ± 46.869 ops/ms 13153.643 ± 1.226 ops/ms
HashCode.booleans 10000 thrpt 5 133.090 ± 0.027 ops/ms 133.041 ± 0.032 ops/ms
HashCode.doubles 1 thrpt 5 438869.947 ± 1806.730 ops/ms 441611.989 ± 692.622 ops/ms
HashCode.doubles 10 thrpt 5 76418.134 ± 42.834 ops/ms 76432.141 ± 368.953 ops/ms
HashCode.doubles 100 thrpt 5 7364.920 ± 6.941 ops/ms 7339.776 ± 28.448 ops/ms
HashCode.doubles 10000 thrpt 5 76.689 ± 0.037 ops/ms 76.787 ± 0.188 ops/ms
HashCode.floats 1 thrpt 5 443741.068 ± 1773.736 ops/ms 444085.364 ± 557.545 ops/ms
HashCode.floats 10 thrpt 5 86047.878 ± 306.709 ops/ms 86306.681 ± 908.752 ops/ms
HashCode.floats 100 thrpt 5 8263.785 ± 37.091 ops/ms 8254.055 ± 3.427 ops/ms
HashCode.floats 10000 thrpt 5 86.146 ± 0.018 ops/ms 86.121 ± 0.022 ops/ms
HashCode.longs 1 thrpt 5 462198.590 ± 27224.002 ops/ms 460673.957 ± 26979.630 ops/ms
HashCode.longs 10 thrpt 5 124744.207 ± 262.268 ops/ms 130367.753 ± 257.766 ops/ms
HashCode.longs 100 thrpt 5 13552.607 ± 2.545 ops/ms 13454.362 ± 8.991 ops/ms
HashCode.longs 10000 thrpt 5 133.122 ± 0.038 ops/ms 133.114 ± 0.016 ops/ms
HashCode.objects 1 thrpt 5 230303.967 ± 919.637 ops/ms 647597.213 ± 2652.002 ops/ms
HashCode.objects 10 thrpt 5 25724.486 ± 111.422 ops/ms 121222.501 ± 270.073 ops/ms
HashCode.objects 100 thrpt 5 2497.018 ± 6.201 ops/ms 11889.717 ± 264.730 ops/ms
HashCode.objects 10000 thrpt 5 27.673 ± 0.012 ops/ms 130.903 ± 0.714 ops/ms
I think there is a performance issue with this PR. For object arrays, the performance is reduced by about 80%.
(I got the result reversed in a previous comment, so I hid it.)
-------------
PR Review: https://git.openjdk.org/jdk/pull/14900#pullrequestreview-1538245804
More information about the core-libs-dev
mailing list