Smarter setAll loop

This commit is contained in:
themode 2022-01-08 07:18:29 +01:00 committed by TheMode
parent b821619a91
commit 42dc264c93

View File

@ -171,32 +171,31 @@ final class PaletteImpl implements Palette, Cloneable {
@Override @Override
public void setAll(@NotNull EntrySupplier supplier) { public void setAll(@NotNull EntrySupplier supplier) {
int[] cache = sizeCache(maxSize()); int[] cache = sizeCache(maxSize());
// Constants final int dimension = this.dimension;
final int size = this.size;
final int dimensionMinus = dimension - 1;
final int dimensionBitCount = this.dimensionBitCount;
final int shiftedDimensionBitCount = dimensionBitCount << 1;
// Fill cache with values // Fill cache with values
int fillValue = -1; int fillValue = -1;
int count = 0; int count = 0;
for (int i = 0; i < size; i++) { int index = 0;
final int y = i >> shiftedDimensionBitCount; for (int y = 0; y < dimension; y++) {
final int z = (i >> (dimensionBitCount)) & dimensionMinus; for (int z = 0; z < dimension; z++) {
final int x = i & dimensionMinus; for (int x = 0; x < dimension; x++) {
int value = supplier.get(x, y, z); int value = supplier.get(x, y, z);
if (fillValue != -2) { // Support for fill fast exit if the supplier returns a constant value
if (fillValue == -1) { if (fillValue != -2) {
fillValue = value; if (fillValue == -1) {
} else if (fillValue != value) { fillValue = value;
fillValue = -2; } else if (fillValue != value) {
fillValue = -2;
}
}
// Set value in cache
if (value != 0) {
value = getPaletteIndex(value);
count++;
}
cache[index++] = value;
} }
} }
if (value != 0) {
value = getPaletteIndex(value);
count++;
}
cache[i] = value;
} }
// Update palette content // Update palette content
if (fillValue < 0) { if (fillValue < 0) {