aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0520-Optimize-NibbleArray-to-use-pooled-buffers.patch
blob: bbd03eef7a5a77c312138224687f512baff0a93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 6 May 2020 23:30:30 -0400
Subject: [PATCH] Optimize NibbleArray to use pooled buffers

Massively reduces memory allocation of 2048 byte buffers by using
an object pool for these.

Uses lots of advanced new capabilities of the Paper codebase :)

diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
index e625842e524f18e469f7695b27d52d4d04892266..d752e793f13a9caf5d255293f7ce9d562fd50064 100644
--- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
+++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
@@ -387,11 +387,11 @@ public class ChunkRegionLoader {
                 }
 
                 if (nibblearray != null && !nibblearray.c()) {
-                    nbttagcompound2.setByteArray("BlockLight", nibblearray.asBytes());
+                    nbttagcompound2.setByteArray("BlockLight", nibblearray.asBytesPoolSafe().clone()); // Paper
                 }
 
                 if (nibblearray1 != null && !nibblearray1.c()) {
-                    nbttagcompound2.setByteArray("SkyLight", nibblearray1.asBytes());
+                    nbttagcompound2.setByteArray("SkyLight", nibblearray1.asBytesPoolSafe().clone()); // Paper
                 }
 
                 nbttaglist.add(nbttagcompound2);
diff --git a/src/main/java/net/minecraft/server/LightEngineStorage.java b/src/main/java/net/minecraft/server/LightEngineStorage.java
index 88277d23c36696fdd5363e41a130c9a443fac2c0..b0b7544592981bcff22c8acee7230a211918ef28 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorage.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorage.java
@@ -148,7 +148,7 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
     protected NibbleArray j(long i) {
         NibbleArray nibblearray = (NibbleArray) this.i.get(i);
 
-        return nibblearray != null ? nibblearray : new NibbleArray();
+        return nibblearray != null ? nibblearray : new NibbleArray().markPoolSafe(); // Paper
     }
 
     protected void a(LightEngineLayer<?, ?> lightenginelayer, long i) {
@@ -317,9 +317,9 @@ public abstract class LightEngineStorage<M extends LightEngineStorageArray<M>> e
 
     protected void a(long i, @Nullable NibbleArray nibblearray) {
         if (nibblearray != null) {
-            this.i.put(i, nibblearray);
+            NibbleArray remove = this.i.put(i, nibblearray); if (remove != null && remove.cleaner != null) remove.cleaner.run(); // Paper - clean up when removed
         } else {
-            this.i.remove(i);
+            NibbleArray remove = this.i.remove(i); if (remove != null && remove.cleaner != null) remove.cleaner.run(); // Paper - clean up when removed
         }
 
     }
diff --git a/src/main/java/net/minecraft/server/LightEngineStorageArray.java b/src/main/java/net/minecraft/server/LightEngineStorageArray.java
index 53199595da71a25710bd1ff8ee2868ee63edc0e1..37c44a89f28c44915fcae5a7e2c4797b1c123723 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorageArray.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorageArray.java
@@ -33,7 +33,9 @@ public abstract class LightEngineStorageArray<M extends LightEngineStorageArray<
 
     public void a(long i) {
         if (this.isVisible) { throw new IllegalStateException("writing to visible data"); } // Paper - avoid copying light data
-        this.data.queueUpdate(i, ((NibbleArray) this.data.getUpdating(i)).b()); // Paper - avoid copying light data
+        NibbleArray updating = this.data.getUpdating(i); // Paper - pool nibbles
+        this.data.queueUpdate(i, new NibbleArray().markPoolSafe(updating.getCloneIfSet())); // Paper - avoid copying light data - pool safe clone
+        if (updating.cleaner != null) MCUtil.scheduleTask(2, updating.cleaner, "Light Engine Release"); // Paper - delay clean incase anything holding ref was still using it
         this.c();
     }
 
diff --git a/src/main/java/net/minecraft/server/LightEngineStorageSky.java b/src/main/java/net/minecraft/server/LightEngineStorageSky.java
index 06bc8371fe9de4d23fdd47e5a3919541bb399fd8..097f58e9ac3f4096d3b9dad75b6ebe76021fa92c 100644
--- a/src/main/java/net/minecraft/server/LightEngineStorageSky.java
+++ b/src/main/java/net/minecraft/server/LightEngineStorageSky.java
@@ -166,9 +166,9 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
                     j = SectionPosition.a(j, EnumDirection.UP);
                 }
 
