Skip to content

Commit cd7de0c

Browse files
authored
fix-chunk-cache-misses (#20)
1 parent 3063c3d commit cd7de0c

File tree

9 files changed

+63
-38
lines changed

9 files changed

+63
-38
lines changed

doc/changelog.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@ <h1>JourneyMap ${version} for Minecraft ${mcversion}</h1>
2323

2424
<p>New in ${version}</p>
2525
<ul>
26-
<li>Fixed: bop plants showing up as wrong color.</li>
27-
<li>Fixed: Fairplay not loading and invalid zip errors at startup</li>
28-
<li>Fixed: Modded dim names in waypoint manager and edit screen.</li>
26+
<li>Fixed: weird chunk errors to memory allocation fix.</li>
2927
</ul>

src/main/java/journeymap/client/cartography/render/BaseRenderer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ protected int getSliceBlockHeight(final ChunkMD chunkMd, final int x, final Inte
291291
final int blockX = (chunkMd.getCoord().chunkXPos << 4) + (x + offset.x);
292292
final int blockZ = (chunkMd.getCoord().chunkZPos << 4) + (z + offset.z);
293293
ChunkMD targetChunkMd;
294-
295-
if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkZPos)
294+
long coord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
295+
if (coord == chunkMd.asLong())
296296
{
297297
targetChunkMd = chunkMd;
298298
}
299299
else
300300
{
301-
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
301+
targetChunkMd = dataCache.getChunkMD(coord);
302302
}
303303

304304
if (targetChunkMd != null)
@@ -500,13 +500,14 @@ public int getSurfaceBlockHeight(final ChunkMD chunkMd, int x, int z, BlockCoord
500500
final int blockZ = (chunkMd.getCoord().chunkZPos << 4) + (z + offset.z);
501501
ChunkMD targetChunkMd;
502502

503-
if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkXPos)
503+
long coord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
504+
if (coord == chunkMd.asLong())
504505
{
505506
targetChunkMd = chunkMd;
506507
}
507508
else
508509
{
509-
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
510+
targetChunkMd = dataCache.getChunkMD(coord);
510511
}
511512

512513
if (targetChunkMd != null)

src/main/java/journeymap/client/data/DataCache.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,39 @@
55

66
package journeymap.client.data;
77

8-
import com.google.common.cache.*;
8+
import com.google.common.cache.Cache;
9+
import com.google.common.cache.CacheBuilder;
10+
import com.google.common.cache.CacheLoader;
11+
import com.google.common.cache.CacheStats;
12+
import com.google.common.cache.LoadingCache;
13+
import com.google.common.cache.RemovalListener;
14+
import com.google.common.cache.RemovalNotification;
15+
import gnu.trove.map.TLongObjectMap;
16+
import gnu.trove.map.hash.TLongObjectHashMap;
917
import journeymap.client.JourneymapClient;
1018
import journeymap.client.log.LogFormatter;
11-
import journeymap.client.model.*;
19+
import journeymap.client.model.ChunkMD;
20+
import journeymap.client.model.EntityDTO;
21+
import journeymap.client.model.MapType;
22+
import journeymap.client.model.RegionCoord;
23+
import journeymap.client.model.RegionImageCache;
24+
import journeymap.client.model.RegionImageSet;
25+
import journeymap.client.model.Waypoint;
1226
import journeymap.client.render.draw.DrawEntityStep;
1327
import journeymap.client.render.draw.DrawWayPointStep;
1428
import journeymap.client.waypoint.WaypointStore;
1529
import journeymap.common.Journeymap;
1630
import net.minecraft.entity.EntityLivingBase;
1731
import net.minecraft.world.ChunkCoordIntPair;
1832

19-
import java.util.*;
33+
import java.util.ArrayList;
34+
import java.util.Collection;
35+
import java.util.Collections;
36+
import java.util.Comparator;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
import java.util.Set;
40+
import java.util.WeakHashMap;
2041
import java.util.concurrent.ExecutionException;
2142
import java.util.concurrent.TimeUnit;
2243

@@ -49,6 +70,8 @@ public class DataCache
4970
private final int chunkCacheExpireSeconds = 30;
5071
private final int defaultConcurrencyLevel = 1;
5172

73+
final TLongObjectMap<ChunkCoordIntPair> longCoordMap = new TLongObjectHashMap<>();
74+
5275
// Private constructor
5376
private DataCache()
5477
{
@@ -381,25 +404,23 @@ public DrawWayPointStep getDrawWayPointStep(Waypoint waypoint)
381404
return waypointDrawSteps.getUnchecked(waypoint);
382405
}
383406
}
384-
// public RGB getColor(Color color)
385-
// {
386-
// return getColor(color.getRGB());
387-
// }
388-
//
389-
// public RGB getColor(int rgbInt)
390-
// {
391-
// synchronized (colors)
392-
// {
393-
// return colors.getUnchecked(rgbInt);
394-
// }
395-
// }
396-
397-
public ChunkMD getChunkMD(ChunkCoordIntPair coord)
407+
408+
public ChunkMD getChunkMD(long coordLong)
398409
{
410+
399411
synchronized (chunkMetadata)
400412
{
401413
ChunkMD chunkMD = null;
402414

415+
ChunkCoordIntPair coord = longCoordMap.get(coordLong);
416+
if (coord == null)
417+
{
418+
int x = (int) (coordLong & 4294967295L);
419+
int z = (int) (coordLong >>> 32 & 4294967295L);
420+
coord = new ChunkCoordIntPair(x, z);
421+
longCoordMap.put(coordLong, coord);
422+
}
423+
403424
try
404425
{
405426
chunkMD = chunkMetadata.getUnchecked(coord);
@@ -480,7 +501,7 @@ public void purge()
480501
{
481502
// Flush images, do syncronously to ensure it's done before cache invalidates
482503
RegionImageCache.instance().flushToDisk(false);
483-
504+
longCoordMap.clear();
484505
synchronized (managedCaches)
485506
{
486507
for (Cache cache : managedCaches.keySet())

src/main/java/journeymap/client/data/PlayerData.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static boolean playerIsUnderground(Minecraft mc, EntityPlayer player)
5757
{
5858
y = posY + 1;
5959

60-
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
60+
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
6161
if (chunkMD != null)
6262
{
6363
if (chunkMD.ceiling(x & 15, z & 15) <= y)
@@ -91,10 +91,7 @@ public EntityDTO load(Class aClass) throws Exception
9191
*/
9292
private String getPlayerBiome(EntityPlayer player)
9393
{
94-
int x = (MathHelper.floor_double(player.posX) >> 4) & 15;
95-
int z = (MathHelper.floor_double(player.posZ) >> 4) & 15;
96-
97-
ChunkMD playerChunk = DataCache.instance().getChunkMD(new ChunkCoordIntPair(player.chunkCoordX, player.chunkCoordZ));
94+
ChunkMD playerChunk = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(player.chunkCoordX, player.chunkCoordZ));
9895
if (playerChunk != null)
9996
{
10097
try

src/main/java/journeymap/client/forge/helper/impl/ForgeHelper_1_7_10.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public BiomeGenBase getBiome(ChunkMD chunkMD, int x, int y, int z)
396396
@Override
397397
public BiomeGenBase getBiome(int x, int y, int z)
398398
{
399-
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
399+
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
400400
return getBiome(chunkMD, x, y, z);
401401
}
402402

@@ -449,7 +449,7 @@ class JmBlockAccess implements IBlockAccess
449449
{
450450
private Chunk getChunk(int x, int z)
451451
{
452-
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
452+
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
453453
if (chunkMD != null && chunkMD.hasChunk())
454454
{
455455
return chunkMD.getChunk();

src/main/java/journeymap/client/model/ChunkMD.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ public boolean canBlockSeeTheSky(int x, int y, int z)
261261
return ForgeHelper.INSTANCE.canBlockSeeTheSky(getChunk(), x, y, z);
262262
}
263263

264+
public long asLong()
265+
{
266+
return ChunkCoordIntPair.chunkXZ2Int(coord.chunkXPos, coord.chunkZPos);
267+
}
268+
264269
public ChunkCoordIntPair getCoord()
265270
{
266271
return coord;

src/main/java/journeymap/client/task/multi/BaseMapTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void performTask(Minecraft mc, JourneymapClient jm, File jmWorldDir, bool
109109
}
110110

111111
ChunkCoordIntPair coord = chunkIter.next();
112-
ChunkMD chunkMd = DataCache.instance().getChunkMD(coord);
112+
ChunkMD chunkMd = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(coord.chunkXPos, coord.chunkZPos));
113113
if (chunkMd != null && chunkMd.hasChunk())
114114
{
115115
try

src/main/java/journeymap/client/task/multi/RenderSpec.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,13 @@ protected Collection<ChunkCoordIntPair> getRenderAreaCoords()
188188
if (primaryRenderCoords == null || primaryRenderCoords.isEmpty())
189189
{
190190
List<Offset> primaryOffsets = offsets.get(primaryRenderDistance);
191-
primaryRenderCoords = new ArrayList<ChunkCoordIntPair>(primaryOffsets.size());
191+
primaryRenderCoords = new ArrayList<>(primaryOffsets.size());
192192
for (Offset offset : primaryOffsets)
193193
{
194194
ChunkCoordIntPair primaryCoord = offset.from(lastPlayerCoord);
195195
primaryRenderCoords.add(primaryCoord);
196-
dataCache.getChunkMD(primaryCoord);
196+
long coord = ChunkCoordIntPair.chunkXZ2Int(primaryCoord.chunkXPos, primaryCoord.chunkZPos);
197+
dataCache.getChunkMD(coord);
197198
}
198199
}
199200

@@ -219,7 +220,8 @@ protected Collection<ChunkCoordIntPair> getRenderAreaCoords()
219220
{
220221
ChunkCoordIntPair secondaryCoord = offset.from(lastPlayerCoord);
221222
renderCoords.add(secondaryCoord);
222-
dataCache.getChunkMD(secondaryCoord);
223+
long coord = ChunkCoordIntPair.chunkXZ2Int(secondaryCoord.chunkXPos, secondaryCoord.chunkZPos);
224+
dataCache.getChunkMD(coord);
223225
}
224226

225227
return renderCoords;

src/main/java/journeymap/client/ui/fullscreen/layer/BlockInfoLayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import journeymap.client.ui.option.LocationFormat;
2020
import net.minecraft.client.Minecraft;
2121
import net.minecraft.client.gui.FontRenderer;
22+
import net.minecraft.world.ChunkCoordIntPair;
2223
import net.minecraft.world.chunk.Chunk;
2324

2425
import java.util.ArrayList;
@@ -62,7 +63,7 @@ public List<DrawStep> onMouseMove(Minecraft mc, double mouseX, double mouseY, in
6263
String info;
6364
if (!chunk.isEmpty())
6465
{
65-
ChunkMD chunkMD = DataCache.instance().getChunkMD(chunk.getChunkCoordIntPair());
66+
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(chunk.xPosition, chunk.zPosition));
6667
int blockY = chunkMD.getPrecipitationHeight(blockCoord.x & 15, blockCoord.z & 15);
6768
String biome = ForgeHelper.INSTANCE.getBiome(blockCoord.x, blockY, blockCoord.z).biomeName;
6869

0 commit comments

Comments
 (0)