Skip to content

Commit cdd0dc8

Browse files
authored
Check for last chunk before acquiring lock (#3066)
1 parent b4129dc commit cdd0dc8

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/SingleThreadQueueExtent.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public class SingleThreadQueueExtent extends ExtentBatchProcessorHolder implemen
6161
private Thread currentThread;
6262
// Last access pointers
6363
private volatile IQueueChunk lastChunk;
64-
private volatile long lastPair = Long.MAX_VALUE;
6564
private boolean enabledQueue = true;
6665
private boolean fastmode = false;
6766
// Array for lazy avoidance of concurrent modification exceptions and needless overcomplication of code (synchronisation is
@@ -163,7 +162,6 @@ protected synchronized void reset() {
163162
}
164163
this.enabledQueue = true;
165164
this.lastChunk = null;
166-
this.lastPair = Long.MAX_VALUE;
167165
this.currentThread = null;
168166
this.initialized = false;
169167
this.setProcessor(EmptyBatchProcessor.getInstance());
@@ -217,7 +215,6 @@ public boolean isEmpty() {
217215
@Override
218216
public <V extends Future<V>> V submit(IQueueChunk chunk) {
219217
if (lastChunk == chunk) {
220-
lastPair = Long.MAX_VALUE;
221218
lastChunk = null;
222219
}
223220
final long index = MathMan.pairInt(chunk.getX(), chunk.getZ());
@@ -268,7 +265,6 @@ public synchronized boolean trim(boolean aggressive) {
268265
cacheSet.trim(aggressive);
269266
if (Thread.currentThread() == currentThread) {
270267
lastChunk = null;
271-
lastPair = Long.MAX_VALUE;
272268
return chunks.isEmpty();
273269
}
274270
if (!submissions.isEmpty()) {
@@ -306,23 +302,21 @@ public IQueueChunk wrap(IQueueChunk chunk) {
306302

307303
@Override
308304
public final IQueueChunk getOrCreateChunk(int x, int z) {
305+
final IQueueChunk lastChunk = this.lastChunk;
306+
if (lastChunk != null && lastChunk.getX() == x && lastChunk.getZ() == z) {
307+
return lastChunk;
308+
}
309+
final long pair = MathMan.pairInt(x, z);
310+
if (!processGet(x, z) || (Settings.settings().REGION_RESTRICTIONS_OPTIONS.RESTRICT_TO_SAFE_RANGE
311+
&& (x > 1875000 || z > 1875000 || x < -1875000 || z < -1875000))) {
312+
// don't store as last chunk, not worth it
313+
return NullChunk.getInstance();
314+
}
309315
getChunkLock.lock();
310316
try {
311-
final long pair = (long) x << 32 | z & 0xffffffffL;
312-
if (pair == lastPair) {
313-
return lastChunk;
314-
}
315-
if (!processGet(x, z) || (Settings.settings().REGION_RESTRICTIONS_OPTIONS.RESTRICT_TO_SAFE_RANGE
316-
// if any chunk coord is outside 30 million blocks
317-
&& (x > 1875000 || z > 1875000 || x < -1875000 || z < -1875000))) {
318-
lastPair = pair;
319-
lastChunk = NullChunk.getInstance();
320-
return NullChunk.getInstance();
321-
}
322317
IQueueChunk chunk = chunks.get(pair);
323318
if (chunk != null) {
324-
lastPair = pair;
325-
lastChunk = chunk;
319+
this.lastChunk = chunk;
326320
return chunk;
327321
}
328322
final int size = chunks.size();
@@ -343,8 +337,7 @@ public final IQueueChunk getOrCreateChunk(int x, int z) {
343337
chunk = wrap(chunk);
344338

345339
chunks.put(pair, chunk);
346-
lastPair = pair;
347-
lastChunk = chunk;
340+
this.lastChunk = chunk;
348341

349342
return chunk;
350343
} finally {

0 commit comments

Comments
 (0)