Add PaletteGetPresentBenchmark

This commit is contained in:
themode 2022-01-07 03:34:27 +01:00 committed by TheMode
parent 51795ccbd1
commit 29017c1d9b
2 changed files with 67 additions and 8 deletions

View File

@ -0,0 +1,44 @@
package net.minestom.jmh.palette;
import net.minestom.server.instance.palette.Palette;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class PaletteGetPresentBenchmark {
@Param({"0", "0.25", "0.5", "0.75", "1"})
public double fullness;
private Palette palette;
@Setup
public void setup() {
palette = Palette.blocks();
var random = ThreadLocalRandom.current();
final int dimension = palette.dimension();
for (int y = 0; y < dimension; y++)
for (int z = 0; z < dimension; z++)
for (int x = 0; x < dimension; x++)
if (random.nextDouble() < fullness)
palette.set(x, y, z, random.nextInt(1, 16));
}
@Benchmark
public void readAll(Blackhole blackHole) {
palette.getAll((x, y, z, value) -> blackHole.consume(value));
}
@Benchmark
public void readAllPresent(Blackhole blackHole) {
palette.getAllPresent((x, y, z, value) -> blackHole.consume(value));
}
}

View File

@ -95,10 +95,11 @@ final class PaletteImpl implements Palette, Cloneable {
getAllOptional(consumer, false);
}
void getAllOptional(@NotNull EntryConsumer consumer, boolean empty) {
void getAllOptional(@NotNull EntryConsumer consumer, boolean consumeEmpty) {
final long[] values = this.values;
final int dimension = this.dimension;
if (values.length == 0) {
if (empty) {
if (consumeEmpty) {
// No values, give all 0 to make the consumer happy
for (int y = 0; y < dimension; y++)
for (int z = 0; z < dimension; z++)
@ -119,16 +120,30 @@ final class PaletteImpl implements Palette, Cloneable {
final long value = values[i];
final int startIndex = i * valuesPerLong;
final int maxIndex = startIndex + valuesPerLong > size ? size - startIndex : valuesPerLong;
if (value == 0 && !empty) continue;
if (value == 0) {
// No values in this long, skip
if (consumeEmpty) {
for (int j = 0; j < maxIndex; j++) {
final int index = startIndex + j;
final int y = index >> shiftedDimensionBitCount;
final int z = index >> dimensionBitCount & dimensionMinus;
final int x = index & dimensionMinus;
consumer.accept(x, y, z, 0);
}
}
continue;
}
for (int j = 0; j < maxIndex; j++) {
final int index = startIndex + j;
final int y = index >> shiftedDimensionBitCount;
final int z = index >> dimensionBitCount & dimensionMinus;
final int x = index & dimensionMinus;
final int bitIndex = j * bitsPerEntry;
final short paletteIndex = (short) (value >> bitIndex & magicMask);
final int result = ids != null ? ids[paletteIndex] : paletteIndex;
if (result != 0 || empty) consumer.accept(x, y, z, result);
if (paletteIndex != 0 || consumeEmpty) {
final int result = ids != null ? ids[paletteIndex] : paletteIndex;
final int y = index >> shiftedDimensionBitCount;
final int z = index >> dimensionBitCount & dimensionMinus;
final int x = index & dimensionMinus;
consumer.accept(x, y, z, result);
}
}
}
}