Micro optimize block properties map

This commit is contained in:
TheMode 2021-07-23 15:44:53 +02:00
parent 8734478126
commit 11b2426629
2 changed files with 15 additions and 23 deletions

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.time.Duration; import java.time.Duration;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -33,27 +32,19 @@ final class BlockImpl implements Block {
@Nullable BlockHandler handler) { @Nullable BlockHandler handler) {
this.registry = registry; this.registry = registry;
this.propertyEntry = propertyEntry; this.propertyEntry = propertyEntry;
this.properties = Collections.unmodifiableMap(properties); this.properties = properties;
this.nbt = nbt; this.nbt = nbt;
this.handler = handler; this.handler = handler;
} }
BlockImpl(@NotNull Registry.BlockEntry registry,
@NotNull BlockLoader.PropertyEntry propertyEntry,
@NotNull Map<String, String> properties) {
this(registry, propertyEntry, properties, null, null);
}
@Override @Override
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) { public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
var properties = new HashMap<>(this.properties); var properties = new HashMap<>(this.properties);
properties.put(property, value); properties.replace(property, value);
Block block = propertyEntry.getProperties(properties); Block block = propertyEntry.getProperties(properties);
if (block == null) if (block == null)
throw new IllegalArgumentException("Invalid property: " + property + ":" + value); throw new IllegalArgumentException("Invalid property: " + property + ":" + value);
if (nbt != null || handler != null) return compute(block);
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
return block;
} }
@Override @Override
@ -67,14 +58,12 @@ final class BlockImpl implements Block {
block = propertyEntry.getProperties(properties); block = propertyEntry.getProperties(properties);
} else { } else {
var newProperties = new HashMap<>(this.properties); var newProperties = new HashMap<>(this.properties);
newProperties.putAll(properties); newProperties.replaceAll((key, value) -> Objects.requireNonNullElse(properties.get(key), value));
block = propertyEntry.getProperties(newProperties); block = propertyEntry.getProperties(newProperties);
} }
if (block == null) if (block == null)
throw new IllegalArgumentException("Invalid properties: " + properties); throw new IllegalArgumentException("Invalid properties: " + properties);
if (nbt != null || handler != null) return compute(block);
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
return block;
} }
@Override @Override
@ -92,7 +81,7 @@ final class BlockImpl implements Block {
@Override @Override
public boolean hasNbt() { public boolean hasNbt() {
return nbt != null && nbt.getSize() > 0; return nbt != null;
} }
@Override @Override
@ -112,8 +101,11 @@ final class BlockImpl implements Block {
@Override @Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) { public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
if (nbt == null) return nbt != null ? tag.read(nbt) : null;
return null; }
return tag.read(nbt);
private Block compute(Block original) {
return nbt == null && handler == null ? original :
new BlockImpl(original.registry(), propertyEntry, original.properties(), nbt, handler);
} }
} }

View File

@ -59,7 +59,7 @@ class BlockLoader {
final JsonObject blockObject = entry.getValue().getAsJsonObject(); final JsonObject blockObject = entry.getValue().getAsJsonObject();
final JsonObject stateObject = blockObject.remove("states").getAsJsonObject(); final JsonObject stateObject = blockObject.remove("states").getAsJsonObject();
retrieveState(blockNamespace, blockObject, stateObject); retrieveState(blockObject, stateObject);
final int defaultState = blockObject.get("defaultStateId").getAsInt(); final int defaultState = blockObject.get("defaultStateId").getAsInt();
final Block defaultBlock = getState(defaultState); final Block defaultBlock = getState(defaultState);
final int id = blockObject.get("id").getAsInt(); final int id = blockObject.get("id").getAsInt();
@ -68,14 +68,14 @@ class BlockLoader {
}); });
} }
private static void retrieveState(String namespace, JsonObject object, JsonObject stateObject) { private static void retrieveState(JsonObject object, JsonObject stateObject) {
PropertyEntry propertyEntry = new PropertyEntry(); PropertyEntry propertyEntry = new PropertyEntry();
stateObject.entrySet().forEach(stateEntry -> { stateObject.entrySet().forEach(stateEntry -> {
final String query = stateEntry.getKey(); final String query = stateEntry.getKey();
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject(); JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
final int stateId = stateOverride.get("stateId").getAsInt(); final int stateId = stateOverride.get("stateId").getAsInt();
final var propertyMap = BlockUtils.parseProperties(query); final var propertyMap = BlockUtils.parseProperties(query);
final Block block = new BlockImpl(Registry.block(object, stateOverride), propertyEntry, propertyMap); final Block block = new BlockImpl(Registry.block(object, stateOverride), propertyEntry, propertyMap, null, null);
BLOCK_STATE_MAP.put(stateId, block); BLOCK_STATE_MAP.put(stateId, block);
propertyEntry.map.put(propertyMap, block); propertyEntry.map.put(propertyMap, block);
}); });