mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-12-03 20:01:49 +00:00
perf: replace biome tag checks with bitset columns
This commit is contained in:
1 parent
6ddf039e49
commit
8a940f7e1f
45 files changed
+938
-314
No files matched your search
+3
-2
@@ -7,6 +7,7 @@ import com.dfsek.terra.addons.biome.extrusion.api.ReplaceableBiome;
|
||||
import com.dfsek.terra.addons.biome.extrusion.extrusions.ReplaceExtrusion;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
public class ReplaceExtrusionTemplate extends SamplerExtrusionTemplate {
|
||||
@@ -14,10 +15,10 @@ public class ReplaceExtrusionTemplate extends SamplerExtrusionTemplate {
|
||||
private @Meta ProbabilityCollection<@Meta ReplaceableBiome> biomes;
|
||||
|
||||
@Value("from")
|
||||
private @Meta String fromTag;
|
||||
private @Meta BiomeTag from;
|
||||
|
||||
@Override
|
||||
public Extrusion get() {
|
||||
return new ReplaceExtrusion(sampler, range, biomes, fromTag);
|
||||
return new ReplaceExtrusion(sampler, range, biomes, from);
|
||||
}
|
||||
}
|
||||
+6
-22
@@ -8,11 +8,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
import com.dfsek.terra.addons.biome.extrusion.api.Extrusion;
|
||||
import com.dfsek.terra.addons.biome.extrusion.api.ReplaceableBiome;
|
||||
import com.dfsek.terra.addons.biome.query.api.BiomeQueries;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.util.collection.TriStateIntCache;
|
||||
import com.dfsek.terra.api.util.range.Range;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTag;
|
||||
|
||||
|
||||
/**
|
||||
@@ -22,34 +21,19 @@ public class ReplaceExtrusion implements Extrusion {
|
||||
private final Sampler sampler;
|
||||
private final Range range;
|
||||
private final ProbabilityCollection<ReplaceableBiome> biomes;
|
||||
private final Predicate<Biome> hasTag;
|
||||
private final TriStateIntCache cache;
|
||||
// Concrete final type keeps has() monomorphic for inlining.
|
||||
private final BiomeTag from;
|
||||
|
||||
public ReplaceExtrusion(Sampler sampler, Range range, ProbabilityCollection<ReplaceableBiome> biomes, String tag) {
|
||||
public ReplaceExtrusion(Sampler sampler, Range range, ProbabilityCollection<ReplaceableBiome> biomes, BiomeTag from) {
|
||||
this.sampler = sampler;
|
||||
this.range = range;
|
||||
this.biomes = biomes;
|
||||
this.hasTag = BiomeQueries.has(tag);
|
||||
this.cache = new TriStateIntCache(Biome.INT_ID_COUNTER.get());
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Biome extrude(Biome original, int x, int y, int z, long seed) {
|
||||
int id = original.getIntID();
|
||||
|
||||
long state = cache.get(id);
|
||||
boolean passes;
|
||||
|
||||
if(state == TriStateIntCache.STATE_UNSET) {
|
||||
// Only run the test if unset in cache
|
||||
passes = hasTag.test(original);
|
||||
cache.set(id, passes);
|
||||
} else {
|
||||
// Read the primitive long directly
|
||||
passes = (state == TriStateIntCache.STATE_TRUE);
|
||||
}
|
||||
|
||||
if(passes) {
|
||||
if(from.has(original.getIntID())) {
|
||||
if(range.isInRange(y)) {
|
||||
return biomes.get(sampler, x, y, z, seed).get(original);
|
||||
}
|
||||
|
||||
@@ -10,5 +10,3 @@ website:
|
||||
source: https://github.com/PolyhedralDev/Terra
|
||||
docs: https://terra.polydev.org
|
||||
license: MIT License
|
||||
depends:
|
||||
biome-query-api: "1.+"
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package extrusion;
|
||||
|
||||
import com.dfsek.seismic.type.sampler.Sampler;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.dfsek.terra.addons.biome.extrusion.api.ReplaceableBiome;
|
||||
import com.dfsek.terra.addons.biome.extrusion.extrusions.ReplaceExtrusion;
|
||||
import com.dfsek.terra.api.properties.Context;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.util.range.ConstantRange;
|
||||
import com.dfsek.terra.api.util.range.Range;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.PlatformBiome;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
class ReplaceExtrusionTest {
|
||||
private static final Sampler SAMPLER = new Sampler() {
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getSample(long seed, double x, double y, double z) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
private static final class StubBiome implements Biome {
|
||||
private final String id;
|
||||
private final Set<String> tags;
|
||||
private final int intId;
|
||||
|
||||
StubBiome(BiomeTagIndex index, String id, Set<String> tags) {
|
||||
this.id = id;
|
||||
this.tags = tags;
|
||||
this.intId = index.register(tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformBiome getPlatformBiome() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColor() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntID() {
|
||||
return intId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
private final BiomeTagIndex index = new BiomeTagIndex();
|
||||
private final Map<String, Biome> biomes = Map.of(
|
||||
"tagged", new StubBiome(index, "tagged", Set.of("X")),
|
||||
"untagged", new StubBiome(index, "untagged", Set.of("Y")),
|
||||
"replacement", new StubBiome(index, "replacement", Set.of()));
|
||||
|
||||
@ParameterizedTest(name = "{0}: {1} at y {2} -> {3}")
|
||||
@CsvSource({
|
||||
"X, tagged, 10, replacement",
|
||||
"X, untagged, 10, untagged",
|
||||
"X, tagged, 100, tagged",
|
||||
"TYPO, tagged, 10, tagged",
|
||||
"TYPO, untagged, 10, untagged",
|
||||
"TYPO, replacement, 10, replacement",
|
||||
})
|
||||
void replacesTaggedBiomesInRange(String tag, String biome, int y, String expected) {
|
||||
ProbabilityCollection<ReplaceableBiome> to = new ProbabilityCollection<ReplaceableBiome>()
|
||||
.add(ReplaceableBiome.of(biomes.get("replacement")), 1);
|
||||
Range range = new ConstantRange(0, 64);
|
||||
ReplaceExtrusion extrusion = new ReplaceExtrusion(SAMPLER, range, to, index.tag(tag));
|
||||
index.seal();
|
||||
assertSame(biomes.get(expected), extrusion.extrude(biomes.get(biome), 0, y, 0, 0));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user