From bb41ef89ff2c8629b4b9989c6d4010d5f504a9c6 Mon Sep 17 00:00:00 2001 From: Gero Date: Mon, 3 Oct 2022 19:22:01 +0200 Subject: [PATCH] Avoid resizing of ArrayList in ChunkMap#getPlayers (#8416) --- patches/server/Rewrite-chunk-system.patch | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/patches/server/Rewrite-chunk-system.patch b/patches/server/Rewrite-chunk-system.patch index eefa783412..53a90e2faa 100644 --- a/patches/server/Rewrite-chunk-system.patch +++ b/patches/server/Rewrite-chunk-system.patch @@ -13178,14 +13178,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - throw chunkStorage.debugFuturesAndCreateReportedException(new IllegalStateException("null value previously set for chunk status"), s); + Object[] backingSet = players.getBackingSet(); + for (int i = 0, len = backingSet.length; i < len; ++i) { -+ Object temp = backingSet[i]; -+ if (!(temp instanceof ServerPlayer)) { ++ if (!(backingSet[i] instanceof ServerPlayer player)) { + continue; } - - if (either == ChunkHolder.NOT_DONE_YET || either.right().isEmpty()) { - return completablefuture; -+ ServerPlayer player = (ServerPlayer)temp; + if (!this.chunkMap.playerChunkManager.isChunkSent(player, this.pos.x, this.pos.z, onlyOnWatchDistanceEdge)) { + continue; } @@ -14473,29 +14471,26 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - Set set = this.playerMap.getPlayers(chunkPos.toLong()); - Builder builder = ImmutableList.builder(); - Iterator iterator = set.iterator(); -- -- while (iterator.hasNext()) { -- ServerPlayer entityplayer = (ServerPlayer) iterator.next(); -- SectionPos sectionposition = entityplayer.getLastSectionPos(); + // Paper start - per player view distance + // there can be potential desync with player's last mapped section and the view distance map, so use the + // view distance map here. -+ List ret = new java.util.ArrayList<>(4); -+ + com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet players = this.playerChunkManager.broadcastMap.getObjectsInRange(chunkPos); + if (players == null) { -+ return ret; ++ return java.util.Collections.emptyList(); + } +- while (iterator.hasNext()) { +- ServerPlayer entityplayer = (ServerPlayer) iterator.next(); +- SectionPos sectionposition = entityplayer.getLastSectionPos(); ++ List ret = new java.util.ArrayList<>(players.size()); + - if (onlyOnWatchDistanceEdge && ChunkMap.isChunkOnRangeBorder(chunkPos.x, chunkPos.z, sectionposition.x(), sectionposition.z(), this.viewDistance) || !onlyOnWatchDistanceEdge && ChunkMap.isChunkInRange(chunkPos.x, chunkPos.z, sectionposition.x(), sectionposition.z(), this.viewDistance)) { - builder.add(entityplayer); + Object[] backingSet = players.getBackingSet(); + for (int i = 0, len = backingSet.length; i < len; ++i) { -+ Object temp = backingSet[i]; -+ if (!(temp instanceof ServerPlayer)) { ++ if (!(backingSet[i] instanceof ServerPlayer player)) { + continue; + } -+ ServerPlayer player = (ServerPlayer)temp; + if (!this.playerChunkManager.isChunkSent(player, chunkPos.x, chunkPos.z, onlyOnWatchDistanceEdge)) { + continue; }