From 6c67192fe2c75128dd84df3b9816d3071b61dad9 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 13 Apr 2018 19:49:03 +1000 Subject: [PATCH] VirtualWorld cache/optimizations --- .../com/boydti/fawe/object/FaweLimit.java | 2 + .../object/schematic/visualizer/SchemVis.java | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/object/FaweLimit.java b/core/src/main/java/com/boydti/fawe/object/FaweLimit.java index 389c681b..21584e15 100644 --- a/core/src/main/java/com/boydti/fawe/object/FaweLimit.java +++ b/core/src/main/java/com/boydti/fawe/object/FaweLimit.java @@ -17,6 +17,7 @@ public class FaweLimit { public int SPEED_REDUCTION = Integer.MAX_VALUE; public boolean FAST_PLACEMENT = false; public boolean CONFIRM_LARGE = true; +// public boolean[] STRIP_NBT = null; public static FaweLimit MAX; @@ -65,6 +66,7 @@ public class FaweLimit { MAX.MAX_EXPRESSION_MS = 50; MAX.FAST_PLACEMENT = true; MAX.CONFIRM_LARGE = true; +// MAX.STRIP_NBT = null; } public boolean MAX_CHANGES() { diff --git a/core/src/main/java/com/boydti/fawe/object/schematic/visualizer/SchemVis.java b/core/src/main/java/com/boydti/fawe/object/schematic/visualizer/SchemVis.java index 75fabb28..6e379456 100644 --- a/core/src/main/java/com/boydti/fawe/object/schematic/visualizer/SchemVis.java +++ b/core/src/main/java/com/boydti/fawe/object/schematic/visualizer/SchemVis.java @@ -41,6 +41,8 @@ import java.util.*; * An Immutable virtual world used to display & select schematics */ public class SchemVis extends ImmutableVirtualWorld { + private static final WeakHashMap DIMENSION_CACHE = new WeakHashMap<>(); + private final WorldData worldData; private final Long2ObjectOpenHashMap> files; @@ -260,28 +262,33 @@ public class SchemVis extends ImmutableVirtualWorld { * @param schemDimensions * @return */ - private BlockVector2D registerAndGetChunkOffset(BlockVector schemDimensions, File file) { + private BlockVector2D registerAndGetChunkOffset(BlockVector2D schemDimensions, File file) { int chunkX = schemDimensions.getBlockX() >> 4; int chunkZ = schemDimensions.getBlockZ() >> 4; MutableBlockVector2D pos2 = new MutableBlockVector2D(); MutableBlockVector2D curPos = lastPos; // Find next free position while (!isAreaFree(curPos, pos2.setComponents(curPos.getBlockX() + chunkX, curPos.getBlockZ() + chunkZ))) { - if (curPos == lastPos && !files.containsKey(MathMan.pairInt(curPos.getBlockX(), curPos.getBlockZ()))) { - curPos = new MutableBlockVector2D(); - curPos.setComponents(lastPos.getBlockX(), lastPos.getBlockZ()); - } +// if (curPos == lastPos && !files.containsKey(MathMan.pairInt(curPos.getBlockX(), curPos.getBlockZ()))) { +// curPos = new MutableBlockVector2D(); +// curPos.setComponents(lastPos.getBlockX(), lastPos.getBlockZ()); +// } curPos.nextPosition(); } // Register the chunks Map.Entry originValue = getEntry(file, MathMan.pairInt(curPos.getBlockX(), curPos.getBlockZ())); + long pairX, pos; for (int x = 0; x <= chunkX; x++) { + int xx = curPos.getBlockX() + x; + pairX = ((long) xx) << 32; for (int z = 0; z <= chunkZ; z++) { - long pos = MathMan.pairInt(curPos.getBlockX() + x, curPos.getBlockZ() + z); + int zz = curPos.getBlockZ() + z; + pos = pairX + (zz & 0xffffffffL); files.put(pos, originValue); } } - return curPos; + for (int i = 0; i < Math.min(chunkX, chunkZ); i++) curPos.nextPosition(); + return curPos.toBlockVector2D(); } private boolean isAreaFree(BlockVector2D chunkPos1, BlockVector2D chunkPos2 /* inclusive */) { @@ -305,9 +312,18 @@ public class SchemVis extends ImmutableVirtualWorld { public void add(File file) throws IOException { File cached = new File(file.getParentFile(), "." + file.getName() + ".cached"); + Integer dimensionPair = DIMENSION_CACHE.get(file); + if (dimensionPair != null) { + int width = (char) MathMan.unpairX(dimensionPair); + int length = (char) MathMan.unpairY(dimensionPair); + BlockVector2D dimensions = new BlockVector2D(width, length); + BlockVector2D offset = registerAndGetChunkOffset(dimensions, cached); + return; + } if (cached.exists() && file.lastModified() <= cached.lastModified()) { - try (FileInputStream fis = new FileInputStream(cached)) { - BlockVector dimensions = new BlockVector(IOUtil.readVarInt(fis), IOUtil.readVarInt(fis), IOUtil.readVarInt(fis)); + try (InputStream fis = new BufferedInputStream(new FileInputStream(cached), 4)) { + BlockVector2D dimensions = new BlockVector2D(IOUtil.readVarInt(fis), IOUtil.readVarInt(fis)); + DIMENSION_CACHE.put(file, MathMan.pair((short) dimensions.getBlockX(), (short) dimensions.getBlockZ())); BlockVector2D offset = registerAndGetChunkOffset(dimensions, cached); } } else { @@ -321,12 +337,11 @@ public class SchemVis extends ImmutableVirtualWorld { clipboard.setOrigin(clipboard.getMinimumPoint()); try { MCAQueue queue = new MCAQueue(null, null, false); - BlockVector dimensions = clipboard.getDimensions().toBlockVector(); + BlockVector2D dimensions = clipboard.getDimensions().toVector2D().toBlockVector2D(); BlockVector2D offset = registerAndGetChunkOffset(dimensions, cached); new Schematic(clipboard).paste(queue, Vector.ZERO, true); try (FileOutputStream fos = new FileOutputStream(cached)) { IOUtil.writeVarInt(fos, dimensions.getBlockX()); - IOUtil.writeVarInt(fos, dimensions.getBlockY()); IOUtil.writeVarInt(fos, dimensions.getBlockZ()); try (FaweOutputStream cos = MainUtil.getCompressedOS(fos, 2)) { @@ -354,6 +369,8 @@ public class SchemVis extends ImmutableVirtualWorld { java.nio.file.Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); } } + + DIMENSION_CACHE.put(file, MathMan.pair((short) dimensions.getBlockX(), (short) dimensions.getBlockZ())); } finally { if (clipboard instanceof Closeable) { ((Closeable) clipboard).close(); @@ -398,7 +415,7 @@ public class SchemVis extends ImmutableVirtualWorld { int OCZ = MathMan.unpairIntY(origin); try { try (FileInputStream fis = new FileInputStream(cached)) { - BlockVector dimensions = new BlockVector(IOUtil.readVarInt(fis), IOUtil.readVarInt(fis), IOUtil.readVarInt(fis)); + BlockVector2D dimensions = new BlockVector2D(IOUtil.readVarInt(fis), IOUtil.readVarInt(fis)); try (FaweInputStream in = MainUtil.getCompressedIS(fis)) { try (NBTInputStream nis = new NBTInputStream(in)) {