From 03abdfdc498965bb387588162a2c7de7555f74a2 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Tue, 9 May 2023 16:52:10 -0400 Subject: [PATCH 1/2] Optimize server thread aliveness check --- .../mixin/bugfix/chunk_deadlock/ServerChunkCacheMixin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/chunk_deadlock/ServerChunkCacheMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/chunk_deadlock/ServerChunkCacheMixin.java index 682577bd..baf84759 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/chunk_deadlock/ServerChunkCacheMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/chunk_deadlock/ServerChunkCacheMixin.java @@ -29,7 +29,7 @@ public abstract class ServerChunkCacheMixin { @Inject(method = "getChunk", at = @At("HEAD"), cancellable = true) private void bailIfServerDead(int chunkX, int chunkZ, ChunkStatus requiredStatus, boolean load, CallbackInfoReturnable cir) { - if(!this.mainThread.isAlive()) { + if(!this.level.getServer().isRunning() && !this.mainThread.isAlive()) { ModernFix.LOGGER.fatal("A mod is accessing chunks from a stopped server (this will also cause memory leaks)"); if(debugDeadServerAccess) { new Exception().printStackTrace(); From bcb33a1018a7c58056d5fc91add49c9838b948e6 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Wed, 10 May 2023 09:54:14 -0400 Subject: [PATCH 2/2] Shorten structure hashes to fix issues on Windows --- .../structure/CachingStructureManager.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/structure/CachingStructureManager.java b/common/src/main/java/org/embeddedt/modernfix/structure/CachingStructureManager.java index b5ed3b88..365fb4d7 100644 --- a/common/src/main/java/org/embeddedt/modernfix/structure/CachingStructureManager.java +++ b/common/src/main/java/org/embeddedt/modernfix/structure/CachingStructureManager.java @@ -49,6 +49,12 @@ public class CachingStructureManager { private static final Set laggyStructureMods = new ObjectOpenHashSet<>(); + private static final int MAX_HASH_LENGTH = 9; + + private static String truncateHash(String hash) { + return hash.substring(0, MAX_HASH_LENGTH + 1); + } + public static CompoundTag readStructureTag(ResourceLocation location, DataFixer datafixer, InputStream stream) throws IOException { byte[] structureBytes = toBytes(stream); CompoundTag currentTag = NbtIo.readCompressed(new ByteArrayInputStream(structureBytes)); @@ -57,20 +63,22 @@ public class CachingStructureManager { } int currentDataVersion = currentTag.getInt("DataVersion"); if(currentDataVersion < SharedConstants.getCurrentVersion().getWorldVersion()) { - synchronized (laggyStructureMods) { - if(laggyStructureMods.add(location.getNamespace())) { - ModernFix.LOGGER.warn("Mod {} is shipping outdated structure files, which can cause worldgen lag; please report this to them.", location.getNamespace()); - } - } /* Needs upgrade, try looking up from cache */ MessageDigest hasher = digestThreadLocal.get(); hasher.reset(); String hash = encodeHex(hasher.digest(structureBytes)); - CompoundTag cachedUpgraded = getCachedUpgraded(location, hash); + CompoundTag cachedUpgraded = getCachedUpgraded(location, truncateHash(hash)); + if(cachedUpgraded == null) + cachedUpgraded = getCachedUpgraded(location, hash); /* pick up old cache */ if(cachedUpgraded != null && cachedUpgraded.getInt("DataVersion") == SharedConstants.getCurrentVersion().getWorldVersion()) { ModernFix.LOGGER.debug("Using cached upgraded version of {}", location); currentTag = cachedUpgraded; } else { + synchronized (laggyStructureMods) { + if(laggyStructureMods.add(location.getNamespace())) { + ModernFix.LOGGER.warn("Mod {} is shipping outdated structure files, which can cause worldgen lag; please report this to them.", location.getNamespace()); + } + } ModernFix.LOGGER.debug("Structure {} is being run through DFU (hash {}), this will cause launch time delays", location, hash); currentTag = NbtUtils.update(datafixer, DataFixTypes.STRUCTURE, currentTag, currentDataVersion, SharedConstants.getCurrentVersion().getWorldVersion()); @@ -99,7 +107,7 @@ public class CachingStructureManager { } private static synchronized void saveCachedUpgraded(ResourceLocation location, String hash, CompoundTag tagToSave) { - File theFile = getCachePath(location, hash); + File theFile = getCachePath(location, truncateHash(hash)); try { NbtIo.writeCompressed(tagToSave, theFile); } catch(IOException e) {