From fa1b56a7cf592f70808895856327bf75351cceaa Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Tue, 15 Jan 2019 13:15:34 -0200 Subject: [PATCH] use short --- .../storage/BlockConnectionStorage.java | 60 +++++++------------ 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java index 63ba9ecea..4a86dde0b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java @@ -10,10 +10,10 @@ import java.util.HashMap; import java.util.Map; public class BlockConnectionStorage extends StoredObject { - private Map> blockStorage = createLongObjectMap(); + private Map> blockStorage = createLongObjectMap(); private static Class fastUtilLongObjectHashMap; - private static Class fastUtilLongShortHashMap; + private static Class fastUtilShortShortHashMap; private static Class nettyLongObjectHashMap; static { @@ -21,19 +21,17 @@ public class BlockConnectionStorage extends StoredObject { fastUtilLongObjectHashMap = Class.forName("it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap"); Via.getPlatform().getLogger().info("Using FastUtil Long2ObjectOpenHashMap for block connections"); } catch (ClassNotFoundException ignored) { - } - try { - fastUtilLongShortHashMap = Class.forName("it.unimi.dsi.fastutil.longs.Long2ShortOpenHashMap"); - Via.getPlatform().getLogger().info("Using FastUtil Long2ShortOpenHashMap for block connections"); - } catch (ClassNotFoundException ignored) { - } - if (fastUtilLongShortHashMap == null && fastUtilLongObjectHashMap == null) { try { nettyLongObjectHashMap = Class.forName("io.netty.util.collection.LongObjectHashMap"); Via.getPlatform().getLogger().info("Using Netty LongObjectHashMap for block connections"); - } catch (ClassNotFoundException ignored) { + } catch (ClassNotFoundException ignored2) { } } + try { + fastUtilShortShortHashMap = Class.forName("it.unimi.dsi.fastutil.shorts.Short2ShortOpenHashMap"); + Via.getPlatform().getLogger().info("Using FastUtil Short2ShortOpenHashMap for block connections"); + } catch (ClassNotFoundException ignored) { + } } public BlockConnectionStorage(UserConnection user) { @@ -42,20 +40,20 @@ public class BlockConnectionStorage extends StoredObject { public void store(Position position, int blockState) { long pair = getChunkIndex(position); - Map map = getChunkMap(pair); + Map map = getChunkMap(pair); map.put(encodeBlockPos(position), (short) blockState); } public int get(Position position) { long pair = getChunkIndex(position); - Map map = getChunkMap(pair); - long blockPositon = encodeBlockPos(position); - return map.containsKey(blockPositon) ? map.get(blockPositon) : 0; + Map map = getChunkMap(pair); + short blockPosition = encodeBlockPos(position); + return map.containsKey(blockPosition) ? map.get(blockPosition) : 0; } public void remove(Position position) { long pair = getChunkIndex(position); - Map map = getChunkMap(pair); + Map map = getChunkMap(pair); map.remove(encodeBlockPos(position)); if (map.isEmpty()) { blockStorage.remove(pair); @@ -70,10 +68,10 @@ public class BlockConnectionStorage extends StoredObject { blockStorage.remove(getChunkIndex(x, z)); } - private Map getChunkMap(long index) { - Map map = blockStorage.get(index); + private Map getChunkMap(long index) { + Map map = blockStorage.get(index); if (map == null) { - map = createLongShortMap(); + map = createShortShortMap(); blockStorage.put(index, map); } return map; @@ -87,11 +85,11 @@ public class BlockConnectionStorage extends StoredObject { return getChunkIndex(position.getX().intValue(), position.getZ().intValue()); } - private long encodeBlockPos(int x, int y, int z) { - return (((long) x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF); + private short encodeBlockPos(int x, int y, int z) { + return (short) (y << 8 | x & 0xF << 4 | z & 0xF); } - private long encodeBlockPos(Position pos) { + private short encodeBlockPos(Position pos) { return encodeBlockPos(pos.getX().intValue(), pos.getY().intValue(), pos.getZ().intValue()); } @@ -113,24 +111,10 @@ public class BlockConnectionStorage extends StoredObject { return new HashMap<>(); } - private Map createLongShortMap() { - if (fastUtilLongShortHashMap != null) { + private Map createShortShortMap() { + if (fastUtilShortShortHashMap != null) { try { - return (Map) fastUtilLongShortHashMap.getConstructor().newInstance(); - } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - } - } - if (fastUtilLongObjectHashMap != null) { - try { - return (Map) fastUtilLongObjectHashMap.getConstructor().newInstance(); - } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - } - } - if (nettyLongObjectHashMap != null) { - try { - return (Map) nettyLongObjectHashMap.getConstructor().newInstance(); + return (Map) fastUtilShortShortHashMap.getConstructor().newInstance(); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); }