Skip to content

Commit ae2d801

Browse files
perf: reduce object churn by swapping to long keys in GeneratedChunkCache
1 parent 8578bba commit ae2d801

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

platforms/minestom/src/main/java/com/dfsek/terra/minestom/chunk/GeneratedChunkCache.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.benmanes.caffeine.cache.LoadingCache;
55
import com.github.benmanes.caffeine.cache.stats.CacheStats;
66
import net.minestom.server.world.DimensionType;
7+
import org.jetbrains.annotations.NotNull;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

@@ -15,7 +16,7 @@
1516

1617
public class GeneratedChunkCache {
1718
private static final Logger log = LoggerFactory.getLogger(GeneratedChunkCache.class);
18-
private final LoadingCache<Pair<Integer, Integer>, CachedChunk> cache;
19+
private final LoadingCache<@NotNull Long, CachedChunk> cache;
1920
private final DimensionType dimensionType;
2021
private final ChunkGenerator generator;
2122
private final ServerWorld world;
@@ -29,7 +30,7 @@ public GeneratedChunkCache(DimensionType dimensionType, ChunkGenerator generator
2930
this.cache = Caffeine.newBuilder()
3031
.maximumSize(128)
3132
.recordStats()
32-
.build((Pair<Integer, Integer> key) -> generateChunk(key.getLeft(), key.getRight()));
33+
.build((Long key) -> generateChunk(unpackX(key), unpackZ(key)));
3334
}
3435

3536
private CachedChunk generateChunk(int x, int z) {
@@ -50,6 +51,18 @@ public void displayStats() {
5051
}
5152

5253
public CachedChunk at(int x, int z) {
53-
return cache.get(Pair.of(x, z));
54+
return cache.get(pack(x, z));
55+
}
56+
57+
private long pack(final int x, final int z) {
58+
return ((long) x) << 32 | z & 0xFFFFFFFFL;
59+
}
60+
61+
private int unpackX(long key) {
62+
return (int) (key >>> 32);
63+
}
64+
65+
private int unpackZ(long key) {
66+
return (int) key;
5467
}
5568
}

0 commit comments

Comments
 (0)