diff --git a/src/main/java/com/volmit/iris/engine/hunk/io/HunkRegionSlice.java b/src/main/java/com/volmit/iris/engine/hunk/io/HunkRegionSlice.java index 08c2fac94..7d1632d5f 100644 --- a/src/main/java/com/volmit/iris/engine/hunk/io/HunkRegionSlice.java +++ b/src/main/java/com/volmit/iris/engine/hunk/io/HunkRegionSlice.java @@ -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 { - public static final Function2> BLOCKDATA = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new BlockDataHunkIOAdapter(), c, "blockdata"); - public static final Function2>> TILE = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new TileDataHunkIOAdapter(), c, "tile"); - public static final Function3> STRING = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new StringHunkIOAdapter(), c, t); - public static final Function3> BOOLEAN = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunkSynced, new BooleanHunkIOAdapter(), c, t); + public static final Function2> BLOCKDATA = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BlockDataHunkIOAdapter(), c, "blockdata"); + public static final Function2>> TILE = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new TileDataHunkIOAdapter(), c, "tile"); + public static final Function3> STRING = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new StringHunkIOAdapter(), c, t); + public static final Function3> BOOLEAN = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BooleanHunkIOAdapter(), c, t); private final Function3> factory; - private final GridLock lock; private final HunkIOAdapter adapter; private final CompoundTag compound; private final String key; @@ -90,7 +54,6 @@ public class HunkRegionSlice { private final int height; public HunkRegionSlice(int height, Function3> factory, HunkIOAdapter 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 { 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 { } public synchronized void save(MultiBurst burst) { - BurstExecutor e = burst.burst(); try { for (Position2 i : save.copy()) { @@ -141,16 +105,14 @@ public class HunkRegionSlice { 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 { } 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 read(int x, int z) throws IOException { AtomicReference e = new AtomicReference<>(); - Hunk xt = lock.withResult(x, z, () -> { - Tag t = compound.getValue().get(key(x, z)); + Hunk 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 { } public void write(Hunk 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 { public void save(Hunk 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 { } 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 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 v = null; + + if (contains(x, z)) { + try { + v = read(x, z); + } catch (IOException e) { + Iris.reportError(e); + e.printStackTrace(); } + } - Hunk 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 get(int x, int z) { - return lock.withResult(x, z, () -> { - Position2 key = new Position2(x, z); + Position2 key = new Position2(x, z); - Hunk c = loadedChunks.get(key); + Hunk 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 getR(int x, int z) { - return lock.withResult(x, z, () -> get(x, z).readOnly()); + return get(x, z).readOnly(); } public Hunk 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) { diff --git a/src/main/java/com/volmit/iris/engine/object/IrisObject.java b/src/main/java/com/volmit/iris/engine/object/IrisObject.java index aa02db2ee..717747793 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisObject.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisObject.java @@ -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"); diff --git a/src/main/java/com/volmit/iris/engine/parallax/ParallaxWorld.java b/src/main/java/com/volmit/iris/engine/parallax/ParallaxWorld.java index ef6afe72b..c0e6065c2 100644 --- a/src/main/java/com/volmit/iris/engine/parallax/ParallaxWorld.java +++ b/src/main/java/com/volmit/iris/engine/parallax/ParallaxWorld.java @@ -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