Align TriStateIntCache

This commit is contained in:
Zoe Gidiere
2025-12-11 01:56:20 -07:00
parent 97b4ea6d94
commit bba55f2669
2 changed files with 16 additions and 5 deletions

View File

@@ -23,9 +23,6 @@ public class ReplaceExtrusion implements Extrusion {
private final Range range; private final Range range;
private final ProbabilityCollection<ReplaceableBiome> biomes; private final ProbabilityCollection<ReplaceableBiome> biomes;
private final Predicate<Biome> hasTag; private final Predicate<Biome> hasTag;
// Replaced ThreadLocal<HashMap> with a specialized primitive cache.
// Shared across all threads safely.
private final TriStateIntCache cache; private final TriStateIntCache cache;
public ReplaceExtrusion(Sampler sampler, Range range, ProbabilityCollection<ReplaceableBiome> biomes, String tag) { public ReplaceExtrusion(Sampler sampler, Range range, ProbabilityCollection<ReplaceableBiome> biomes, String tag) {
@@ -33,7 +30,7 @@ public class ReplaceExtrusion implements Extrusion {
this.range = range; this.range = range;
this.biomes = biomes; this.biomes = biomes;
this.hasTag = BiomeQueries.has(tag); this.hasTag = BiomeQueries.has(tag);
this.cache = new TriStateIntCache(Math.max(512, Biome.INT_ID_COUNTER.get())); this.cache = new TriStateIntCache(Biome.INT_ID_COUNTER.get());
} }
@Override @Override

View File

@@ -24,8 +24,22 @@ public class TriStateIntCache {
ARRAY_BASE_OFFSET = UnsafeUtils.UNSAFE.arrayBaseOffset(long[].class); ARRAY_BASE_OFFSET = UnsafeUtils.UNSAFE.arrayBaseOffset(long[].class);
} }
private static int getOptimalMaxKeys(int requestedKeys) {
// 192 keys fill the first cache line exactly (along with the 16-byte header)
if (requestedKeys <= 192) {
return 192;
}
// For every additional line, we fit 256 keys (64 bytes * 4 keys/byte)
// We calculate the overflow beyond 192, round up to the nearest 256, and add it back.
int overflow = requestedKeys - 192;
int chunks = (overflow + 255) >>> 8; // Fast ceil division by 256
return 192 + (chunks << 8); // chunks * 256
}
public TriStateIntCache(int maxKeySize) { public TriStateIntCache(int maxKeySize) {
this.data = new long[(maxKeySize + 31) >>> 5]; this.data = new long[(getOptimalMaxKeys(maxKeySize) + 31) >>> 5];
} }
/** /**