From d6bc85ccbe1cba25a516e2cefdbe7fdd16773683 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sat, 16 Feb 2019 12:46:10 +1000 Subject: [PATCH] Speed up the BlockState hashCode method by caching (As it's Immutable). Allows some better optimisations in the future by using them as map keys --- .../java/com/sk89q/worldedit/world/block/BlockState.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index a67235d0c..6e4c5e721 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -146,7 +146,7 @@ public class BlockState implements BlockStateHolder { @Override public boolean equalsFuzzy(BlockStateHolder o) { if (this == o) { - // Added a reference equality check for + // Added a reference equality check for speediness return true; } if (!getBlockType().equals(o.getBlockType())) { @@ -223,8 +223,13 @@ public class BlockState implements BlockStateHolder { return equalsFuzzy((BlockState) obj); } + private Integer hashCodeCache = null; + @Override public int hashCode() { - return Objects.hash(blockType, values); + if (hashCodeCache == null) { + hashCodeCache = Objects.hash(blockType, values); + } + return hashCodeCache; } }