9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2026-01-04 15:31:43 +00:00
Files
DivineMC/divinemc-server/paper-patches/features/0011-SparklyPaper-Optimize-canSee-checks.patch
NONPLAYT 836fb16210 Update to 1.21.8
Upstream has released updates that appear to apply and compile correctly

Purpur Changes:
PurpurMC/Purpur@a39c4cb0 Updated Upstream (Paper)
PurpurMC/Purpur@ea7b18ab Updated Upstream (Paper)
PurpurMC/Purpur@0f82c210 Updated Upstream (Paper)
PurpurMC/Purpur@8de15d66 this is important for the build to not fail..
PurpurMC/Purpur@5053eb0c use a different method for dropping lapis, closes #1692
2025-07-23 15:10:35 +03:00

46 lines
3.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com>
Date: Sat, 1 Feb 2025 19:52:39 +0300
Subject: [PATCH] SparklyPaper: Optimize "canSee" checks
Original project: https://github.com/SparklyPower/SparklyPaper
Patch description:
The "canSee" checks is in a hot path, invoked by each entity for each player on the server if they are in tracking range, so optimizing it is pretty nice
First, we change the original "HashMap" to fastutil's "Object2ObjectOpenHashMap", because the containsKey throughput is better
Then, we add a "isEmpty()" check before attempting to check if the map contains something
This seems stupid, but it does seem that it improves the performance a bit, and it makes sense, "containsKey(...)" does not attempt to check the map size before attempting to check if the map contains the key
We also create a "canSee" method tailored for "ChunkMap#updatePlayer()", a method without the equals check (the "updatePlayer()" already checks if the entity is the same entity) because the CraftPlayer's `equals()` check is a *bit* expensive compared to only checking the object's identity, and because the identity has already been check, we don't need to check it twice.
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 445970cf0f7d4c9955324ae9d3ade9f5db659930..5164bdd2567d0055e7717701fa5028652759476b 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -217,7 +217,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player, PluginMessa
private static final WeakHashMap<Plugin, WeakReference<Plugin>> pluginWeakReferences = new WeakHashMap<>();
private static final net.kyori.adventure.text.Component DEFAULT_KICK_COMPONENT = net.kyori.adventure.text.Component.translatable("multiplayer.disconnect.kicked");
private final ConversationTracker conversationTracker = new ConversationTracker();
- private final Map<UUID, Set<WeakReference<Plugin>>> invertedVisibilityEntities = new HashMap<>();
+ private final Map<UUID, Set<WeakReference<Plugin>>> invertedVisibilityEntities = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(); // DivineMC - SparklyPaper: Optimize "canSee" checks
private final Set<UUID> unlistedEntities = new HashSet<>(); // Paper - Add Listing API for Player
private long firstPlayed = 0;
private long lastPlayed = 0;
@@ -2353,9 +2353,15 @@ public class CraftPlayer extends CraftHumanEntity implements Player, PluginMessa
@Override
public boolean canSee(org.bukkit.entity.Entity entity) {
- return this.equals(entity) || entity.isVisibleByDefault() ^ this.invertedVisibilityEntities.containsKey(entity.getUniqueId()); // SPIGOT-7312: Can always see self
+ return this.equals(entity) || entity.isVisibleByDefault() ^ (!invertedVisibilityEntities.isEmpty() && this.invertedVisibilityEntities.containsKey(entity.getUniqueId())); // SPIGOT-7312: Can always see self // DivineMC - SparklyPaper: Optimize "canSee" checks
}
+ // DivineMC start - SparklyPaper: Optimize "canSee" checks
+ public boolean canSeeChunkMapUpdatePlayer(org.bukkit.entity.Entity entity) {
+ return entity.isVisibleByDefault() ^ (!invertedVisibilityEntities.isEmpty() && this.invertedVisibilityEntities.containsKey(entity.getUniqueId()));
+ }
+ // DivineMC end - SparklyPaper: Optimize "canSee" checks
+
public boolean canSeePlayer(UUID uuid) {
org.bukkit.entity.Entity entity = this.getServer().getPlayer(uuid);