Fix parallax issues

This commit is contained in:
Daniel Mills 2021-07-21 08:10:26 -04:00
parent c5262f188e
commit cde9dbe406
3 changed files with 72 additions and 126 deletions

View File

@ -22,7 +22,6 @@ import com.volmit.iris.Iris;
import com.volmit.iris.engine.hunk.Hunk;
import com.volmit.iris.engine.object.tile.TileData;
import com.volmit.iris.engine.parallel.BurstExecutor;
import com.volmit.iris.engine.parallel.GridLock;
import com.volmit.iris.engine.parallel.MultiBurst;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap;
@ -39,48 +38,13 @@ import org.bukkit.block.data.BlockData;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
// TODO: THIS IS SO SLOW
public class HunkRegionSlice<T> {
public static final Function2<Integer, CompoundTag, HunkRegionSlice<BlockData>> BLOCKDATA = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new BlockDataHunkIOAdapter(), c, "blockdata");
public static final Function2<Integer, CompoundTag, HunkRegionSlice<TileData<? extends TileState>>> TILE = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new TileDataHunkIOAdapter(), c, "tile");
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<String>> STRING = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new StringHunkIOAdapter(), c, t);
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<Boolean>> BOOLEAN = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new BooleanHunkIOAdapter(), c, t);
public static final Function2<Integer, CompoundTag, HunkRegionSlice<BlockData>> BLOCKDATA = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BlockDataHunkIOAdapter(), c, "blockdata");
public static final Function2<Integer, CompoundTag, HunkRegionSlice<TileData<? extends TileState>>> TILE = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new TileDataHunkIOAdapter(), c, "tile");
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<String>> STRING = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new StringHunkIOAdapter(), c, t);
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<Boolean>> BOOLEAN = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BooleanHunkIOAdapter(), c, t);
private final Function3<Integer, Integer, Integer, Hunk<T>> factory;
private final GridLock lock;
private final HunkIOAdapter<T> adapter;
private final CompoundTag compound;
private final String key;
@ -90,7 +54,6 @@ public class HunkRegionSlice<T> {
private final int height;
public HunkRegionSlice(int height, Function3<Integer, Integer, Integer, Hunk<T>> factory, HunkIOAdapter<T> adapter, CompoundTag compound, String key) {
this.lock = new GridLock(32, 32);
this.height = height;
this.loadedChunks = new KMap<>();
this.factory = factory;
@ -117,7 +80,9 @@ public class HunkRegionSlice<T> {
Long l = lastUse.get(i);
if (l == null || M.ms() - l > t) {
v++;
unload(i.getX(), i.getZ());
MultiBurst.burst.lazy(() -> {
unload(i.getX(), i.getZ());
});
}
}
@ -133,7 +98,6 @@ public class HunkRegionSlice<T> {
}
public synchronized void save(MultiBurst burst) {
BurstExecutor e = burst.burst();
try {
for (Position2 i : save.copy()) {
@ -141,16 +105,14 @@ public class HunkRegionSlice<T> {
continue;
}
e.queue(() -> save(i.getX(), i.getZ()));
save(i.getX(), i.getZ());
try {
lock.withNasty(i.getX(), i.getZ(), () -> save.remove(i));
save.remove(i);
} catch (Throwable eer) {
Iris.reportError(eer);
}
}
e.complete();
} catch (Throwable ee) {
Iris.reportError(ee);
}
@ -161,28 +123,23 @@ public class HunkRegionSlice<T> {
}
public void delete(int x, int z) {
lock.with(x, z, () -> compound.getValue().remove(key(x, z)));
compound.getValue().remove(key(x, z));
}
public Hunk<T> read(int x, int z) throws IOException {
AtomicReference<IOException> e = new AtomicReference<>();
Hunk<T> xt = lock.withResult(x, z, () -> {
Tag t = compound.getValue().get(key(x, z));
Hunk<T> xt = null;
if (!(t instanceof ByteArrayTag)) {
Iris.verbose("NOT BYTE ARRAY!");
return null;
}
Tag t = compound.getValue().get(key(x, z));
if ((t instanceof ByteArrayTag)) {
try {
return adapter.read(factory, (ByteArrayTag) t);
xt = adapter.read(factory, (ByteArrayTag) t);
} catch (IOException xe) {
Iris.reportError(xe);
e.set(xe);
}
return null;
});
}
if (xt != null) {
return xt;
@ -196,7 +153,7 @@ public class HunkRegionSlice<T> {
}
public void write(Hunk<T> hunk, int x, int z) throws IOException {
lock.withIO(x, z, () -> compound.getValue().put(key(x, z), hunk.writeByteArrayTag(adapter, key(x, z))));
compound.getValue().put(key(x, z), hunk.writeByteArrayTag(adapter, key(x, z)));
}
public synchronized int unloadAll() {
@ -214,7 +171,7 @@ public class HunkRegionSlice<T> {
public void save(Hunk<T> region, int x, int z) {
try {
lock.withIO(x, z, () -> write(region, x, z));
write(region, x, z);
} catch (IOException e) {
Iris.reportError(e);
e.printStackTrace();
@ -222,84 +179,74 @@ public class HunkRegionSlice<T> {
}
public boolean isLoaded(int x, int z) {
return lock.withResult(x, z, () -> loadedChunks.containsKey(new Position2(x, z)));
return loadedChunks.containsKey(new Position2(x, z));
}
public void save(int x, int z) {
lock.with(x, z, () -> {
if (isLoaded(x, z)) {
save(get(x, z), x, z);
}
});
if (isLoaded(x, z)) {
save(get(x, z), x, z);
}
}
public void unload(int x, int z) {
lock.with(x, z, () -> {
Position2 key = new Position2(x, z);
if (isLoaded(x, z)) {
if (save.contains(key)) {
save(x, z);
save.remove(key);
}
lastUse.remove(key);
loadedChunks.remove(key);
Position2 key = new Position2(x, z);
if (isLoaded(x, z)) {
if (save.contains(key)) {
save(x, z);
save.remove(key);
}
});
lastUse.remove(key);
loadedChunks.remove(key);
}
}
public Hunk<T> load(int x, int z) {
return lock.withResult(x, z, () -> {
if (isLoaded(x, z)) {
return loadedChunks.get(new Position2(x, z));
if (isLoaded(x, z)) {
return loadedChunks.get(new Position2(x, z));
}
Hunk<T> v = null;
if (contains(x, z)) {
try {
v = read(x, z);
} catch (IOException e) {
Iris.reportError(e);
e.printStackTrace();
}
}
Hunk<T> v = null;
if (v == null) {
v = factory.apply(16, height, 16);
}
if (contains(x, z)) {
try {
v = read(x, z);
} catch (IOException e) {
Iris.reportError(e);
e.printStackTrace();
}
}
loadedChunks.put(new Position2(x, z), v);
if (v == null) {
v = factory.apply(16, height, 16);
}
loadedChunks.put(new Position2(x, z), v);
return v;
});
return v;
}
public Hunk<T> get(int x, int z) {
return lock.withResult(x, z, () -> {
Position2 key = new Position2(x, z);
Position2 key = new Position2(x, z);
Hunk<T> c = loadedChunks.get(key);
Hunk<T> c = loadedChunks.get(key);
if (c == null) {
c = load(x, z);
}
if (c == null) {
c = load(x, z);
}
lastUse.put(new Position2(x, z), M.ms());
lastUse.put(new Position2(x, z), M.ms());
return c;
});
return c;
}
public Hunk<T> getR(int x, int z) {
return lock.withResult(x, z, () -> get(x, z).readOnly());
return get(x, z).readOnly();
}
public Hunk<T> getRW(int x, int z) {
return lock.withResult(x, z, () -> {
save.add(new Position2(x, z));
return get(x, z);
});
save.add(new Position2(x, z));
return get(x, z);
}
private String key(int x, int z) {

View File

@ -68,7 +68,7 @@ public class IrisObject extends IrisRegistrant {
private int w;
private int d;
private int h;
private transient final IrisLock readLock = new IrisLock("read-conclock").setDisabled(true);
private transient final IrisLock readLock = new IrisLock("read-conclock");
private transient BlockVector center;
private transient volatile boolean smartBored = false;
private transient IrisLock lock = new IrisLock("Preloadcache");

View File

@ -28,12 +28,14 @@ import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.documentation.ChunkCoordinates;
import com.volmit.iris.util.documentation.RegionCoordinates;
import com.volmit.iris.util.format.C;
import com.volmit.iris.util.plugin.Command;
import com.volmit.iris.util.scheduling.J;
import org.bukkit.block.TileState;
import org.bukkit.block.data.BlockData;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
@SuppressWarnings("ALL")
public class ParallaxWorld implements ParallaxAccess {
@ -234,25 +236,22 @@ public class ParallaxWorld implements ParallaxAccess {
}
@Override
public void cleanup(long r, long c) {
J.a(() -> {
try {
int rr = 0;
int cc = 0;
for (ParallaxRegion i : loadedRegions.v()) {
public synchronized void cleanup(long r, long c) {
try {
int rr = 0;
for (ParallaxRegion i : loadedRegions.v()) {
burst.lazy(() -> {
if (i.hasBeenIdleLongerThan(r)) {
rr++;
unload(i.getX(), i.getZ());
} else {
cc += i.cleanup(c);
i.cleanup(c);
}
}
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
});
}
});
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
}
}
@Override