-                return new NibbleArray((new NibbleArrayFlat(nibblearray1, 0)).asBytes());
+                return new NibbleArray().markPoolSafe(new NibbleArrayFlat(nibblearray1, 0).asBytes()); // Paper - mark pool use as safe (no auto cleaner)
             } else {
-                return new NibbleArray();
+                return new NibbleArray().markPoolSafe(); // Paper - mark pool use as safe (no auto cleaner)
             }
         }
     }
@@ -197,7 +197,7 @@ public class LightEngineStorageSky extends LightEngineStorage<LightEngineStorage
                                 ((LightEngineStorageSky.a) this.f).a(i);
                             }
 
-                            Arrays.fill(this.a(i, true).asBytes(), (byte) -1);
+                            Arrays.fill(this.a(i, true).asBytesPoolSafe(), (byte) -1); // Paper
                             k = SectionPosition.c(SectionPosition.b(i));
                             l = SectionPosition.c(SectionPosition.c(i));
                             int i1 = SectionPosition.c(SectionPosition.d(i));
diff --git a/src/main/java/net/minecraft/server/NibbleArray.java b/src/main/java/net/minecraft/server/NibbleArray.java
index 996c8326387b5a7fe62db6a76e000144565cb85b..8cedfdd820cc02a76607b53e0b054fc74654f907 100644
--- a/src/main/java/net/minecraft/server/NibbleArray.java
+++ b/src/main/java/net/minecraft/server/NibbleArray.java
@@ -1,16 +1,75 @@
 package net.minecraft.server;
 
