Switch to arrays to improve object-allocation
This commit is contained in:
parent
c6019c722d
commit
63a003b13e
|
@ -35,7 +35,7 @@ import de.bluecolored.bluemap.core.world.BlockNeighborhood;
|
|||
import de.bluecolored.bluemap.core.world.BlockState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockStateModelFactory {
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class BlockStateModelFactory {
|
|||
private final ResourceModelBuilder resourceModelBuilder;
|
||||
private final LiquidModelBuilder liquidModelBuilder;
|
||||
|
||||
private final Collection<Variant> variants = new ArrayList<>();
|
||||
private final List<Variant> variants = new ArrayList<>();
|
||||
|
||||
public BlockStateModelFactory(ResourcePack resourcePack, TextureGallery textureGallery, RenderSettings renderSettings) {
|
||||
this.resourcePack = resourcePack;
|
||||
|
@ -88,7 +88,11 @@ public class BlockStateModelFactory {
|
|||
float blockColorOpacity = 0;
|
||||
variants.clear();
|
||||
stateResource.forEach(blockState, block.getX(), block.getY(), block.getZ(), variants::add);
|
||||
for (Variant variant : variants) {
|
||||
|
||||
//noinspection ForLoopReplaceableByForEach
|
||||
for (int i = 0; i < variants.size(); i++) {
|
||||
Variant variant = variants.get(i);
|
||||
|
||||
BlockModel modelResource = variant.getModel().getResource(resourcePack::getBlockModel);
|
||||
if (modelResource == null) continue;
|
||||
|
||||
|
|
|
@ -49,8 +49,6 @@ import de.bluecolored.bluemap.core.world.BlockNeighborhood;
|
|||
import de.bluecolored.bluemap.core.world.ExtendedBlock;
|
||||
import de.bluecolored.bluemap.core.world.LightData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This model builder creates a BlockStateModel using the information from parsed resource-pack json files.
|
||||
*/
|
||||
|
@ -100,7 +98,7 @@ public class ResourceModelBuilder {
|
|||
// render model
|
||||
int modelStart = blockModel.getStart();
|
||||
|
||||
List<Element> elements = modelResource.getElements();
|
||||
Element[] elements = modelResource.getElements();
|
||||
if (elements != null) {
|
||||
for (Element element : elements) {
|
||||
buildModelElementResource(element, blockModel.initialize());
|
||||
|
|
|
@ -199,9 +199,11 @@ public class ResourcePack {
|
|||
if (Files.isRegularFile(fabricModJson)) {
|
||||
try (BufferedReader reader = Files.newBufferedReader(fabricModJson)) {
|
||||
JsonObject rootElement = ResourcesGson.INSTANCE.fromJson(reader, JsonObject.class);
|
||||
for (JsonElement element : rootElement.getAsJsonArray("jars")) {
|
||||
Path file = root.resolve(element.getAsJsonObject().get("file").getAsString());
|
||||
if (Files.exists(file)) loadResourcePath(file, resourceLoader);
|
||||
if (rootElement.has("jars")) {
|
||||
for (JsonElement element : rootElement.getAsJsonArray("jars")) {
|
||||
Path file = root.resolve(element.getAsJsonObject().get("file").getAsString());
|
||||
if (Files.exists(file)) loadResourcePath(file, resourceLoader);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.global.logDebug("Failed to read fabric.mod.json: " + ex);
|
||||
|
|
|
@ -15,7 +15,7 @@ public class BlockModel {
|
|||
|
||||
private ResourcePath<BlockModel> parent;
|
||||
private Map<String, TextureVariable> textures = new HashMap<>();
|
||||
private List<Element> elements;
|
||||
private Element[] elements;
|
||||
private boolean ambientocclusion = true;
|
||||
|
||||
private transient boolean liquid = false;
|
||||
|
@ -34,7 +34,7 @@ public class BlockModel {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
public List<Element> getElements() {
|
||||
public Element[] getElements() {
|
||||
return elements;
|
||||
}
|
||||
|
||||
|
@ -84,8 +84,10 @@ public class BlockModel {
|
|||
|
||||
parent.textures.forEach(this::applyTextureVariable);
|
||||
if (this.elements == null && parent.elements != null) {
|
||||
this.elements = new ArrayList<>();
|
||||
parent.elements.forEach(element -> this.elements.add(element.copy()));
|
||||
this.elements = new Element[parent.elements.length];
|
||||
for (int i = 0; i < this.elements.length; i++){
|
||||
this.elements[i] = parent.elements[i].copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,11 +19,11 @@ import java.util.function.Consumer;
|
|||
@JsonAdapter(Multipart.Adapter.class)
|
||||
public class Multipart {
|
||||
|
||||
private List<VariantSet> parts = new ArrayList<>();
|
||||
private VariantSet[] parts = new VariantSet[0];
|
||||
|
||||
private Multipart(){}
|
||||
|
||||
public List<VariantSet> getParts() {
|
||||
public VariantSet[] getParts() {
|
||||
return parts;
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class Multipart {
|
|||
|
||||
@Override
|
||||
public Multipart read(JsonReader in, Gson gson) throws IOException {
|
||||
Multipart result = new Multipart();
|
||||
List<VariantSet> parts = new ArrayList<>();
|
||||
|
||||
in.beginArray();
|
||||
while (in.hasNext()) {
|
||||
|
@ -63,10 +63,12 @@ public class Multipart {
|
|||
|
||||
if (variantSet == null) continue;
|
||||
if (condition != null) variantSet.setCondition(condition);
|
||||
result.parts.add(variantSet);
|
||||
parts.add(variantSet);
|
||||
}
|
||||
in.endArray();
|
||||
|
||||
Multipart result = new Multipart();
|
||||
result.parts = parts.toArray(VariantSet[]::new);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package de.bluecolored.bluemap.core.resources.resourcepack.blockstate;
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.annotations.JsonAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import de.bluecolored.bluemap.api.debug.DebugDump;
|
||||
import de.bluecolored.bluemap.core.resources.AbstractTypeAdapterFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@SuppressWarnings("FieldMayBeFinal")
|
||||
|
@ -19,15 +17,15 @@ import java.util.function.Consumer;
|
|||
public class VariantSet {
|
||||
|
||||
private BlockStateCondition condition;
|
||||
private List<Variant> variants;
|
||||
private Variant[] variants;
|
||||
|
||||
private transient double totalWeight;
|
||||
|
||||
public VariantSet(List<Variant> variants) {
|
||||
public VariantSet(Variant... variants) {
|
||||
this(BlockStateCondition.all(), variants);
|
||||
}
|
||||
|
||||
public VariantSet(BlockStateCondition condition, List<Variant> variants) {
|
||||
public VariantSet(BlockStateCondition condition, Variant... variants) {
|
||||
this.condition = condition;
|
||||
this.variants = variants;
|
||||
|
||||
|
@ -42,12 +40,12 @@ public class VariantSet {
|
|||
this.condition = condition;
|
||||
}
|
||||
|
||||
public List<Variant> getVariants() {
|
||||
public Variant[] getVariants() {
|
||||
return variants;
|
||||
}
|
||||
|
||||
private double summarizeWeights() {
|
||||
return variants.stream()
|
||||
return Arrays.stream(variants)
|
||||
.mapToDouble(Variant::getWeight)
|
||||
.sum();
|
||||
}
|
||||
|
@ -64,7 +62,7 @@ public class VariantSet {
|
|||
}
|
||||
|
||||
private static float hashToFloat(int x, int y, int z) {
|
||||
final long hash = x * 73438747 ^ y * 9357269 ^ z * 4335792;
|
||||
final long hash = x * 73438747L ^ y * 9357269L ^ z * 4335792L;
|
||||
return (hash * (hash + 456149) & 0x00ffffff) / (float) 0x01000000;
|
||||
}
|
||||
|
||||
|
@ -76,11 +74,11 @@ public class VariantSet {
|
|||
|
||||
@Override
|
||||
public VariantSet read(JsonReader in, Gson gson) throws IOException {
|
||||
List<Variant> variants;
|
||||
Variant[] variants;
|
||||
if (in.peek() == JsonToken.BEGIN_ARRAY) {
|
||||
variants = gson.fromJson(in, new TypeToken<List<Variant>>(){}.getType());
|
||||
variants = gson.fromJson(in, Variant[].class);
|
||||
} else {
|
||||
variants = Collections.singletonList(gson.fromJson(in, Variant.class));
|
||||
variants = new Variant[]{ gson.fromJson(in, Variant.class) };
|
||||
}
|
||||
|
||||
return new VariantSet(variants);
|
||||
|
|
|
@ -20,12 +20,12 @@ import java.util.function.Consumer;
|
|||
@JsonAdapter(Variants.Adapter.class)
|
||||
public class Variants {
|
||||
|
||||
private List<VariantSet> variants = new ArrayList<>();
|
||||
private VariantSet[] variants = new VariantSet[0];
|
||||
private VariantSet defaultVariant;
|
||||
|
||||
private Variants(){}
|
||||
|
||||
public List<VariantSet> getVariants() {
|
||||
public VariantSet[] getVariants() {
|
||||
return variants;
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,8 @@ public class Variants {
|
|||
|
||||
@Override
|
||||
public Variants read(JsonReader in, Gson gson) throws IOException {
|
||||
Variants result = new Variants();
|
||||
VariantSet defaultVariant = null;
|
||||
List<VariantSet> variants = new ArrayList<>();
|
||||
|
||||
in.beginObject();
|
||||
while (in.hasNext()) {
|
||||
|
@ -71,13 +72,16 @@ public class Variants {
|
|||
variantSet.setCondition(condition);
|
||||
|
||||
if (variantSet.getCondition() == BlockStateCondition.all()) {
|
||||
result.defaultVariant = variantSet;
|
||||
defaultVariant = variantSet;
|
||||
} else if (variantSet.getCondition() != BlockStateCondition.none()) {
|
||||
result.variants.add(variantSet);
|
||||
variants.add(variantSet);
|
||||
}
|
||||
}
|
||||
in.endObject();
|
||||
|
||||
Variants result = new Variants();
|
||||
result.defaultVariant = defaultVariant;
|
||||
result.variants = variants.toArray(VariantSet[]::new);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue