Skip to content

Commit 3a1e723

Browse files
committed
Improve linear region efficiency by caching the whole region-file data
1 parent dbde93c commit 3a1e723

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/mca/MCAWorld.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class MCAWorld implements World {
5656
private final ChunkLoader chunkLoader = new ChunkLoader(this);
5757
private final LoadingCache<Vector2i, Region> regionCache = Caffeine.newBuilder()
5858
.executor(BlueMap.THREAD_POOL)
59-
.maximumSize(64)
59+
.maximumSize(32)
6060
.expireAfterWrite(10, TimeUnit.MINUTES)
6161
.build(this::loadRegion);
6262
private final LoadingCache<Vector2i, Chunk> chunkCache = Caffeine.newBuilder()
@@ -176,11 +176,13 @@ public void preloadRegionChunks(int x, int z) {
176176

177177
@Override
178178
public void invalidateChunkCache() {
179+
regionCache.invalidateAll();
179180
chunkCache.invalidateAll();
180181
}
181182

182183
@Override
183184
public void invalidateChunkCache(int x, int z) {
185+
regionCache.invalidate(VECTOR_2_I_CACHE.get(x >> 5, z >> 5));
184186
chunkCache.invalidate(VECTOR_2_I_CACHE.get(x, z));
185187
}
186188

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/mca/region/LinearRegion.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ public class LinearRegion implements Region {
6969
private final Path regionFile;
7070
private final Vector2i regionPos;
7171

72+
private boolean initialized = false;
73+
74+
private byte version;
75+
private long newestTimestamp;
76+
private byte compressionLevel;
77+
private short chunkCount;
78+
private int dataLength;
79+
private long dataHash;
80+
private byte[] compressedData;
81+
7282
public LinearRegion(MCAWorld world, Path regionFile) throws IllegalArgumentException {
7383
this.world = world;
7484
this.regionFile = regionFile;
@@ -86,26 +96,14 @@ public LinearRegion(MCAWorld world, Vector2i regionPos) throws IllegalArgumentEx
8696
this.regionFile = world.getRegionFolder().resolve(getRegionFileName(regionPos.getX(), regionPos.getY()));
8797
}
8898

89-
@Override
90-
public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
99+
private synchronized void init() throws IOException {
100+
if (initialized) return;
101+
91102
if (Files.notExists(regionFile)) return;
92103

93104
long fileLength = Files.size(regionFile);
94105
if (fileLength == 0) return;
95106

96-
int chunkStartX = regionPos.getX() * 32;
97-
int chunkStartZ = regionPos.getY() * 32;
98-
99-
byte[] chunkDataBuffer = null;
100-
byte[] compressedData;
101-
102-
byte version;
103-
long newestTimestamp;
104-
byte compressionLevel;
105-
short chunkCount;
106-
int dataLength;
107-
long dataHash;
108-
109107
try (
110108
InputStream in = Files.newInputStream(regionFile, StandardOpenOption.READ);
111109
BufferedInputStream bIn = new BufferedInputStream(in);
@@ -136,6 +134,18 @@ public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
136134

137135
}
138136

137+
initialized = true;
138+
}
139+
140+
@Override
141+
public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
142+
if (!initialized) init();
143+
144+
int chunkStartX = regionPos.getX() * 32;
145+
int chunkStartZ = regionPos.getY() * 32;
146+
147+
byte[] chunkDataBuffer = null;
148+
139149
try (
140150
InputStream in = new ZstdInputStream(new ByteArrayInputStream(compressedData));
141151
BufferedInputStream bIn = new BufferedInputStream(in);

0 commit comments

Comments
 (0)