9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-19 15:09:18 +00:00

cleanup palette and fix incorrect bit resizing

This commit is contained in:
Julian Krings
2025-08-24 18:39:39 +02:00
parent 4702534f9c
commit 67b29cc363
4 changed files with 51 additions and 63 deletions

View File

@@ -105,7 +105,7 @@ nmsBindings.forEach { key, value ->
pluginJars(tasks.jar.flatMap { it.archiveFile }) pluginJars(tasks.jar.flatMap { it.archiveFile })
javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(jvmVersion.getOrDefault(key, 21))} javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(jvmVersion.getOrDefault(key, 21))}
runDirectory.convention(layout.buildDirectory.dir("run/$key")) runDirectory.convention(layout.buildDirectory.dir("run/$key"))
systemProperty("disable.watchdog", "") systemProperty("disable.watchdog", "true")
systemProperty("net.kyori.ansi.colorLevel", color) systemProperty("net.kyori.ansi.colorLevel", color)
systemProperty("com.mojang.eula.agree", true) systemProperty("com.mojang.eula.agree", true)
systemProperty("iris.suppressReporting", !errorReporting) systemProperty("iris.suppressReporting", !errorReporting)

View File

@@ -141,11 +141,13 @@ public class DataContainer<T> {
} }
public void writeDos(DataOutputStream dos) throws IOException { public void writeDos(DataOutputStream dos) throws IOException {
Varint.writeUnsignedVarInt(length, dos); synchronized (this) {
Varint.writeUnsignedVarInt(palette.get().size(), dos); Varint.writeUnsignedVarInt(length, dos);
palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data)); Varint.writeUnsignedVarInt(palette.get().size(), dos);
data.get().write(dos); palette.get().iterateIO((data, __) -> writer.writeNodeData(dos, data));
dos.flush(); data.get().write(dos);
dos.flush();
}
} }
private Palette<T> newPalette(DataInputStream din) throws IOException { private Palette<T> newPalette(DataInputStream din) throws IOException {
@@ -163,51 +165,41 @@ public class DataContainer<T> {
return new HashPalette<>(); return new HashPalette<>();
} }
public void ensurePaletted(T t) {
if (palette.get().id(t) == -1) {
expandOne();
}
}
public void set(int position, T t) { public void set(int position, T t) {
synchronized (this) { synchronized (this) {
int id = palette.get().id(t); int id = palette.get().id(t);
if (id == -1) { if (id == -1) {
expandOne();
id = palette.get().add(t); id = palette.get().add(t);
updateBits();
} }
data.get().set(position, id); data.get().set(position, id);
} }
} }
private void expandOne() { private void updateBits() {
if (palette.get().size() + 1 >= BIT[bits.get()]) { if (palette.get().bits() == bits.get())
setBits(bits.get() + 1); return;
int bits = palette.get().bits();
if (this.bits.get() <= LINEAR_BITS_LIMIT != bits <= LINEAR_BITS_LIMIT) {
palette.updateAndGet(p -> newPalette(bits).from(p));
} }
data.updateAndGet(d -> d.setBits(bits));
this.bits.set(bits);
} }
public T get(int position) { public T get(int position) {
synchronized (this) { synchronized (this) {
int id = data.get().get(position) + 1; int id = data.get().get(position);
if (id <= 0) { if (id <= 0) {
return null; return null;
} }
return palette.get().get(id - 1); return palette.get().get(id);
}
}
public void setBits(int bits) {
if (this.bits.get() != bits) {
if (this.bits.get() <= LINEAR_BITS_LIMIT != bits <= LINEAR_BITS_LIMIT) {
palette.set(newPalette(bits).from(palette.get()));
}
this.bits.set(bits);
data.set(data.get().setBits(bits));
} }
} }

View File

@@ -23,24 +23,22 @@ import com.volmit.iris.util.function.Consumer2;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class HashPalette<T> implements Palette<T> { public class HashPalette<T> implements Palette<T> {
private final ReentrantLock lock = new ReentrantLock();
private final LinkedHashMap<T, Integer> palette; private final LinkedHashMap<T, Integer> palette;
private final KMap<Integer, T> lookup; private final KMap<Integer, T> lookup;
private final AtomicInteger size; private final AtomicInteger size;
public HashPalette() { public HashPalette() {
this.size = new AtomicInteger(0); this.size = new AtomicInteger(1);
this.palette = new LinkedHashMap<>(); this.palette = new LinkedHashMap<>();
this.lookup = new KMap<>(); this.lookup = new KMap<>();
add(null); palette.put(null, 0);
} }
@Override @Override
public T get(int id) { public T get(int id) {
if (id < 0 || id >= size.get()) { if (id <= 0 || id >= size.get()) {
return null; return null;
} }
@@ -49,17 +47,16 @@ public class HashPalette<T> implements Palette<T> {
@Override @Override
public int add(T t) { public int add(T t) {
lock.lock(); if (t == null) {
try { return 0;
int index = size.getAndIncrement(); }
palette.put(t, index);
if (t != null) { synchronized (palette) {
return palette.computeIfAbsent(t, $ -> {
int index = size.getAndIncrement();
lookup.put(index, t); lookup.put(index, t);
} return index;
return index; });
} finally {
lock.unlock();
} }
} }
@@ -80,8 +77,7 @@ public class HashPalette<T> implements Palette<T> {
@Override @Override
public void iterate(Consumer2<T, Integer> c) { public void iterate(Consumer2<T, Integer> c) {
lock.lock(); synchronized (palette) {
try {
for (T i : palette.keySet()) { for (T i : palette.keySet()) {
if (i == null) { if (i == null) {
continue; continue;
@@ -89,8 +85,6 @@ public class HashPalette<T> implements Palette<T> {
c.accept(i, id(i)); c.accept(i, id(i));
} }
} finally {
lock.unlock();
} }
} }
} }

View File

@@ -21,17 +21,16 @@ package com.volmit.iris.util.hunk.bits;
import com.volmit.iris.util.function.Consumer2; import com.volmit.iris.util.function.Consumer2;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
public class LinearPalette<T> implements Palette<T> { public class LinearPalette<T> implements Palette<T> {
private final AtomicReference<AtomicReferenceArray<T>> palette; private volatile AtomicReferenceArray<T> palette;
private final AtomicInteger size; private final AtomicInteger size;
public LinearPalette(int initialSize) { public LinearPalette(int initialSize) {
this.size = new AtomicInteger(0); this.size = new AtomicInteger(1);
this.palette = new AtomicReference<>(new AtomicReferenceArray<>(initialSize)); this.palette = new AtomicReferenceArray<>(initialSize);
palette.get().set(size.getAndIncrement(), null); palette.set(0, null);
} }
@Override @Override
@@ -40,26 +39,29 @@ public class LinearPalette<T> implements Palette<T> {
return null; return null;
} }
return palette.get().get(id); return palette.get(id);
} }
@Override @Override
public int add(T t) { public int add(T t) {
if (t == null) {
return 0;
}
int index = size.getAndIncrement(); int index = size.getAndIncrement();
grow(index + 1); grow(index + 1);
palette.get().set(index, t); palette.set(index, t);
return index; return index;
} }
private void grow(int newLength) { private synchronized void grow(int newLength) {
if (newLength > palette.get().length()) { if (newLength > palette.length()) {
AtomicReferenceArray<T> a = new AtomicReferenceArray<>(newLength + size.get()); AtomicReferenceArray<T> a = new AtomicReferenceArray<>(newLength);
for (int i = 0; i < palette.get().length(); i++) { for (int i = 0; i < palette.length(); i++) {
a.set(i, palette.get().get(i)); a.set(i, palette.get(i));
} }
palette.set(a); palette = a;
} }
} }
@@ -69,8 +71,8 @@ public class LinearPalette<T> implements Palette<T> {
return 0; return 0;
} }
for (int i = 1; i < size() + 1; i++) { for (int i = 1; i < size.get(); i++) {
if (t.equals(palette.get().get(i))) { if (t.equals(palette.get(i))) {
return i; return i;
} }
} }
@@ -86,7 +88,7 @@ public class LinearPalette<T> implements Palette<T> {
@Override @Override
public void iterate(Consumer2<T, Integer> c) { public void iterate(Consumer2<T, Integer> c) {
for (int i = 1; i < size() + 1; i++) { for (int i = 1; i < size() + 1; i++) {
c.accept(palette.get().get(i), i); c.accept(palette.get(i), i);
} }
} }
} }