From 00722bc4633410ca7644b531f3a0a2172f000385 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sat, 14 Jan 2023 00:27:09 +0100 Subject: [PATCH] Reduce `/plot home` overhead (#3921) * Add JFR events * Replace global connected plots cache with local cache * Simplify isOwner check * Avoid extensive hashing of plots * Clear caches in more cases * Revert "Add JFR events" This reverts commit 78c107f1 --- .../java/com/plotsquared/core/plot/Plot.java | 41 ++++++++++--------- .../core/plot/PlotModificationManager.java | 6 ++- .../core/util/query/GlobalPlotProvider.java | 6 +-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 098b95656..bab1a3d61 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -122,9 +122,6 @@ public class Plot { private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); private static final Cleaner CLEANER = Cleaner.create(); - static Set connected_cache; - static Set regions_cache; - static { FLAG_DECIMAL_FORMAT.setMaximumFractionDigits(340); } @@ -207,6 +204,8 @@ public class Plot { */ private Plot origin; + private Set connectedCache; + /** * Constructor for a new plot. * (Only changes after plot.create() will be properly set in the database) @@ -576,7 +575,14 @@ public class Plot { return false; } final Set connected = getConnectedPlots(); - return connected.stream().anyMatch(current -> uuid.equals(current.getOwner())); + for (Plot current : connected) { + // can skip ServerPlotFlag check in getOwner() + // as flags are synchronized between plots + if (uuid.equals(current.getOwnerAbs())) { + return true; + } + } + return false; } /** @@ -1294,6 +1300,7 @@ public class Plot { DBFunc.delete(current); current.setOwnerAbs(null); current.settings = null; + current.clearCache(); for (final PlotPlayer pp : players) { this.plotListener.plotEntry(pp, current); } @@ -1864,6 +1871,7 @@ public class Plot { this.area.removePlot(this.id); this.id = plot.getId(); this.area.addPlotAbs(this); + clearCache(); DBFunc.movePlot(this, plot); TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L)); return true; @@ -2128,17 +2136,16 @@ public class Plot { this.origin.origin = base; other.origin = base; this.origin = base; - connected_cache = null; + this.connectedCache = null; } } else { if (this.origin != null) { this.origin.origin = null; this.origin = null; } - connected_cache = null; + this.connectedCache = null; } DBFunc.setMerged(this, this.getSettings().getMerged()); - regions_cache = null; } } @@ -2173,8 +2180,7 @@ public class Plot { } public void clearCache() { - connected_cache = null; - regions_cache = null; + this.connectedCache = null; if (this.origin != null) { this.origin.origin = null; this.origin = null; @@ -2301,10 +2307,9 @@ public class Plot { if (!this.isMerged()) { return Collections.singleton(this); } - if (connected_cache != null && connected_cache.contains(this)) { - return connected_cache; + if (this.connectedCache != null && this.connectedCache.contains(this)) { + return this.connectedCache; } - regions_cache = null; HashSet tmpSet = new HashSet<>(); tmpSet.add(this); @@ -2409,7 +2414,7 @@ public class Plot { } } } - connected_cache = tmpSet; + this.connectedCache = tmpSet; return tmpSet; } @@ -2421,19 +2426,15 @@ public class Plot { * @return all regions within the plot */ public @NonNull Set getRegions() { - if (regions_cache != null && connected_cache != null && connected_cache.contains(this)) { - return regions_cache; - } if (!this.isMerged()) { Location pos1 = this.getBottomAbs().withY(getArea().getMinBuildHeight()); Location pos2 = this.getTopAbs().withY(getArea().getMaxBuildHeight()); - connected_cache = Sets.newHashSet(this); + this.connectedCache = Sets.newHashSet(this); CuboidRegion rg = new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()); - regions_cache = Collections.singleton(rg); - return regions_cache; + return Collections.singleton(rg); } Set plots = this.getConnectedPlots(); - Set regions = regions_cache = new HashSet<>(); + Set regions = new HashSet<>(); Set visited = new HashSet<>(); for (Plot current : plots) { if (visited.contains(current.getId())) { diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java b/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java index 61658391f..cfab430b8 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java @@ -263,6 +263,7 @@ public final class PlotModificationManager { return; } Plot current = queue.poll(); + current.clearCache(); if (plot.getArea().getTerrain() != PlotAreaTerrainType.NONE) { try { PlotSquared.platform().regionManager().regenerateRegion( @@ -327,6 +328,7 @@ public final class PlotModificationManager { ArrayList ids = new ArrayList<>(plots.size()); for (Plot current : plots) { current.setHome(null); + current.clearCache(); ids.add(current.getId()); } this.plot.clearRatings(); @@ -478,8 +480,7 @@ public final class PlotModificationManager { this.plot.updateWorldBorder(); } } - Plot.connected_cache = null; - Plot.regions_cache = null; + this.plot.clearCache(); this.plot.getTrusted().clear(); this.plot.getMembers().clear(); this.plot.getDenied().clear(); @@ -630,6 +631,7 @@ public final class PlotModificationManager { if (queue.size() > 0) { queue.enqueue(); } + visited.forEach(Plot::clearCache); return toReturn; } diff --git a/Core/src/main/java/com/plotsquared/core/util/query/GlobalPlotProvider.java b/Core/src/main/java/com/plotsquared/core/util/query/GlobalPlotProvider.java index 2e635101e..e02994e63 100644 --- a/Core/src/main/java/com/plotsquared/core/util/query/GlobalPlotProvider.java +++ b/Core/src/main/java/com/plotsquared/core/util/query/GlobalPlotProvider.java @@ -23,9 +23,9 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.world.PlotAreaManager; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; -import java.util.Set; +import java.util.List; class GlobalPlotProvider implements PlotProvider { @@ -37,7 +37,7 @@ class GlobalPlotProvider implements PlotProvider { @Override public Collection getPlots() { - final Set plots = new HashSet<>(); + final List plots = new ArrayList<>(); for (final PlotArea plotArea : this.plotAreaManager.getAllPlotAreas()) { plots.addAll(plotArea.getPlots()); }