Use fastutil treemap

This commit is contained in:
TheMode 2021-07-11 00:56:54 +02:00
parent 99d4682655
commit 560b450b3e
3 changed files with 9 additions and 26 deletions

View File

@ -4,8 +4,6 @@ import net.minestom.server.Tickable;
import net.minestom.server.Viewable; import net.minestom.server.Viewable;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec; import net.minestom.server.coordinate.Vec;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFColumnarSpace; import net.minestom.server.entity.pathfinding.PFColumnarSpace;
import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.EventDispatcher;
@ -42,8 +40,7 @@ import java.util.concurrent.ConcurrentHashMap;
* You generally want to avoid storing references of this object as this could lead to a huge memory leak, * You generally want to avoid storing references of this object as this could lead to a huge memory leak,
* you should store the chunk coordinates instead. * you should store the chunk coordinates instead.
*/ */
public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Tickable, TagHandler, DataContainer { public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Tickable, TagHandler {
public static final int CHUNK_SIZE_X = 16; public static final int CHUNK_SIZE_X = 16;
public static final int CHUNK_SIZE_Z = 16; public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16; public static final int CHUNK_SECTION_SIZE = 16;
@ -68,7 +65,6 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
// Data // Data
private final NBTCompound nbt = new NBTCompound(); private final NBTCompound nbt = new NBTCompound();
protected Data data;
public Chunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) { public Chunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) {
this.identifier = UUID.randomUUID(); this.identifier = UUID.randomUUID();
@ -102,7 +98,7 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
@Override @Override
public abstract void setBlock(int x, int y, int z, @NotNull Block block); public abstract void setBlock(int x, int y, int z, @NotNull Block block);
public abstract @NotNull TreeMap<Integer, Section> getSections(); public abstract @NotNull Map<Integer, Section> getSections();
public abstract @NotNull Section getSection(int section); public abstract @NotNull Section getSection(int section);
@ -373,17 +369,6 @@ public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Ticka
tag.write(nbt, value); tag.write(nbt, value);
} }
@Nullable
@Override
public Data getData() {
return data;
}
@Override
public void setData(@Nullable Data data) {
this.data = data;
}
/** /**
* Sends the chunk data to {@code player}. * Sends the chunk data to {@code player}.
* *

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance; package net.minestom.server.instance;
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList; import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.coordinate.Vec; import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.pathfinding.PFBlock; import net.minestom.server.entity.pathfinding.PFBlock;
@ -11,12 +12,10 @@ import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome; import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.TreeMap;
/** /**
* Represents a {@link Chunk} which store each individual block in memory. * Represents a {@link Chunk} which store each individual block in memory.
@ -25,7 +24,7 @@ import java.util.TreeMap;
*/ */
public class DynamicChunk extends Chunk { public class DynamicChunk extends Chunk {
protected final TreeMap<Integer, Section> sectionMap = new TreeMap<>(); protected final Int2ObjectAVLTreeMap<Section> sectionMap = new Int2ObjectAVLTreeMap<>();
// Key = ChunkUtils#getBlockIndex // Key = ChunkUtils#getBlockIndex
protected final Int2ObjectOpenHashMap<Block> entries = new Int2ObjectOpenHashMap<>(); protected final Int2ObjectOpenHashMap<Block> entries = new Int2ObjectOpenHashMap<>();
@ -69,7 +68,7 @@ public class DynamicChunk extends Chunk {
} }
@Override @Override
public @NotNull TreeMap<Integer, Section> getSections() { public @NotNull Map<Integer, Section> getSections() {
return sectionMap; return sectionMap;
} }
@ -129,7 +128,7 @@ public class DynamicChunk extends Chunk {
packet.biomes = biomes; packet.biomes = biomes;
packet.chunkX = chunkX; packet.chunkX = chunkX;
packet.chunkZ = chunkZ; packet.chunkZ = chunkZ;
packet.sections = (Map<Integer, Section>) sectionMap.clone(); // TODO deep clone packet.sections = sectionMap.clone(); // TODO deep clone
packet.entries = entries.clone(); packet.entries = entries.clone();
this.cachedPacketTime = getLastChangeTime(); this.cachedPacketTime = getLastChangeTime();
@ -141,8 +140,8 @@ public class DynamicChunk extends Chunk {
@Override @Override
public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) { public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) {
DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ); DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ);
for (var entry : sectionMap.entrySet()) { for (var entry : sectionMap.int2ObjectEntrySet()) {
dynamicChunk.sectionMap.put(entry.getKey(), entry.getValue().clone()); dynamicChunk.sectionMap.put(entry.getIntKey(), entry.getValue().clone());
} }
dynamicChunk.entries.putAll(entries); dynamicChunk.entries.putAll(entries);
return dynamicChunk; return dynamicChunk;

View File

@ -5,7 +5,6 @@ import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2LongRBTreeMap; import it.unimi.dsi.fastutil.ints.Int2LongRBTreeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.DynamicChunk;
import net.minestom.server.instance.Section; import net.minestom.server.instance.Section;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler; import net.minestom.server.instance.block.BlockHandler;
@ -133,7 +132,7 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
final int index = entry.getKey(); final int index = entry.getKey();
final var block = entry.getValue(); final var block = entry.getValue();
final BlockHandler handler = block.handler(); final BlockHandler handler = block.handler();
if(handler == null) if (handler == null)
continue; continue;
final var blockEntityTags = handler.getBlockEntityTags(); final var blockEntityTags = handler.getBlockEntityTags();
if (blockEntityTags.isEmpty()) // Verify if the block should be sent as block entity to client if (blockEntityTags.isEmpty()) // Verify if the block should be sent as block entity to client