Skip to content

Commit

Permalink
fix: non-atomic player chunk position tracking
Browse files Browse the repository at this point in the history
This resolves the case when all no-tick chunks sometimes decide to
reload, causing lag spikes
  • Loading branch information
ishland committed Jul 26, 2023
1 parent 07a0666 commit a38bbd8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -11,6 +11,8 @@
import net.minecraft.util.math.ChunkPos;
import org.threadly.concurrent.NoThreadScheduler;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -77,9 +79,22 @@ public void tick(ThreadedAnvilChunkStorage tacs) {
}

private void scheduleTick(ThreadedAnvilChunkStorage tacs) {
if (this.isTicking.compareAndSet(false, true))
if (this.isTicking.compareAndSet(false, true)) {
List<Runnable> tasks = new ArrayList<>(this.pendingActionsOnScheduler.size() + 3);
{
Runnable r;
while ((r = this.pendingActionsOnScheduler.poll()) != null) {
tasks.add(r);
}
}
executor.execute(() -> {
drainQueue(this.pendingActionsOnScheduler);
for (Runnable task : tasks) {
try {
task.run();
} catch (Throwable t) {
t.printStackTrace();
}
}

boolean hasNoTickTicketUpdates;
if (pendingPurge) {
Expand All @@ -106,6 +121,7 @@ private void scheduleTick(ThreadedAnvilChunkStorage tacs) {
this.isTicking.set(false);
if (hasNormalTicketUpdates || hasNoTickUpdates) scheduleTick(tacs); // run more tasks
});
}
}

private void drainQueue(ConcurrentLinkedQueue<Runnable> queue) {
Expand Down
Expand Up @@ -68,6 +68,7 @@ protected void setLevel(long chunkPos, int level) {
}
} else {
if (!this.distanceFromNearestPlayer.containsKey(chunkPos)) {
pendingTicketRemoves.remove(chunkPos);
pendingTicketAdds.enqueue(new ChunkPos(chunkPos), level);
}
pendingTicketAdds.changePriority(new ChunkPos(chunkPos), level);
Expand Down

0 comments on commit a38bbd8

Please sign in to comment.