mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 17:11:58 +01:00
Use bytes whenever possible
This commit is contained in:
parent
476b697a8f
commit
4512cef7d9
@ -12,18 +12,14 @@ import java.util.function.IntUnaryOperator;
|
||||
* Palette that switches between its backend based on the use case.
|
||||
*/
|
||||
final class AdaptivePalette implements Palette {
|
||||
final int dimension;
|
||||
final int maxBitsPerEntry;
|
||||
final int defaultBitsPerEntry;
|
||||
|
||||
final byte dimension, defaultBitsPerEntry, maxBitsPerEntry;
|
||||
SpecializedPalette palette;
|
||||
|
||||
AdaptivePalette(int dimension, int maxBitsPerEntry, int bitsPerEntry) {
|
||||
AdaptivePalette(byte dimension, byte maxBitsPerEntry, byte bitsPerEntry) {
|
||||
validateDimension(dimension);
|
||||
this.dimension = dimension;
|
||||
this.maxBitsPerEntry = maxBitsPerEntry;
|
||||
this.defaultBitsPerEntry = bitsPerEntry;
|
||||
|
||||
this.palette = new FilledPalette(dimension, 0);
|
||||
}
|
||||
|
||||
@ -132,7 +128,7 @@ final class AdaptivePalette implements Palette {
|
||||
return new FilledPalette(dimension, entries.iterator().nextInt());
|
||||
} else if (currentBitsPerEntry > defaultBitsPerEntry &&
|
||||
(bitsPerEntry = MathUtils.bitsToRepresent(entries.size() - 1)) < currentBitsPerEntry) {
|
||||
flexiblePalette.resize(bitsPerEntry);
|
||||
flexiblePalette.resize((byte) bitsPerEntry);
|
||||
return flexiblePalette;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* Palette containing a single value. Useful for both empty and full palettes.
|
||||
*/
|
||||
record FilledPalette(int dimension, int value) implements SpecializedPalette.Immutable {
|
||||
record FilledPalette(byte dim, int value) implements SpecializedPalette.Immutable {
|
||||
@Override
|
||||
public int get(int x, int y, int z) {
|
||||
return value;
|
||||
@ -14,10 +14,11 @@ record FilledPalette(int dimension, int value) implements SpecializedPalette.Imm
|
||||
|
||||
@Override
|
||||
public void getAll(@NotNull EntryConsumer consumer) {
|
||||
final int dimension = dimension();
|
||||
for (int y = 0; y < dimension; y++)
|
||||
for (int z = 0; z < dimension; z++)
|
||||
for (int x = 0; x < dimension; x++)
|
||||
final byte dimension = this.dim;
|
||||
final int value = this.value;
|
||||
for (byte y = 0; y < dimension; y++)
|
||||
for (byte z = 0; z < dimension; z++)
|
||||
for (byte x = 0; x < dimension; x++)
|
||||
consumer.accept(x, y, z, value);
|
||||
}
|
||||
|
||||
@ -31,6 +32,11 @@ record FilledPalette(int dimension, int value) implements SpecializedPalette.Imm
|
||||
return value != 0 ? maxSize() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int dimension() {
|
||||
return dim;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull SpecializedPalette clone() {
|
||||
return this;
|
||||
|
@ -31,7 +31,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
||||
|
||||
// Specific to this palette type
|
||||
private final AdaptivePalette adaptivePalette;
|
||||
private int bitsPerEntry;
|
||||
private byte bitsPerEntry;
|
||||
private int count;
|
||||
|
||||
private long[] values;
|
||||
@ -40,7 +40,7 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
||||
// value = palette index
|
||||
private Int2IntOpenHashMap valueToPaletteMap;
|
||||
|
||||
FlexiblePalette(AdaptivePalette adaptivePalette, int bitsPerEntry) {
|
||||
FlexiblePalette(AdaptivePalette adaptivePalette, byte bitsPerEntry) {
|
||||
this.adaptivePalette = adaptivePalette;
|
||||
|
||||
this.bitsPerEntry = bitsPerEntry;
|
||||
@ -235,17 +235,13 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeByte((byte) bitsPerEntry);
|
||||
writer.writeByte(bitsPerEntry);
|
||||
if (bitsPerEntry <= maxBitsPerEntry()) { // Palette index
|
||||
writer.writeVarIntList(paletteToValueList, BinaryWriter::writeVarInt);
|
||||
}
|
||||
writer.writeLongArray(values);
|
||||
}
|
||||
|
||||
private int fixBitsPerEntry(int bitsPerEntry) {
|
||||
return bitsPerEntry > maxBitsPerEntry() ? 15 : bitsPerEntry;
|
||||
}
|
||||
|
||||
private void retrieveAll(@NotNull EntryConsumer consumer, boolean consumeEmpty) {
|
||||
final long[] values = this.values;
|
||||
final int dimension = this.dimension();
|
||||
@ -309,8 +305,9 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
||||
}
|
||||
}
|
||||
|
||||
void resize(int newBitsPerEntry) {
|
||||
FlexiblePalette palette = new FlexiblePalette(adaptivePalette, fixBitsPerEntry(newBitsPerEntry));
|
||||
void resize(byte newBitsPerEntry) {
|
||||
newBitsPerEntry = newBitsPerEntry > maxBitsPerEntry() ? 15 : newBitsPerEntry;
|
||||
FlexiblePalette palette = new FlexiblePalette(adaptivePalette, newBitsPerEntry);
|
||||
palette.paletteToValueList = paletteToValueList;
|
||||
palette.valueToPaletteMap = valueToPaletteMap;
|
||||
getAll(palette::set);
|
||||
@ -322,9 +319,10 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable {
|
||||
private int getPaletteIndex(int value) {
|
||||
if (!hasPalette()) return value;
|
||||
final int lastPaletteIndex = this.paletteToValueList.size();
|
||||
if (lastPaletteIndex >= maxPaletteSize(bitsPerEntry)) {
|
||||
final byte bpe = this.bitsPerEntry;
|
||||
if (lastPaletteIndex >= maxPaletteSize(bpe)) {
|
||||
// Palette is full, must resize
|
||||
resize(bitsPerEntry + 1);
|
||||
resize((byte) (bpe + 1));
|
||||
return getPaletteIndex(value);
|
||||
}
|
||||
final int lookup = valueToPaletteMap.putIfAbsent(value, lastPaletteIndex);
|
||||
|
@ -20,7 +20,7 @@ public interface Palette extends Writeable {
|
||||
}
|
||||
|
||||
static Palette newPalette(int dimension, int maxBitsPerEntry, int bitsPerEntry) {
|
||||
return new AdaptivePalette(dimension, maxBitsPerEntry, bitsPerEntry);
|
||||
return new AdaptivePalette((byte) dimension, (byte) maxBitsPerEntry, (byte) bitsPerEntry);
|
||||
}
|
||||
|
||||
int get(int x, int y, int z);
|
||||
|
Loading…
Reference in New Issue
Block a user