From 0db5993bfb14cef081a91ae32d0e217e7cacfc41 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 26 Jul 2022 16:40:31 +0200 Subject: [PATCH] Use array for metadata lookup instead of map Signed-off-by: TheMode --- .../net/minestom/server/entity/Metadata.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Metadata.java b/src/main/java/net/minestom/server/entity/Metadata.java index 009647b6c..70af1c321 100644 --- a/src/main/java/net/minestom/server/entity/Metadata.java +++ b/src/main/java/net/minestom/server/entity/Metadata.java @@ -14,10 +14,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; import org.jglrxavpok.hephaistos.nbt.NBT; -import space.vectrix.flare.fastutil.Int2ObjectSyncMap; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -155,7 +155,8 @@ public final class Metadata { } private final Entity entity; - private final Int2ObjectSyncMap> metadataMap = Int2ObjectSyncMap.hashmap(); + private volatile Entry[] entries = new Entry[0]; + private volatile Map> entryMap = null; @SuppressWarnings("FieldMayBeFinal") private volatile boolean notifyAboutChanges = true; @@ -167,12 +168,21 @@ public final class Metadata { @SuppressWarnings("unchecked") public T getIndex(int index, @Nullable T defaultValue) { - final Entry entry = this.metadataMap.get(index); + final Entry[] entries = this.entries; + if (index < 0 || index >= entries.length) return defaultValue; + final Entry entry = entries[index]; return entry != null ? (T) entry.value() : defaultValue; } public void setIndex(int index, @NotNull Entry entry) { - this.metadataMap.put(index, entry); + Entry[] entries = this.entries; + // Resize array if necessary + if (index >= entries.length) { + final int newLength = Math.max(entries.length * 2, index + 1); + this.entries = entries = Arrays.copyOf(entries, newLength); + this.entryMap = null; + } + entries[index] = entry; // Send metadata packet to update viewers and self final Entity entity = this.entity; if (entity != null && entity.isActive()) { @@ -206,7 +216,17 @@ public final class Metadata { } public @NotNull Map> getEntries() { - return metadataMap; + Map> map = entryMap; + if (map == null) { + map = new HashMap<>(); + final Entry[] entries = this.entries; + for (int i = 0; i < entries.length; i++) { + final Entry entry = entries[i]; + if (entry != null) map.put(i, entry); + } + this.entryMap = Map.copyOf(map); + } + return map; } public sealed interface Entry extends Writeable