+import com.destroystokyo.paper.util.pooled.PooledObjects; // Paper
+
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 public class NibbleArray {
 
-    @Nullable
-    protected byte[] a;
+    // Paper start
+    public static byte[] EMPTY_NIBBLE = new byte[2048];
+    private static final int nibbleBucketSizeMultiplier = Integer.getInteger("Paper.nibbleBucketSize", 3072);
+    private static final int maxPoolSize = Integer.getInteger("Paper.maxNibblePoolSize", (int) Math.min(6, Math.max(1, Runtime.getRuntime().maxMemory() / 1024 / 1024 / 1024)) * (nibbleBucketSizeMultiplier * 8));
+    public static final PooledObjects<byte[]> BYTE_2048 = new PooledObjects<>(() -> new byte[2048], maxPoolSize);
+    public static void releaseBytes(byte[] bytes) {
+        if (bytes != null && bytes != EMPTY_NIBBLE && bytes.length == 2048) {
+            System.arraycopy(EMPTY_NIBBLE, 0, bytes, 0, 2048);
+            BYTE_2048.release(bytes);
+        }
+    }
+
+    public NibbleArray markPoolSafe(byte[] bytes) {
+        if (bytes != EMPTY_NIBBLE) this.a = bytes;
+        return markPoolSafe();
+    }
+    public NibbleArray markPoolSafe() {
+        poolSafe = true;
+        return this;
+    }
+    public byte[] getIfSet() {
+        return this.a != null ? this.a : EMPTY_NIBBLE;
+    }
+    public byte[] getCloneIfSet() {
+        if (a == null) {
+            return EMPTY_NIBBLE;
+        }
+        byte[] ret = BYTE_2048.acquire();
+        System.arraycopy(getIfSet(), 0, ret, 0, 2048);
+        return ret;
+    }
+
+    public NibbleArray cloneAndSet(byte[] bytes) {
+        if (bytes != null && bytes != EMPTY_NIBBLE) {
+            this.a = BYTE_2048.acquire();
+            System.arraycopy(bytes, 0, this.a, 0, 2048);
+        }
+        return this;
+    }
+    boolean poolSafe = false;
+    public java.lang.Runnable cleaner;
+    private void registerCleaner() {
+        if (!poolSafe) {
+            cleaner = MCUtil.registerCleaner(this, this.a, NibbleArray::releaseBytes);
+        } else {
+            cleaner = MCUtil.once(() -> NibbleArray.releaseBytes(this.a));
+        }
+    }
+    // Paper end
+    @Nullable protected byte[] a;
+
 
     public NibbleArray() {}
 
     public NibbleArray(byte[] abyte) {
+        // Paper start
+        this(abyte, false);
+    }
+    public NibbleArray(byte[] abyte, boolean isSafe) {
         this.a = abyte;
+        if (!isSafe) this.a = getCloneIfSet(); // Paper - clone for safety
+        registerCleaner();
+        // Paper end
         if (abyte.length != 2048) {
             throw (IllegalArgumentException) SystemUtils.c(new IllegalArgumentException("ChunkNibbleArrays should be 2048 bytes not: " + abyte.length));
         }
@@ -44,7 +103,8 @@ public class NibbleArray {
 
     public void a(int i, int j) { // PAIL: private -> public
         if (this.a == null) {
-            this.a = new byte[2048];
+            this.a = BYTE_2048.acquire(); // Paper
+            registerCleaner();// Paper
         }
 
         int k = this.d(i);
@@ -66,14 +126,36 @@ public class NibbleArray {
     public byte[] asBytes() {
         if (this.a == null) {
             this.a = new byte[2048];
+        } else { // Paper start
+            // Accessor may need this object past garbage collection so need to clone it and return pooled value
+            // If we know its safe for pre GC access, use asBytesPoolSafe(). If you just need read, use getIfSet()
+            Runnable cleaner = this.cleaner;
+            if (cleaner != null) {
+                this.a = this.a.clone();
+                cleaner.run(); // release the previously pooled value
+                this.cleaner = null;
+            }
+        }
+        // Paper end
+
+        return this.a;
+    }
+
+    @Nonnull
+    public byte[] asBytesPoolSafe() {
+        if (this.a == null) {
+            this.a = BYTE_2048.acquire(); // Paper
+            registerCleaner(); // Paper
         }
 
+        //noinspection ConstantConditions
         return this.a;
     }
+    // Paper end
 
     public NibbleArray copy() { return this.b(); } // Paper - OBFHELPER
     public NibbleArray b() {
-        return this.a == null ? new NibbleArray() : new NibbleArray((byte[]) this.a.clone());
+        return this.a == null ? new NibbleArray() : new NibbleArray(this.a); // Paper - clone in ctor
     }
 
     public String toString() {
diff --git a/src/main/java/net/minecraft/server/NibbleArrayFlat.java b/src/main/java/net/minecraft/server/NibbleArrayFlat.java
index 67c960292db9d99ac85b5d0dda50ae48ef942c1b..5e3efa1fa6c089df35971ce5c83da384f7dbd402 100644
--- a/src/main/java/net/minecraft/server/NibbleArrayFlat.java
+++ b/src/main/java/net/minecraft/server/NibbleArrayFlat.java
@@ -8,7 +8,7 @@ public class NibbleArrayFlat extends NibbleArray {
 
     public NibbleArrayFlat(NibbleArray nibblearray, int i) {
         super(128);
-        System.arraycopy(nibblearray.asBytes(), i * 128, this.a, 0, 128);
+        System.arraycopy(nibblearray.getIfSet(), i * 128, this.a, 0, 128); // Paper
     }
 
     @Override
@@ -18,7 +18,7 @@ public class NibbleArrayFlat extends NibbleArray {
 
     @Override
     public byte[] asBytes() {
-        byte[] abyte = new byte[2048];
+        byte[] abyte = BYTE_2048.acquire(); // Paper
 
         for (int i = 0; i < 16; ++i) {
             System.arraycopy(this.a, 0, abyte, i * 128, 128);
diff --git a/src/main/java/net/minecraft/server/PacketPlayOutLightUpdate.java b/src/main/java/net/minecraft/server/PacketPlayOutLightUpdate.java
index cd1ad45469aa163b9bc41774ae80adfa617fd97b..27068f73a2b1fd97dc675ceb3f0eb07ac9e59437 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutLightUpdate.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutLightUpdate.java
@@ -16,13 +16,42 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
     private List<byte[]> g;
     private List<byte[]> h;
 
+    // Paper start
+    java.lang.Runnable cleaner1;
+    java.lang.Runnable cleaner2;
+    java.util.concurrent.atomic.AtomicInteger remainingSends = new java.util.concurrent.atomic.AtomicInteger(0);
+
+    @Override
+    public void onPacketDispatch(EntityPlayer player) {
+        remainingSends.incrementAndGet();
+    }
+
+    @Override
+    public void onPacketDispatchFinish(EntityPlayer player, io.netty.channel.ChannelFuture future) {
+        if (remainingSends.decrementAndGet() <= 0) {
+            // incase of any race conditions, schedule this delayed
+            MCUtil.scheduleTask(5, () -> {
+                if (remainingSends.get() == 0) {
+                    cleaner1.run();
+                    cleaner2.run();
+                }
+            }, "Light Packet Release");
+        }
+    }
+
+    @Override
+    public boolean hasFinishListener() {
+        return true;
+    }
+
+    // Paper end
     public PacketPlayOutLightUpdate() {}
 
     public PacketPlayOutLightUpdate(ChunkCoordIntPair chunkcoordintpair, LightEngine lightengine) {
         this.a = chunkcoordintpair.x;
         this.b = chunkcoordintpair.z;
-        this.g = Lists.newArrayList();
-        this.h = Lists.newArrayList();
+        this.g = Lists.newArrayList();cleaner1 = MCUtil.registerListCleaner(this, this.g, NibbleArray::releaseBytes); // Paper
+        this.h = Lists.newArrayList();cleaner2 = MCUtil.registerListCleaner(this, this.h, NibbleArray::releaseBytes); // Paper
 
         for (int i = 0; i < 18; ++i) {
             NibbleArray nibblearray = lightengine.a(EnumSkyBlock.SKY).a(SectionPosition.a(chunkcoordintpair, -1 + i));
@@ -33,7 +62,7 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
                     this.e |= 1 << i;
                 } else {
                     this.c |= 1 << i;
-                    this.g.add(nibblearray.asBytes().clone());
+                    this.g.add(nibblearray.getCloneIfSet()); // Paper
                 }
             }
 
@@ -42,7 +71,7 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
                     this.f |= 1 << i;
                 } else {
                     this.d |= 1 << i;
-                    this.h.add(nibblearray1.asBytes().clone());
+                    this.h.add(nibblearray1.getCloneIfSet()); // Paper
                 }
             }
         }
@@ -54,8 +83,8 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
         this.b = chunkcoordintpair.z;
         this.c = i;
         this.d = j;
-        this.g = Lists.newArrayList();
-        this.h = Lists.newArrayList();
+        this.g = Lists.newArrayList();cleaner1 = MCUtil.registerListCleaner(this, this.g, NibbleArray::releaseBytes); // Paper
+        this.h = Lists.newArrayList();cleaner2 = MCUtil.registerListCleaner(this, this.h, NibbleArray::releaseBytes); // Paper
 
         for (int k = 0; k < 18; ++k) {
             NibbleArray nibblearray;
@@ -63,7 +92,7 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
             if ((this.c & 1 << k) != 0) {
                 nibblearray = lightengine.a(EnumSkyBlock.SKY).a(SectionPosition.a(chunkcoordintpair, -1 + k));
                 if (nibblearray != null && !nibblearray.c()) {
-                    this.g.add(nibblearray.asBytes().clone());
+                    this.g.add(nibblearray.getCloneIfSet()); // Paper
                 } else {
                     this.c &= ~(1 << k);
                     if (nibblearray != null) {
@@ -75,7 +104,7 @@ public class PacketPlayOutLightUpdate implements Packet<PacketListenerPlayOut> {
             if ((this.d & 1 << k) != 0) {
                 nibblearray = lightengine.a(EnumSkyBlock.BLOCK).a(SectionPosition.a(chunkcoordintpair, -1 + k));
                 if (nibblearray != null && !nibblearray.c()) {
-                    this.h.add(nibblearray.asBytes().clone());
+                    this.h.add(nibblearray.getCloneIfSet()); // Paper
                 } else {
                     this.d &= ~(1 << k);
                     if (nibblearray != null) {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java
index 39ef95cbbb1d1d049354ae1e8991309e918d0462..ec3f560556c3056bdfc11cc6a7eb76420f13abad 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java
@@ -271,14 +271,14 @@ public class CraftChunk implements Chunk {
                     sectionSkyLights[i] = emptyLight;
                 } else {
                     sectionSkyLights[i] = new byte[2048];
-                    System.arraycopy(skyLightArray.asBytes(), 0, sectionSkyLights[i], 0, 2048);
+                    System.arraycopy(skyLightArray.getIfSet(), 0, sectionSkyLights[i], 0, 2048); // Paper
                 }
                 NibbleArray emitLightArray = lightengine.a(EnumSkyBlock.BLOCK).a(SectionPosition.a(x, i, z));
                 if (emitLightArray == null) {
                     sectionEmitLights[i] = emptyLight;
                 } else {
                     sectionEmitLights[i] = new byte[2048];
-                    System.arraycopy(emitLightArray.asBytes(), 0, sectionEmitLights[i], 0, 2048);
+                    System.arraycopy(emitLightArray.getIfSet(), 0, sectionEmitLights[i], 0, 2048); // Paper
                 }
             }
         }