Reduce property map allocation

This commit is contained in:
TheMode 2021-06-19 16:12:36 +02:00
parent 06ebb14991
commit 61bd28b298
2 changed files with 15 additions and 14 deletions

View File

@ -2,9 +2,11 @@ package net.minestom.server.instance.block;
import com.google.gson.JsonObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -80,7 +82,11 @@ class BlockRegistry {
}
private static Map<String, String> getPropertyMap(String query) {
Map<String, String> result = new HashMap<>();
if (query.equals("[]")) {
return Collections.emptyMap();
}
final int capacity = StringUtils.countMatches(query, ',') + 1;
Map<String, String> result = new HashMap<>(capacity);
final String propertiesString = query.substring(1);
StringBuilder keyBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();

View File

@ -14,27 +14,22 @@ import java.util.Objects;
class BlockTest implements Block {
private final Registry.BlockEntry registry;
private final Map<String, String> properties;
private final NBTCompound compound;
private final BlockHandler handler;
private final Map<String, String> unmodifiableProperties;
BlockTest(Registry.BlockEntry registry,
Map<String, String> properties,
NBTCompound compound,
BlockHandler handler) {
BlockTest(@NotNull Registry.BlockEntry registry,
@NotNull Map<String, String> properties,
@Nullable NBTCompound compound,
@Nullable BlockHandler handler) {
this.registry = registry;
this.properties = properties;
this.properties = Collections.unmodifiableMap(properties);
this.compound = compound;
this.handler = handler;
this.unmodifiableProperties = Collections.unmodifiableMap(properties);
}
BlockTest(Registry.BlockEntry registry,
Map<String, String> properties) {
BlockTest(@NotNull Registry.BlockEntry registry,
@NotNull Map<String, String> properties) {
this(registry, properties, null, null);
}
@ -68,7 +63,7 @@ class BlockTest implements Block {
@Override
public @NotNull Map<String, String> getProperties() {
return unmodifiableProperties;
return properties;
}
@Override