From b155fb672094079b2b60bb42c2b697377f842fd6 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 24 Jun 2021 23:13:31 +0200 Subject: [PATCH] Micro-optimization for BlockLoader --- .../server/instance/block/BlockLoader.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/block/BlockLoader.java b/src/main/java/net/minestom/server/instance/block/BlockLoader.java index ec1a9c5fa..bf1e76c0f 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockLoader.java +++ b/src/main/java/net/minestom/server/instance/block/BlockLoader.java @@ -1,30 +1,38 @@ package net.minestom.server.instance.block; import com.google.gson.JsonObject; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minestom.server.registry.Registry; import net.minestom.server.utils.block.BlockUtils; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Loads {@link Block blocks} from file. */ +@ApiStatus.Internal class BlockLoader { + // Maps do not need to be thread-safe as they are fully populated + // in the static initializer, should not be modified during runtime + // Block namespace -> registry data - private static final Map NAMESPACE_MAP = new ConcurrentHashMap<>(); - // Block id -> registry data - private static final Map BLOCK_ID_MAP = new ConcurrentHashMap<>(); - // Block state -> block object - private static final Map BLOCK_STATE_MAP = new ConcurrentHashMap<>(); + private static final Map NAMESPACE_MAP = new HashMap<>(); // Block namespace -> properties map to block access - private static final Map BLOCK_PROPERTY_MAP = new ConcurrentHashMap<>(); + private static final Map BLOCK_PROPERTY_MAP = new HashMap<>(); + // Block id -> registry data + private static final Int2ObjectMap BLOCK_ID_MAP = new Int2ObjectOpenHashMap<>(); + // Block state -> block object + private static final Int2ObjectMap BLOCK_STATE_MAP = new Int2ObjectOpenHashMap<>(); static @Nullable Block get(@NotNull String namespace) { - if (!namespace.contains(":")) { + if (namespace.indexOf(':') == -1) { // Default to minecraft namespace namespace = "minecraft:" + namespace; }