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
public void setAll(@NotNull EntrySupplier supplier) {
int[] cache = sizeCache(maxSize());
// Constants
final int size = this.size;
final int dimensionMinus = dimension - 1;
final int dimensionBitCount = this.dimensionBitCount;
final int shiftedDimensionBitCount = dimensionBitCount << 1;
final int dimension = this.dimension;
// Fill cache with values
int fillValue = -1;
int count = 0;
for (int i = 0; i < size; i++) {
final int y = i >> shiftedDimensionBitCount;
final int z = (i >> (dimensionBitCount)) & dimensionMinus;
final int x = i & dimensionMinus;
int value = supplier.get(x, y, z);
if (fillValue != -2) {
if (fillValue == -1) {
fillValue = value;
} else if (fillValue != value) {
fillValue = -2;
int index = 0;
for (int y = 0; y < dimension; y++) {
for (int z = 0; z < dimension; z++) {
for (int x = 0; x < dimension; x++) {
int value = supplier.get(x, y, z);
// Support for fill fast exit if the supplier returns a constant value
if (fillValue != -2) {
if (fillValue == -1) {
fillValue = value;
} 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
if (fillValue < 0) {