From d43eb3bc346de7b180cc01dea086677bb8704a4e Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 17 Aug 2014 19:08:20 -0700 Subject: [PATCH] Optimize the query cache a bit. --- .../com/sk89q/worldguard/bukkit/QueryCache.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/QueryCache.java b/src/main/java/com/sk89q/worldguard/bukkit/QueryCache.java index 2f969deb..b9c3fd99 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/QueryCache.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/QueryCache.java @@ -37,7 +37,7 @@ */ class QueryCache { - private final ConcurrentMap cache = new ConcurrentHashMap(); + private final ConcurrentMap cache = new ConcurrentHashMap(16, 0.75f, 2); /** * Get from the cache a {@code ApplicableRegionSet} if an entry exists; @@ -76,12 +76,20 @@ private static class CacheKey { private final int x; private final int y; private final int z; + private final int hashCode; private CacheKey(Location location) { this.world = location.getWorld(); this.x = location.getBlockX(); this.y = location.getBlockY(); this.z = location.getBlockZ(); + + // Pre-compute hash code + int result = world.hashCode(); + result = 31 * result + x; + result = 31 * result + y; + result = 31 * result + z; + this.hashCode = result; } @Override @@ -101,11 +109,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - int result = world.hashCode(); - result = 31 * result + x; - result = 31 * result + y; - result = 31 * result + z; - return result; + return hashCode; } }