From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 29 May 2020 20:29:02 -0400 Subject: [PATCH] Synchronize PalettedContainer instead of ReentrantLock Mojang has flaws in their logic about chunks being concurrently wrote to. So we constantly see crashes around multiple threads writing. Additionally, java has optimized synchronization so well that its in many times faster than trying to manage read wrote locks for low contention situations. And this is extremely a low contention situation. diff --git a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java index d030baca9dd3c34d14653094c139d853c67ac332..bd003e980ac0fe8c4e4b5df5b4002ae3ada39862 100644 --- a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java +++ b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java @@ -35,15 +35,15 @@ public class PalettedContainer implements PaletteResize { private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer"); public void acquire() { - this.threadingDetector.checkAndLock(); + // this.threadingDetector.checkAndLock(); // Paper - disable this - use proper synchronization } public void release() { - this.threadingDetector.checkAndUnlock(); + // this.threadingDetector.checkAndUnlock(); // Paper - disable this } public static Codec> codec(IdMap idList, Codec entryCodec, PalettedContainer.Strategy provider, T object) { - return RecordCodecBuilder.create((instance) -> { + return RecordCodecBuilder.>create((instance) -> { // Paper - decompile fixes return instance.group(entryCodec.mapResult(ExtraCodecs.orElsePartial(object)).listOf().fieldOf("palette").forGetter(PalettedContainer.DiscData::paletteEntries), Codec.LONG_STREAM.optionalFieldOf("data").forGetter(PalettedContainer.DiscData::storage)).apply(instance, PalettedContainer.DiscData::new); }).comapFlatMap((serialized) -> { return read(idList, provider, serialized); @@ -85,7 +85,7 @@ public class PalettedContainer implements PaletteResize { return data2.palette.idFor(object); } - public T getAndSet(int x, int y, int z, T value) { + public synchronized T getAndSet(int x, int y, int z, T value) { // Paper - synchronize this.acquire(); Object var5; @@ -108,7 +108,7 @@ public class PalettedContainer implements PaletteResize { return this.data.palette.valueFor(j); } - public void set(int x, int y, int z, T value) { + public synchronized void set(int x, int y, int z, T value) { // Paper - synchronize this.acquire(); try { @@ -142,7 +142,7 @@ public class PalettedContainer implements PaletteResize { }); } - public void read(FriendlyByteBuf buf) { + public synchronized void read(FriendlyByteBuf buf) { // Paper - synchronize this.acquire(); try { @@ -157,7 +157,7 @@ public class PalettedContainer implements PaletteResize { } - public void write(FriendlyByteBuf buf) { + public synchronized void write(FriendlyByteBuf buf) { // Paper - synchronize this.acquire(); try { @@ -168,7 +168,7 @@ public class PalettedContainer implements PaletteResize { } - private static DataResult> read(IdMap idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData serialized) { + private static synchronized DataResult> read(IdMap idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData serialized) { // Paper - synchronize List list = serialized.paletteEntries(); int i = provider.size(); int j = provider.calculateBitsForSerialization(idList, list.size()); @@ -207,7 +207,7 @@ public class PalettedContainer implements PaletteResize { return DataResult.success(new PalettedContainer<>(idList, provider, configuration, bitStorage, list)); } - private PalettedContainer.DiscData write(IdMap idList, PalettedContainer.Strategy provider) { + private synchronized PalettedContainer.DiscData write(IdMap idList, PalettedContainer.Strategy provider) { // Paper - synchronize this.acquire(); PalettedContainer.DiscData var12;