From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 27 Apr 2020 04:05:38 -0700 Subject: [PATCH] Stop copy-on-write operations for updating light data Causes huge memory allocations + gc issues diff --git a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorage.java b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorage.java index 5f3d2c090d098834e38e447d93f1ea8184c8fb3e..5b1ff4ff87591dd4ff0b79e4ac6ff0494fc3d0f8 100644 --- a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorage.java +++ b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorage.java @@ -26,8 +26,8 @@ public abstract class LightEngineStorage> e protected final LongSet b = new LongOpenHashSet(); protected final LongSet c = new LongOpenHashSet(); protected final LongSet d = new LongOpenHashSet(); - protected volatile M e; - protected final M f; + protected volatile M e_visible; protected final Object visibleUpdateLock = new Object(); // Paper - diff on change, should be "visible" - force compile fail on usage change + protected final M f; // Paper - diff on change, should be "updating" protected final LongSet g = new LongOpenHashSet(); protected final LongSet h = new LongOpenHashSet(); protected final Long2ObjectMap i = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap()); @@ -41,8 +41,8 @@ public abstract class LightEngineStorage> e this.l = enumskyblock; this.m = ilightaccess; this.f = m0; - this.e = m0.b(); - this.e.d(); + this.e_visible = m0.b(); // Paper - avoid copying light data + this.e_visible.d(); // Paper - avoid copying light data } protected boolean g(long i) { @@ -51,7 +51,15 @@ public abstract class LightEngineStorage> e @Nullable protected NibbleArray a(long i, boolean flag) { - return this.a(flag ? this.f : this.e, i); + // Paper start - avoid copying light data + if (flag) { + return this.a(this.f, i); + } else { + synchronized (this.visibleUpdateLock) { + return this.a(this.e_visible, i); + } + } + // Paper end - avoid copying light data } @Nullable @@ -364,10 +372,12 @@ public abstract class LightEngineStorage> e protected void e() { if (!this.g.isEmpty()) { + synchronized (this.visibleUpdateLock) { // Paper - avoid copying light data M m0 = this.f.b(); m0.d(); - this.e = m0; + this.e_visible = m0; // Paper - avoid copying light data + } // Paper - avoid copying light data this.g.clear(); } diff --git a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageArray.java b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageArray.java index 242a2c5dea1241b515b9eee7c334ab3c31ad9d12..ed7864c552054fc47c6010a094230ce4aebf1c54 100644 --- a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageArray.java +++ b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageArray.java @@ -9,10 +9,23 @@ public abstract class LightEngineStorageArray a; - - protected LightEngineStorageArray(Long2ObjectOpenHashMap long2objectopenhashmap) { - this.a = long2objectopenhashmap; + protected final com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object data; // Paper - avoid copying light data + protected final boolean isVisible; // Paper - avoid copying light data + java.util.function.Function lookup; // Paper - faster branchless lookup + + // Paper start - avoid copying light data + protected LightEngineStorageArray(com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object data, boolean isVisible) { + if (isVisible) { + data.performUpdatesLockMap(); + } + this.data = data; + this.isVisible = isVisible; + if (isVisible) { + lookup = data::getVisibleAsync; + } else { + lookup = data::getUpdating; + } + // Paper end - avoid copying light data this.c(); this.d = true; } @@ -20,16 +33,17 @@ public abstract class LightEngineStorageArray { protected LightEngineStorageBlock(ILightAccess ilightaccess) { - super(EnumSkyBlock.BLOCK, ilightaccess, new LightEngineStorageBlock.a(new Long2ObjectOpenHashMap())); + super(EnumSkyBlock.BLOCK, ilightaccess, new LightEngineStorageBlock.a(new com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object<>(), false)); // Paper - avoid copying light data } @Override @@ -23,13 +23,13 @@ public class LightEngineStorageBlock extends LightEngineStorage { - public a(Long2ObjectOpenHashMap long2objectopenhashmap) { - super(long2objectopenhashmap); + public a(com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object long2objectopenhashmap, boolean isVisible) { // Paper - avoid copying light data + super(long2objectopenhashmap, isVisible); // Paper - avoid copying light data } @Override public LightEngineStorageBlock.a b() { - return new LightEngineStorageBlock.a(this.a.clone()); + return new a(this.data, true); // Paper - avoid copying light data } } } diff --git a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageSky.java b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageSky.java index befc8f846c772d58ee687ad427bb71206b4dc43e..64d37f4c6a8167f47c80953a388ea6635490563a 100644 --- a/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageSky.java +++ b/src/main/java/net/minecraft/world/level/lighting/LightEngineStorageSky.java @@ -23,15 +23,16 @@ public class LightEngineStorageSky extends LightEngineStorage(), new com.destroystokyo.paper.util.map.QueuedChangesMapLong2Int(), Integer.MAX_VALUE, false)); // Paper - avoid copying light data } @Override protected int d(long i) { long j = SectionPosition.e(i); int k = SectionPosition.c(j); - LightEngineStorageSky.a lightenginestoragesky_a = (LightEngineStorageSky.a) this.e; - int l = lightenginestoragesky_a.c.get(SectionPosition.f(j)); + synchronized (this.visibleUpdateLock) { // Paper - avoid copying light data + LightEngineStorageSky.a lightenginestoragesky_a = (LightEngineStorageSky.a) this.e_visible; // Paper - avoid copying light data - must be after lock acquire + int l = lightenginestoragesky_a.otherData.getVisibleAsync(SectionPosition.f(j)); // Paper - avoid copying light data if (l != lightenginestoragesky_a.b && k < l) { NibbleArray nibblearray = this.a(lightenginestoragesky_a, j); // Paper - decompile fix @@ -52,6 +53,7 @@ public class LightEngineStorageSky extends LightEngineStorage j) { ((LightEngineStorageSky.a) this.f).b = j; - ((LightEngineStorageSky.a) this.f).c.defaultReturnValue(((LightEngineStorageSky.a) this.f).b); + ((LightEngineStorageSky.a) this.f).otherData.queueDefaultReturnValue(((LightEngineStorageSky.a) this.f).b); // Paper - avoid copying light data } long k = SectionPosition.f(i); - int l = ((LightEngineStorageSky.a) this.f).c.get(k); + int l = ((LightEngineStorageSky.a) this.f).otherData.getUpdating(k); // Paper - avoid copying light data if (l < j + 1) { - ((LightEngineStorageSky.a) this.f).c.put(k, j + 1); + ((LightEngineStorageSky.a) this.f).otherData.queueUpdate(k, j + 1); // Paper - avoid copying light data if (this.o.contains(k)) { this.q(i); if (l > ((LightEngineStorageSky.a) this.f).b) { @@ -107,7 +109,7 @@ public class LightEngineStorageSky extends LightEngineStorage= k; } @@ -327,18 +329,21 @@ public class LightEngineStorageSky extends LightEngineStorage { private int b; - private final Long2IntOpenHashMap c; - - public a(Long2ObjectOpenHashMap long2objectopenhashmap, Long2IntOpenHashMap long2intopenhashmap, int i) { - super(long2objectopenhashmap); - this.c = long2intopenhashmap; - long2intopenhashmap.defaultReturnValue(i); + private final com.destroystokyo.paper.util.map.QueuedChangesMapLong2Int otherData; // Paper - avoid copying light data + + // Paper start - avoid copying light data + public a(com.destroystokyo.paper.util.map.QueuedChangesMapLong2Object data, com.destroystokyo.paper.util.map.QueuedChangesMapLong2Int otherData, int i, boolean isVisible) { + super(data, isVisible); + this.otherData = otherData; + otherData.queueDefaultReturnValue(i); + // Paper end - avoid copying light data this.b = i; } @Override public LightEngineStorageSky.a b() { - return new LightEngineStorageSky.a(this.a.clone(), this.c.clone(), this.b); + this.otherData.performUpdatesLockMap(); // Paper - avoid copying light data + return new LightEngineStorageSky.a(this.data, this.otherData, this.b, true); // Paper - avoid copying light data } } }