From 7dd6aa2360ed101b083dbb4b44b60e3a8e104525 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 18 Apr 2021 01:55:35 +0200 Subject: [PATCH] Separate PerChunkThreadProvider from PerInstanceThreadProvider, support arbitrary ThreadProvider#findThread number --- .../net/minestom/server/UpdateManager.java | 2 +- .../net/minestom/server/entity/Player.java | 1 + .../server/thread/PerChunkThreadProvider.java | 20 +++++++++++++++++++ .../thread/PerInstanceThreadProvider.java | 12 ++++++----- .../server/thread/ThreadProvider.java | 4 ++-- 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index 60b2c67ce..d12266a19 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -39,8 +39,8 @@ public final class UpdateManager { { // DEFAULT THREAD PROVIDER - //threadProvider = new PerGroupChunkProvider(); threadProvider = new PerInstanceThreadProvider(4); + //threadProvider = new PerChunkThreadProvider(4); } /** diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b961e8082..d683ef356 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -327,6 +327,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, packet.process(this); } + //System.out.println(getAcquiredElement().getHandler().getBatchThread()); Collection> players = new ArrayList<>(); //if (username.equals("TheMode911")) for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) { diff --git a/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java b/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java new file mode 100644 index 000000000..ecc66166a --- /dev/null +++ b/src/main/java/net/minestom/server/thread/PerChunkThreadProvider.java @@ -0,0 +1,20 @@ +package net.minestom.server.thread; + +import net.minestom.server.instance.Chunk; +import net.minestom.server.utils.chunk.ChunkUtils; +import org.jetbrains.annotations.NotNull; + +/** + * Each {@link Chunk} gets assigned to a random thread. + */ +public class PerChunkThreadProvider extends ThreadProvider { + + public PerChunkThreadProvider(int threadCount) { + super(threadCount); + } + + @Override + public long findThread(@NotNull Chunk chunk) { + return ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ()); + } +} diff --git a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java index 87c541f60..5fee99902 100644 --- a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java @@ -1,10 +1,12 @@ package net.minestom.server.thread; import net.minestom.server.instance.Chunk; +import net.minestom.server.instance.Instance; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.ThreadLocalRandom; - +/** + * Each {@link Instance} gets assigned to a random thread. + */ public class PerInstanceThreadProvider extends ThreadProvider { public PerInstanceThreadProvider(int threadCount) { @@ -12,7 +14,7 @@ public class PerInstanceThreadProvider extends ThreadProvider { } @Override - public int findThread(@NotNull Chunk chunk) { - return ThreadLocalRandom.current().nextInt(getThreads().size()); + public long findThread(@NotNull Chunk chunk) { + return System.identityHashCode(chunk.getInstance()); } -} +} \ No newline at end of file diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 08d2fa34b..f04aa996f 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -57,10 +57,10 @@ public abstract class ThreadProvider { * * @param chunk the chunk */ - public abstract int findThread(@NotNull Chunk chunk); + public abstract long findThread(@NotNull Chunk chunk); protected void addChunk(Chunk chunk) { - int threadId = findThread(chunk); + final int threadId = (int) (Math.abs(findThread(chunk)) % threads.size()); BatchThread thread = threads.get(threadId); var chunks = threadChunkMap.computeIfAbsent(thread, batchThread -> ConcurrentHashMap.newKeySet());