From 1a781fc5b05fcb32d1da1e5f16a98543987ebc40 Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 18 Aug 2014 10:57:02 -0700 Subject: [PATCH] Try last ChunkState accessed in ChunkHashTable. --- .../managers/index/ChunkHashTable.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java b/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java index dbb13038..b3730ca1 100644 --- a/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java +++ b/src/main/java/com/sk89q/worldguard/protection/managers/index/ChunkHashTable.java @@ -55,6 +55,8 @@ public class ChunkHashTable implements ConcurrentRegionIndex { private LongHashTable states = new LongHashTable(); private final RegionIndex index; private final Object lock = new Object(); + @Nullable + private ChunkState lastState; /** * Create a new instance. @@ -130,6 +132,8 @@ private void rebuild() { if (!positions.isEmpty()) { executor.submit(new EnumerateRegions(positions)); } + + lastState = null; } } @@ -172,6 +176,10 @@ public void forget(Vector2D chunkPosition) { checkNotNull(chunkPosition); synchronized (lock) { states.remove(chunkPosition.getBlockX(), chunkPosition.getBlockZ()); + ChunkState state = lastState; + if (state != null && state.getPosition().getBlockX() == chunkPosition.getBlockX() && state.getPosition().getBlockZ() == chunkPosition.getBlockZ()) { + lastState = null; + } } } @@ -181,6 +189,7 @@ public void forgetAll() { executor.shutdownNow(); states = new LongHashTable(); executor = createExecutor(); + lastState = null; } } @@ -224,7 +233,14 @@ public void applyContaining(Vector position, Predicate consumer checkNotNull(position); checkNotNull(consumer); - ChunkState state = get(new Vector2D(position.getBlockX() >> 4, position.getBlockZ() >> 4), false); + ChunkState state = lastState; + int chunkX = position.getBlockX() >> 4; + int chunkZ = position.getBlockZ() >> 4; + + if (state == null || state.getPosition().getBlockX() != chunkX || state.getPosition().getBlockZ() != chunkZ) { + state = get(new Vector2D(chunkX, chunkZ), false); + } + if (state != null && state.isLoaded()) { for (ProtectedRegion region : state.getRegions()) { if (region.contains(position)) {