From b89e6264bba11c42fbad056018fd4833cdd4cbd9 Mon Sep 17 00:00:00 2001 From: filoghost Date: Mon, 31 Jan 2022 23:41:28 +0100 Subject: [PATCH] Optimization and memory leak fix --- .../plugin/HolographicDisplays.java | 17 ++++---- .../hologram/tracking/ItemLineTracker.java | 1 + .../plugin/hologram/tracking/LineTracker.java | 1 + .../hologram/tracking/LineTrackerManager.java | 1 + .../tracking/PositionBasedLineTracker.java | 7 ++-- .../hologram/tracking/TextLineTracker.java | 1 + .../hologram/tracking/TextLineViewer.java | 1 + .../plugin/hologram/tracking/Viewer.java | 1 + .../plugin/listener/PlayerListener.java | 12 +++--- .../tracking => tick}/CachedPlayer.java | 4 +- .../plugin/tick/TickingTask.java | 41 ++++++++++--------- 11 files changed, 51 insertions(+), 36 deletions(-) rename plugin/src/main/java/me/filoghost/holographicdisplays/plugin/{hologram/tracking => tick}/CachedPlayer.java (90%) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java index e37950bb..2ebec423 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java @@ -129,16 +129,16 @@ public class HolographicDisplays extends FCommonsPlugin { internalHologramEditor = new InternalHologramEditor(internalHologramManager, configManager); new HologramCommandManager(this, internalHologramEditor).register(this); - // Listeners - registerListener(new PlayerListener(nmsManager, lineTrackerManager, lineClickListener)); - registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager)); - UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener(); - registerListener(updateNotificationListener); - // Tasks TickingTask tickingTask = new TickingTask(tickClock, placeholderTracker, lineTrackerManager, lineClickListener); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, tickingTask, 0, 1); - updateNotificationListener.runAsyncUpdateCheck(this); + + // Listeners + PlayerListener playerListener = new PlayerListener(nmsManager, lineClickListener, tickingTask); + registerListener(playerListener); + registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager)); + UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener(); + registerListener(updateNotificationListener); // Enable the APIs HolographicDisplaysAPIProvider.setImplementation( @@ -157,6 +157,9 @@ public class HolographicDisplays extends FCommonsPlugin { errorCollector.logToConsole(); Bukkit.getScheduler().runTaskLater(this, errorCollector::logSummaryToConsole, 10L); } + + // Run the update checker + updateNotificationListener.runAsyncUpdateCheck(this); } @SuppressWarnings("deprecation") diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/ItemLineTracker.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/ItemLineTracker.java index 7fe0a71b..593295f9 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/ItemLineTracker.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/ItemLineTracker.java @@ -9,6 +9,7 @@ import me.filoghost.holographicdisplays.nms.common.NMSManager; import me.filoghost.holographicdisplays.nms.common.entity.ItemNMSPacketEntity; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLine; import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.MustBeInvokedByOverriders; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTracker.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTracker.java index 66b88f21..ac11523f 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTracker.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTracker.java @@ -6,6 +6,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLine; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.bukkit.entity.Player; import org.jetbrains.annotations.MustBeInvokedByOverriders; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTrackerManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTrackerManager.java index b71b873e..9772687b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTrackerManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/LineTrackerManager.java @@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemHologramLin import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLine; import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.bukkit.entity.Player; import java.util.Collection; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/PositionBasedLineTracker.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/PositionBasedLineTracker.java index 1748b60e..5925932b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/PositionBasedLineTracker.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/PositionBasedLineTracker.java @@ -6,6 +6,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking; import me.filoghost.holographicdisplays.common.PositionCoordinates; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.bukkit.Location; import org.jetbrains.annotations.MustBeInvokedByOverriders; @@ -35,8 +36,8 @@ abstract class PositionBasedLineTracker extends LineTracker } @Override - protected final boolean shouldTrackPlayer(CachedPlayer cachedPlayer) { - Location playerLocation = cachedPlayer.getLocation(); + protected final boolean shouldTrackPlayer(CachedPlayer player) { + Location playerLocation = player.getLocation(); if (playerLocation.getWorld() != getLine().getWorldIfLoaded()) { return false; } @@ -46,7 +47,7 @@ abstract class PositionBasedLineTracker extends LineTracker return diffX <= (double) ENTITY_VIEW_RANGE && diffZ <= (double) ENTITY_VIEW_RANGE - && getLine().isVisibleTo(cachedPlayer.getBukkitPlayer()); + && getLine().isVisibleTo(player.getBukkitPlayer()); } @MustBeInvokedByOverriders diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java index a6344736..8bd805b3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java @@ -11,6 +11,7 @@ import me.filoghost.holographicdisplays.nms.common.entity.TextNMSPacketEntity; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextHologramLine; import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.jetbrains.annotations.MustBeInvokedByOverriders; import java.util.Objects; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineViewer.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineViewer.java index 3fa5d561..53d77481 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineViewer.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineViewer.java @@ -6,6 +6,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking; import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.jetbrains.annotations.Nullable; import java.util.Objects; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/Viewer.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/Viewer.java index 35e335ef..3c9e14ba 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/Viewer.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/Viewer.java @@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking; import me.filoghost.holographicdisplays.nms.common.IndividualTextPacketGroup; import me.filoghost.holographicdisplays.nms.common.PacketGroup; +import me.filoghost.holographicdisplays.plugin.tick.CachedPlayer; import org.bukkit.Location; import org.bukkit.entity.Player; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/PlayerListener.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/PlayerListener.java index 94e008a6..4d629714 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/PlayerListener.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/PlayerListener.java @@ -6,7 +6,7 @@ package me.filoghost.holographicdisplays.plugin.listener; import me.filoghost.holographicdisplays.nms.common.NMSManager; -import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; +import me.filoghost.holographicdisplays.plugin.tick.TickingTask; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -16,25 +16,27 @@ import org.bukkit.event.player.PlayerQuitEvent; public class PlayerListener implements Listener { private final NMSManager nmsManager; - private final LineTrackerManager lineTrackerManager; private final LineClickListener lineClickListener; + private final TickingTask tickingTask; - public PlayerListener(NMSManager nmsManager, LineTrackerManager lineTrackerManager, LineClickListener lineClickListener) { + public PlayerListener(NMSManager nmsManager, LineClickListener lineClickListener, TickingTask tickingTask) { this.nmsManager = nmsManager; - this.lineTrackerManager = lineTrackerManager; this.lineClickListener = lineClickListener; + this.tickingTask = tickingTask; } @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); + tickingTask.onPlayerJoin(player); nmsManager.injectPacketListener(player, lineClickListener); } + @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); - lineTrackerManager.onPlayerQuit(player); + tickingTask.onPlayerQuit(player); nmsManager.uninjectPacketListener(player); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/CachedPlayer.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/CachedPlayer.java similarity index 90% rename from plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/CachedPlayer.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/CachedPlayer.java index 9e6d2357..3630030d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/CachedPlayer.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/CachedPlayer.java @@ -3,9 +3,8 @@ * * SPDX-License-Identifier: GPL-3.0-or-later */ -package me.filoghost.holographicdisplays.plugin.hologram.tracking; +package me.filoghost.holographicdisplays.plugin.tick; -import me.filoghost.holographicdisplays.plugin.tick.TickClock; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -28,6 +27,7 @@ public class CachedPlayer { } public Location getLocation() { + // Avoid creating a new object on each invocation long currentTick = tickClock.getCurrentTick(); if (locationUpdateTick != currentTick) { location = player.getLocation(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/TickingTask.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/TickingTask.java index b4237695..e3fd6f27 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/TickingTask.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/tick/TickingTask.java @@ -6,16 +6,14 @@ package me.filoghost.holographicdisplays.plugin.tick; import me.filoghost.fcommons.logging.Log; -import me.filoghost.holographicdisplays.plugin.hologram.tracking.CachedPlayer; import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; import me.filoghost.holographicdisplays.plugin.placeholder.tracking.ActivePlaceholderTracker; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.Collection; -import java.util.WeakHashMap; +import java.util.Iterator; +import java.util.List; public class TickingTask implements Runnable { @@ -23,7 +21,7 @@ public class TickingTask implements Runnable { private final ActivePlaceholderTracker placeholderTracker; private final LineTrackerManager lineTrackerManager; private final LineClickListener lineClickListener; - private final WeakHashMap cachedPlayersMap; + private final List onlinePlayers; private long lastErrorLogTick; @@ -36,7 +34,24 @@ public class TickingTask implements Runnable { this.placeholderTracker = placeholderTracker; this.lineTrackerManager = lineTrackerManager; this.lineClickListener = lineClickListener; - this.cachedPlayersMap = new WeakHashMap<>(); + this.onlinePlayers = new ArrayList<>(); + } + + public void onPlayerJoin(Player player) { + onlinePlayers.add(new CachedPlayer(player, tickClock)); + } + + public void onPlayerQuit(Player player) { + Iterator iterator = onlinePlayers.iterator(); + + while (iterator.hasNext()) { + if (iterator.next().getBukkitPlayer() == player) { + iterator.remove(); + break; + } + } + + lineTrackerManager.onPlayerQuit(player); } @Override @@ -47,7 +62,7 @@ public class TickingTask implements Runnable { placeholderTracker.clearOutdatedEntries(); try { - lineTrackerManager.update(getOnlineCachedPlayers()); + lineTrackerManager.update(onlinePlayers); } catch (Throwable t) { // Catch all types of Throwable because we're using NMS code if (tickClock.getCurrentTick() - lastErrorLogTick >= 20) { @@ -63,16 +78,4 @@ public class TickingTask implements Runnable { lineClickListener.processQueuedClickEvents(); } - private Collection getOnlineCachedPlayers() { - Collection bukkitPlayers = Bukkit.getOnlinePlayers(); - Collection cachedPlayers = new ArrayList<>(bukkitPlayers.size()); - - for (Player bukkitPlayer : bukkitPlayers) { - CachedPlayer cachedPlayer = cachedPlayersMap.computeIfAbsent(bukkitPlayer, key -> new CachedPlayer(key, tickClock)); - cachedPlayers.add(cachedPlayer); - } - - return cachedPlayers; - } - }