Skip to content

Commit 68c9e88

Browse files
committed
perf: optimize player logic and calculations
1 parent 2b6fef4 commit 68c9e88

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/main/java/me/mapacheee/extendedhorizons/runtime/RuntimeOrchestratorService.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.mojang.datafixers.util.Pair;
3030

3131
import java.util.ArrayList;
32+
import java.util.Collection;
3233
import java.util.Collections;
3334
import java.util.List;
3435

@@ -37,6 +38,9 @@ public final class RuntimeOrchestratorService {
3738

3839
private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeOrchestratorService.class);
3940
private static final int TICK_LENGTH_DIVISOR = 2;
41+
private static final int EQUIPMENT_SLOT_COUNT = EquipmentSlot.values().length;
42+
private static final int MAX_PLAYERS_PER_TICK = 200;
43+
private static final long NANOS_PER_TICK = 50_000_000L;
4044

4145
private final Container<EhConfig> configContainer;
4246
private final SessionRegistry sessionRegistry;
@@ -86,38 +90,42 @@ private void runtimeTick() {
8690
if (plugin == null || !plugin.isEnabled()) {
8791
return;
8892
}
89-
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
90-
if (players.isEmpty()) {
93+
List<Player> playerList = new ArrayList<>(Bukkit.getOnlinePlayers());
94+
if (playerList.isEmpty()) {
9195
return;
9296
}
93-
Collections.shuffle(players);
97+
Collections.shuffle(playerList);
9498

9599
int nettyThreadCount = Math.max(1, SystemPropertyUtil.getInt(
96100
"io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
97101
EhConfig config = this.configContainer.get();
98102
this.generationLimiterService.reset(config.maxGlobalGenerationsPerTick());
99103
long periodTicks = Math.max(1L, config.runtimePeriodTicks());
100-
long nanosPerServerTick = 50_000_000L * periodTicks;
104+
long nanosPerServerTick = NANOS_PER_TICK * periodTicks;
101105
long tickLengthNanos = nanosPerServerTick / TICK_LENGTH_DIVISOR;
102-
long maxTimePerPlayerNanos = (nettyThreadCount * tickLengthNanos) / Math.max(nettyThreadCount, players.size());
106+
107+
int playerCount = Math.min(playerList.size(), MAX_PLAYERS_PER_TICK);
108+
long maxTimePerPlayerNanos = (nettyThreadCount * tickLengthNanos) / Math.max(1, playerCount);
103109

104110
this.orchestratorTick = (this.orchestratorTick + 1) & Integer.MAX_VALUE;
105111
boolean farPlayersEnabled = config.farPlayersEnabled();
106112
boolean pollEquipment = farPlayersEnabled && Math.floorMod(this.orchestratorTick, config.farPlayerEquipTicks()) == 0;
107113

108-
for (Player player : players) {
109-
this.sessionRegistry.ensureFor(player, false);
114+
for (Player player : playerList) {
115+
player.getWorld();
116+
this.sessionRegistry.ensureFor(player, false);
110117
FoliaTaskUtil.runForPlayer(player, plugin, () -> {
111118
try {
112-
Location loc = player.getLocation();
113-
ServerPlayer nmsPlayer = ((CraftPlayer) player).getHandle();
119+
Location loc = player.getLocation();
120+
player.getWorld();
121+
ServerPlayer nmsPlayer = ((CraftPlayer) player).getHandle();
114122

115123
if (farPlayersEnabled) {
116124
List<SynchedEntityData.DataValue<?>> metadata = nmsPlayer.getEntityData().packAll();
117125
List<Pair<EquipmentSlot, ItemStack>> equipment;
118126

119127
if (pollEquipment) {
120-
equipment = new ArrayList<>(6);
128+
equipment = new ArrayList<>(EQUIPMENT_SLOT_COUNT);
121129
for (EquipmentSlot slot : EquipmentSlot.values()) {
122130
ItemStack item = nmsPlayer.getItemBySlot(slot);
123131
equipment.add(Pair.of(slot, item.copy()));

0 commit comments

Comments
 (0)