Trim registry hashmaps to improve lookup performance

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-10-17 14:29:27 +02:00
parent fb39fa59f5
commit 999d774f90
2 changed files with 11 additions and 4 deletions

View File

@ -3,7 +3,6 @@ package net.minestom.server.instance.block;
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.registry.Registry; import net.minestom.server.registry.Registry;
import net.minestom.server.tag.Tag; import net.minestom.server.tag.Tag;
@ -18,7 +17,7 @@ import java.util.function.Function;
final class BlockImpl implements Block { final class BlockImpl implements Block {
// Block state -> block object // Block state -> block object
private static final Int2ObjectMap<Block> BLOCK_STATE_MAP = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectOpenHashMap<Block> BLOCK_STATE_MAP = new Int2ObjectOpenHashMap<>();
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS, private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
(container, namespace, object) -> { (container, namespace, object) -> {
final JsonObject stateObject = object.remove("states").getAsJsonObject(); final JsonObject stateObject = object.remove("states").getAsJsonObject();
@ -43,6 +42,10 @@ final class BlockImpl implements Block {
.weakValues() .weakValues()
.build(); .build();
static {
BLOCK_STATE_MAP.trim();
}
static Block get(@NotNull String namespace) { static Block get(@NotNull String namespace) {
return CONTAINER.get(namespace); return CONTAINER.get(namespace);
} }

View File

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.entity.EntitySpawnType; import net.minestom.server.entity.EntitySpawnType;
import net.minestom.server.entity.EquipmentSlot; import net.minestom.server.entity.EquipmentSlot;
@ -77,9 +76,11 @@ public final class Registry {
// namespace -> registry data // namespace -> registry data
private final Map<String, T> namespaceMap = new HashMap<>(); private final Map<String, T> namespaceMap = new HashMap<>();
// id -> registry data // id -> registry data
private final Int2ObjectMap<T> idMap = new Int2ObjectOpenHashMap<>(); private final Int2ObjectOpenHashMap<T> idMap = new Int2ObjectOpenHashMap<>();
private final Collection<T> objects = Collections.unmodifiableCollection(namespaceMap.values()); private final Collection<T> objects = Collections.unmodifiableCollection(namespaceMap.values());
private boolean initialized;
@ApiStatus.Internal @ApiStatus.Internal
public Container(Resource resource, Loader<T> loader) { public Container(Resource resource, Loader<T> loader) {
final JsonObject objects = Registry.load(resource); final JsonObject objects = Registry.load(resource);
@ -88,6 +89,8 @@ public final class Registry {
final JsonObject object = entry.getValue().getAsJsonObject(); final JsonObject object = entry.getValue().getAsJsonObject();
loader.accept(this, namespace, object); loader.accept(this, namespace, object);
} }
this.initialized = true;
this.idMap.trim();
} }
public T get(@NotNull String namespace) { public T get(@NotNull String namespace) {
@ -107,6 +110,7 @@ public final class Registry {
} }
public void register(@NotNull T value) { public void register(@NotNull T value) {
Check.stateCondition(initialized, "Registering is only available within the loader lambda.");
this.idMap.put(value.id(), value); this.idMap.put(value.id(), value);
this.namespaceMap.put(value.name(), value); this.namespaceMap.put(value.name(), value);
} }