From e78878c8cc17c8e5f05b08af4996e1e16b99a6cd Mon Sep 17 00:00:00 2001 From: Sen Date: Mon, 19 May 2025 18:03:59 +0200 Subject: [PATCH] remove long chunk mapping --- client/src/client/world/WorldClient.java | 16 +- common/src/common/util/LongHashMap.java | 285 ------------------ server/src/server/world/WorldServer.java | 74 +++-- .../src/server/worldgen/BiomeGenLayered.java | 15 +- .../worldgen/structure/MapGenStructure.java | 10 +- 5 files changed, 58 insertions(+), 342 deletions(-) delete mode 100755 common/src/common/util/LongHashMap.java diff --git a/client/src/client/world/WorldClient.java b/client/src/client/world/WorldClient.java index 5986b9b..ab87696 100755 --- a/client/src/client/world/WorldClient.java +++ b/client/src/client/world/WorldClient.java @@ -1,6 +1,7 @@ package client.world; import java.util.List; +import java.util.Map; import java.util.Set; import client.Client; @@ -9,6 +10,7 @@ import client.renderer.particle.EntityFirework; import common.biome.Biome; import common.block.Block; import common.collect.Lists; +import common.collect.Maps; import common.collect.Sets; import common.dimension.Dimension; import common.entity.Entity; @@ -30,7 +32,6 @@ import common.tileentity.TileEntity; import common.util.BlockPos; import common.util.ChunkPos; import common.util.ExtMath; -import common.util.LongHashMap; import common.util.Vec3; import common.util.BlockPos.MutableBlockPos; import common.world.Chunk; @@ -45,7 +46,7 @@ public class WorldClient extends AWorldClient private final Set entityList = Sets.newHashSet(); private final Set spawnQueue = Sets.newHashSet(); private final Set previousActive = Sets.newHashSet(); - private final LongHashMap chunkMapping = new LongHashMap(); + private final Map chunkMapping = Maps.newHashMap(); private final List chunkListing = Lists.newArrayList(); private final Chunk blankChunk = new EmptyChunk(this); // public final Profiler profiler; @@ -127,12 +128,13 @@ public class WorldClient extends AWorldClient public void doPreChunk(int x, int z, boolean load) { + ChunkPos pos = new ChunkPos(x, z); if (load) { - if(this.chunkMapping.getValueByKey(LongHashMap.packInt(x, z)) != null) + if(this.chunkMapping.get(pos) != null) this.doPreChunk(x, z, false); Chunk chunk = new Chunk(this, x, z); - this.chunkMapping.add(LongHashMap.packInt(x, z), chunk); + this.chunkMapping.put(pos, chunk); this.chunkListing.add(chunk); chunk.setLoaded(true); } @@ -143,7 +145,7 @@ public class WorldClient extends AWorldClient { chunk.onChunkUnload(); } - this.chunkMapping.remove(LongHashMap.packInt(x, z)); + this.chunkMapping.remove(pos); this.chunkListing.remove(chunk); } @@ -353,13 +355,13 @@ public class WorldClient extends AWorldClient public Chunk getChunk(int x, int z) { - Chunk chunk = this.chunkMapping.getValueByKey(LongHashMap.packInt(x, z)); + Chunk chunk = this.chunkMapping.get(new ChunkPos(x, z)); return chunk == null ? this.blankChunk : chunk; } public String getInfo() { - return "Chunk-Cache: M " + this.chunkMapping.getNumHashElements() + ", L " + this.chunkListing.size(); + return "Chunk-Cache: M " + this.chunkMapping.size() + ", L " + this.chunkListing.size(); } public void playSound(SoundEvent sound, double x, double y, double z, float volume) diff --git a/common/src/common/util/LongHashMap.java b/common/src/common/util/LongHashMap.java deleted file mode 100755 index 89aba64..0000000 --- a/common/src/common/util/LongHashMap.java +++ /dev/null @@ -1,285 +0,0 @@ -package common.util; - -public class LongHashMap -{ - private transient LongHashMap.Entry[] hashArray = new LongHashMap.Entry[4096]; - private transient int numHashElements; - private int mask; - private int capacity = 3072; - private final float percentUseable = 0.75F; - private transient volatile int modCount; - - public static long packInt(int x, int z) { - return (long)x & 4294967295L | ((long)z & 4294967295L) << 32; - } - - public LongHashMap() - { - this.mask = this.hashArray.length - 1; - } - - /** - * returns the hashed key given the original key - */ - private static int getHashedKey(long originalKey) - { - return hash((int)(originalKey ^ originalKey >>> 32)); - } - - /** - * the hash function - */ - private static int hash(int integer) - { - integer = integer ^ integer >>> 20 ^ integer >>> 12; - return integer ^ integer >>> 7 ^ integer >>> 4; - } - - /** - * gets the index in the hash given the array length and the hashed key - */ - private static int getHashIndex(int p_76158_0_, int p_76158_1_) - { - return p_76158_0_ & p_76158_1_; - } - - public int getNumHashElements() - { - return this.numHashElements; - } - - /** - * get the value from the map given the key - */ - public V getValueByKey(long p_76164_1_) - { - int i = getHashedKey(p_76164_1_); - - for (LongHashMap.Entry entry = this.hashArray[getHashIndex(i, this.mask)]; entry != null; entry = entry.nextEntry) - { - if (entry.key == p_76164_1_) - { - return entry.value; - } - } - - return (V)null; - } - - public boolean containsItem(long p_76161_1_) - { - return this.getEntry(p_76161_1_) != null; - } - - final LongHashMap.Entry getEntry(long p_76160_1_) - { - int i = getHashedKey(p_76160_1_); - - for (LongHashMap.Entry entry = this.hashArray[getHashIndex(i, this.mask)]; entry != null; entry = entry.nextEntry) - { - if (entry.key == p_76160_1_) - { - return entry; - } - } - - return null; - } - - /** - * Add a key-value pair. - */ - public void add(long p_76163_1_, V p_76163_3_) - { - int i = getHashedKey(p_76163_1_); - int j = getHashIndex(i, this.mask); - - for (LongHashMap.Entry entry = this.hashArray[j]; entry != null; entry = entry.nextEntry) - { - if (entry.key == p_76163_1_) - { - entry.value = p_76163_3_; - return; - } - } - - ++this.modCount; - this.createKey(i, p_76163_1_, p_76163_3_, j); - } - - /** - * resizes the table - */ - private void resizeTable(int p_76153_1_) - { - LongHashMap.Entry[] entry = this.hashArray; - int i = entry.length; - - if (i == 1073741824) - { - this.capacity = Integer.MAX_VALUE; - } - else - { - LongHashMap.Entry[] entry1 = new LongHashMap.Entry[p_76153_1_]; - this.copyHashTableTo(entry1); - this.hashArray = entry1; - this.mask = this.hashArray.length - 1; - this.capacity = (int)((float)p_76153_1_ * this.percentUseable); - } - } - - /** - * copies the hash table to the specified array - */ - private void copyHashTableTo(LongHashMap.Entry[] p_76154_1_) - { - LongHashMap.Entry[] entry = this.hashArray; - int i = p_76154_1_.length; - - for (int j = 0; j < entry.length; ++j) - { - LongHashMap.Entry entry1 = entry[j]; - - if (entry1 != null) - { - entry[j] = null; - - while (true) - { - LongHashMap.Entry entry2 = entry1.nextEntry; - int k = getHashIndex(entry1.hash, i - 1); - entry1.nextEntry = p_76154_1_[k]; - p_76154_1_[k] = entry1; - entry1 = entry2; - - if (entry2 == null) - { - break; - } - } - } - } - } - - /** - * calls the removeKey method and returns removed object - */ - public V remove(long p_76159_1_) - { - LongHashMap.Entry entry = this.removeKey(p_76159_1_); - return (V)(entry == null ? null : entry.value); - } - - final LongHashMap.Entry removeKey(long p_76152_1_) - { - int i = getHashedKey(p_76152_1_); - int j = getHashIndex(i, this.mask); - LongHashMap.Entry entry = this.hashArray[j]; - LongHashMap.Entry entry1; - LongHashMap.Entry entry2; - - for (entry1 = entry; entry1 != null; entry1 = entry2) - { - entry2 = entry1.nextEntry; - - if (entry1.key == p_76152_1_) - { - ++this.modCount; - --this.numHashElements; - - if (entry == entry1) - { - this.hashArray[j] = entry2; - } - else - { - entry.nextEntry = entry2; - } - - return entry1; - } - - entry = entry1; - } - - return entry1; - } - - /** - * creates the key in the hash table - */ - private void createKey(int p_76156_1_, long p_76156_2_, V p_76156_4_, int p_76156_5_) - { - LongHashMap.Entry entry = this.hashArray[p_76156_5_]; - this.hashArray[p_76156_5_] = new LongHashMap.Entry(p_76156_1_, p_76156_2_, p_76156_4_, entry); - - if (this.numHashElements++ >= this.capacity) - { - this.resizeTable(2 * this.hashArray.length); - } - } - - static class Entry - { - final long key; - V value; - LongHashMap.Entry nextEntry; - final int hash; - - Entry(int p_i1553_1_, long p_i1553_2_, V p_i1553_4_, LongHashMap.Entry p_i1553_5_) - { - this.value = p_i1553_4_; - this.nextEntry = p_i1553_5_; - this.key = p_i1553_2_; - this.hash = p_i1553_1_; - } - - public final long getKey() - { - return this.key; - } - - public final V getValue() - { - return this.value; - } - - public final boolean equals(Object p_equals_1_) - { - if (!(p_equals_1_ instanceof LongHashMap.Entry)) - { - return false; - } - else - { - LongHashMap.Entry entry = (LongHashMap.Entry)p_equals_1_; - Object object = Long.valueOf(this.getKey()); - Object object1 = Long.valueOf(entry.getKey()); - - if (object == object1 || object != null && object.equals(object1)) - { - Object object2 = this.getValue(); - Object object3 = entry.getValue(); - - if (object2 == object3 || object2 != null && object2.equals(object3)) - { - return true; - } - } - - return false; - } - } - - public final int hashCode() - { - return LongHashMap.getHashedKey(this.key); - } - - public final String toString() - { - return this.getKey() + "=" + this.getValue(); - } - } -} diff --git a/server/src/server/world/WorldServer.java b/server/src/server/world/WorldServer.java index ae195e8..2282cfc 100755 --- a/server/src/server/world/WorldServer.java +++ b/server/src/server/world/WorldServer.java @@ -66,7 +66,6 @@ import common.util.ChunkPos; import common.util.ExtMath; import common.util.FileUtils; import common.util.IntHashMap; -import common.util.LongHashMap; import common.util.NextTickListEntry; import common.util.PortalType; import common.util.Position; @@ -130,15 +129,15 @@ public final class WorldServer extends AWorldServer { private final TreeSet ticksNext = new TreeSet(); private final EventList[] queue = new EventList[] {new EventList(), new EventList()}; private final List ticksNow = Lists.newArrayList(); - private final Set dropped = Collections.newSetFromMap(new ConcurrentHashMap()); - private final LongHashMap chunks = new LongHashMap(); + private final Set dropped = Collections.newSetFromMap(new ConcurrentHashMap()); + private final Map chunks = Maps.newHashMap(); private final List loaded = Lists.newArrayList(); private final Map toRemove = new ConcurrentHashMap(); private final Set pending = Collections.newSetFromMap(new ConcurrentHashMap()); - private final LongHashMap loaders = new LongHashMap(); + private final Map loaders = Maps.newHashMap(); private final Set loaderList = Sets.newHashSet(); private final List managed = Lists.newArrayList(); - private final LongHashMap instances = new LongHashMap(); + private final Map instances = Maps.newHashMap(); private final List toUpdate = Lists.newArrayList(); private final List instList = Lists.newArrayList(); private final Set tracked = Sets.newHashSet(); @@ -512,12 +511,12 @@ public final class WorldServer extends AWorldServer { if(!this.debug) { for(int i = 0; i < 100; ++i) { if(!this.dropped.isEmpty()) { - Long v = (Long)this.dropped.iterator().next(); - Chunk chunk = (Chunk)this.chunks.getValueByKey(v.longValue()); + ChunkPos v = this.dropped.iterator().next(); + Chunk chunk = this.chunks.get(v); if(chunk != null) { chunk.onChunkUnload(); this.saveChunkData(chunk); - this.chunks.remove(v.longValue()); + this.chunks.remove(v); this.loaded.remove(chunk); } this.dropped.remove(v); @@ -570,10 +569,10 @@ public final class WorldServer extends AWorldServer { } public boolean addLoader(BlockPos pos) { - long chunk = LongHashMap.packInt(pos.getX() / 16, pos.getZ() / 16); - if(this.loaders.containsItem(chunk)) + ChunkPos chunk = new ChunkPos(pos.getX() / 16, pos.getZ() / 16); + if(this.loaders.containsKey(chunk)) return false; - this.loaders.add(chunk, pos); + this.loaders.put(chunk, pos); this.loaderList.add(pos); this.loadersModified = true; return true; @@ -584,8 +583,8 @@ public final class WorldServer extends AWorldServer { // } public boolean removeLoader(BlockPos pos) { - long chunk = LongHashMap.packInt(pos.getX() / 16, pos.getZ() / 16); - BlockPos loader = this.loaders.getValueByKey(chunk); + ChunkPos chunk = new ChunkPos(pos.getX() / 16, pos.getZ() / 16); + BlockPos loader = this.loaders.get(chunk); if(!pos.equals(loader)) return false; this.loaders.remove(chunk); @@ -1325,18 +1324,18 @@ public final class WorldServer extends AWorldServer { } public Chunk getChunk(int x, int z) { - Chunk chunk = this.chunks.getValueByKey(LongHashMap.packInt(x, z)); + Chunk chunk = this.chunks.get(new ChunkPos(x, z)); return chunk == null ? this.loadChunk(x, z) : chunk; } private boolean chunkExists(int x, int z) { - return this.chunks.containsItem(LongHashMap.packInt(x, z)); + return this.chunks.containsKey(new ChunkPos(x, z)); } public void dropChunk(int x, int z) { - long chunk = LongHashMap.packInt(x, z); - if(!this.loaders.containsItem(chunk)) - this.dropped.add(Long.valueOf(chunk)); + ChunkPos chunk = new ChunkPos(x, z); + if(!this.loaders.containsKey(chunk)) + this.dropped.add(chunk); } public void loadForcedChunks() { @@ -1352,9 +1351,9 @@ public final class WorldServer extends AWorldServer { } public Chunk loadChunk(int x, int z) { - long id = LongHashMap.packInt(x, z); - this.dropped.remove(Long.valueOf(id)); - Chunk chunk = (Chunk)this.chunks.getValueByKey(id); + ChunkPos id = new ChunkPos(x, z); + this.dropped.remove(id); + Chunk chunk = this.chunks.get(id); if(chunk == null) { if(!this.debug) @@ -1364,7 +1363,7 @@ public final class WorldServer extends AWorldServer { chunk = this.generate(x, z); } - this.chunks.add(id, chunk); + this.chunks.put(id, chunk); this.loaded.add(chunk); chunk.onChunkLoad(); this.popChunk(x, z); @@ -1652,7 +1651,7 @@ public final class WorldServer extends AWorldServer { if(!this.loaderList.isEmpty()) this.loadersModified = true; for(BlockPos pos : this.loaderList) { - this.loaders.remove(LongHashMap.packInt(pos.getX() / 16, pos.getZ() / 16)); + this.loaders.remove(new ChunkPos(pos.getX() / 16, pos.getZ() / 16)); } this.loaderList.clear(); for(Iterator> iter = this.server.getWarps().entrySet().iterator(); iter.hasNext();) { @@ -1668,11 +1667,11 @@ public final class WorldServer extends AWorldServer { })); this.exterminated = true; // this.dataModified = true; - for(Long v : this.dropped) { - Chunk chunk = this.chunks.getValueByKey(v.longValue()); + for(ChunkPos v : this.dropped) { + Chunk chunk = this.chunks.get(v); if(chunk != null) { chunk.onChunkUnload(); - this.chunks.remove(v.longValue()); + this.chunks.remove(v); this.loaded.remove(chunk); } } @@ -1681,11 +1680,11 @@ public final class WorldServer extends AWorldServer { this.loaded.clear(); this.setExterminatedGen(); for(Chunk chunk : loaded) { - long pos = LongHashMap.packInt(chunk.xPos, chunk.zPos); + ChunkPos pos = new ChunkPos(chunk.xPos, chunk.zPos); chunk.onChunkUnload(); this.chunks.remove(pos); chunk = this.generate(chunk.xPos, chunk.zPos); - this.chunks.add(pos, chunk); + this.chunks.put(pos, chunk); this.loaded.add(chunk); chunk.onChunkLoad(); chunk.setTerrainPopulated(true); @@ -1764,7 +1763,7 @@ public final class WorldServer extends AWorldServer { Object[] worlds = server.getWorlds().toArray(); for(Object obj : worlds) { WorldServer world = (WorldServer)obj; - chunks += world.chunks.getNumHashElements(); + chunks += world.chunks.size(); entities += world.entities.size(); tiles += world.tiles.size(); ticked += world.tickable.size(); @@ -1864,13 +1863,13 @@ public final class WorldServer extends AWorldServer { } private boolean hasPlayerInstance(int chunkX, int chunkZ) { - long v = (long)chunkX + 2147483647L | (long)chunkZ + 2147483647L << 32; - return this.instances.getValueByKey(v) != null; + ChunkPos v = new ChunkPos(chunkX, chunkZ); + return this.instances.get(v) != null; } public boolean updateBiomes(int chunkX, int chunkZ) { - long v = (long)chunkX + 2147483647L | (long)chunkZ + 2147483647L << 32; - PlayerInstance ins = this.instances.getValueByKey(v); + ChunkPos v = new ChunkPos(chunkX, chunkZ); + PlayerInstance ins = this.instances.get(v); if(ins == null) return false; Chunk chunk = this.getChunk(chunkX, chunkZ); @@ -1880,12 +1879,12 @@ public final class WorldServer extends AWorldServer { } private PlayerInstance getPlayerInstance(int chunkX, int chunkZ, boolean create) { - long v = (long)chunkX + 2147483647L | (long)chunkZ + 2147483647L << 32; - PlayerInstance inst = this.instances.getValueByKey(v); + ChunkPos v = new ChunkPos(chunkX, chunkZ); + PlayerInstance inst = this.instances.get(v); if(inst == null && create) { inst = new PlayerInstance(chunkX, chunkZ); - this.instances.add(v, inst); + this.instances.put(v, inst); this.instList.add(inst); } @@ -2837,9 +2836,8 @@ public final class WorldServer extends AWorldServer { player.connection.getLoadedChunkList().remove(this.position); if(this.watching.isEmpty()) { - long v = (long)this.position.x + 2147483647L | (long)this.position.z + 2147483647L << 32; this.increaseInhabitedTime(chunk); - WorldServer.this.instances.remove(v); + WorldServer.this.instances.remove(this.position); WorldServer.this.instList.remove(this); if(this.updates > 0) { diff --git a/server/src/server/worldgen/BiomeGenLayered.java b/server/src/server/worldgen/BiomeGenLayered.java index bdf9230..b2d892d 100755 --- a/server/src/server/worldgen/BiomeGenLayered.java +++ b/server/src/server/worldgen/BiomeGenLayered.java @@ -1,12 +1,14 @@ package server.worldgen; import java.util.List; +import java.util.Map; import java.util.Set; import common.biome.Biome; import common.collect.Lists; +import common.collect.Maps; import common.util.BlockPos; -import common.util.LongHashMap; +import common.util.ChunkPos; import common.worldgen.BiomeGenerator; import server.worldgen.layer.GenLayer; import server.worldgen.layer.GenLayerAddAreas; @@ -54,7 +56,7 @@ public class BiomeGenLayered implements BiomeGenerator { private final GenLayer genBiomes; private final GenLayer biomeIndexLayer; - private final LongHashMap cacheMap = new LongHashMap(); + private final Map cacheMap = Maps.newHashMap(); private final List cache = Lists.newArrayList(); private long lastCleanupTime; @@ -125,13 +127,13 @@ public class BiomeGenLayered implements BiomeGenerator { { x = x >> 4; z = z >> 4; - long i = LongHashMap.packInt(x, z); // (long)x & 4294967295L | ((long)z & 4294967295L) << 32; - CacheBlock blk = this.cacheMap.getValueByKey(i); + ChunkPos pos = new ChunkPos(x, z); + CacheBlock blk = this.cacheMap.get(pos); if (blk == null) { blk = new CacheBlock(x, z); - this.cacheMap.add(i, blk); + this.cacheMap.put(pos, blk); this.cache.add(blk); } @@ -156,8 +158,7 @@ public class BiomeGenLayered implements BiomeGenerator { if (l > 30000L || l < 0L) { this.cache.remove(k--); - long i1 = LongHashMap.packInt(blk.xPosition, blk.zPosition) ; // (long)biomecache$block.xPosition & 4294967295L | ((long)biomecache$block.zPosition & 4294967295L) << 32; - this.cacheMap.remove(i1); + this.cacheMap.remove(new ChunkPos(blk.xPosition, blk.zPosition)); } } } diff --git a/server/src/server/worldgen/structure/MapGenStructure.java b/server/src/server/worldgen/structure/MapGenStructure.java index dc911fe..ab8c362 100755 --- a/server/src/server/worldgen/structure/MapGenStructure.java +++ b/server/src/server/worldgen/structure/MapGenStructure.java @@ -9,7 +9,6 @@ import common.nbt.NBTTagCompound; import common.rng.Random; import common.util.BlockPos; import common.util.ChunkPos; -import common.util.LongHashMap; import common.world.World; import server.world.WorldServer; import server.world.WorldServer.WorldSavedData; @@ -19,7 +18,7 @@ import server.worldgen.caves.MapGenBase; public abstract class MapGenStructure extends MapGenBase { private WorldSavedData structureData; - protected Map structureMap = Maps.newHashMap(); + protected Map structureMap = Maps.newHashMap(); public abstract String getStructureName(); @@ -34,14 +33,15 @@ public abstract class MapGenStructure extends MapGenBase return; this.initializeStructureData(worldIn); - if (!this.structureMap.containsKey(Long.valueOf(LongHashMap.packInt(chunkX, chunkZ)))) + ChunkPos pos = new ChunkPos(chunkX, chunkZ); + if (!this.structureMap.containsKey(pos)) { this.rand.intv(); if (this.canSpawnStructureAtCoords(chunkX, chunkZ)) { StructureStart structurestart = this.getStructureStart(chunkX, chunkZ); - this.structureMap.put(Long.valueOf(LongHashMap.packInt(chunkX, chunkZ)), structurestart); + this.structureMap.put(pos, structurestart); this.setStructureStart(chunkX, chunkZ, structurestart); } } @@ -221,7 +221,7 @@ public abstract class MapGenStructure extends MapGenBase if (structurestart != null) { - this.structureMap.put(Long.valueOf(LongHashMap.packInt(i, j)), structurestart); + this.structureMap.put(new ChunkPos(i, j), structurestart); } } }