Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-chunk-cache-misses #20

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions doc/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ <h1>JourneyMap ${version} for Minecraft ${mcversion}</h1>

<p>New in ${version}</p>
<ul>
<li>Fixed: bop plants showing up as wrong color.</li>
<li>Fixed: Fairplay not loading and invalid zip errors at startup</li>
<li>Fixed: Modded dim names in waypoint manager and edit screen.</li>
<li>Fixed: weird chunk errors to memory allocation fix.</li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ protected int getSliceBlockHeight(final ChunkMD chunkMd, final int x, final Inte
final int blockX = (chunkMd.getCoord().chunkXPos << 4) + (x + offset.x);
final int blockZ = (chunkMd.getCoord().chunkZPos << 4) + (z + offset.z);
ChunkMD targetChunkMd;

if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkZPos)
long coord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (coord == chunkMd.asLong())
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
targetChunkMd = dataCache.getChunkMD(coord);
}

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

if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkXPos)
long coord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (coord == chunkMd.asLong())
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
targetChunkMd = dataCache.getChunkMD(coord);
}

if (targetChunkMd != null)
Expand Down
57 changes: 39 additions & 18 deletions src/main/java/journeymap/client/data/DataCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,39 @@

package journeymap.client.data;

import com.google.common.cache.*;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import journeymap.client.JourneymapClient;
import journeymap.client.log.LogFormatter;
import journeymap.client.model.*;
import journeymap.client.model.ChunkMD;
import journeymap.client.model.EntityDTO;
import journeymap.client.model.MapType;
import journeymap.client.model.RegionCoord;
import journeymap.client.model.RegionImageCache;
import journeymap.client.model.RegionImageSet;
import journeymap.client.model.Waypoint;
import journeymap.client.render.draw.DrawEntityStep;
import journeymap.client.render.draw.DrawWayPointStep;
import journeymap.client.waypoint.WaypointStore;
import journeymap.common.Journeymap;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.world.ChunkCoordIntPair;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -49,6 +70,8 @@ public class DataCache
private final int chunkCacheExpireSeconds = 30;
private final int defaultConcurrencyLevel = 1;

final TLongObjectMap<ChunkCoordIntPair> longCoordMap = new TLongObjectHashMap<>();

// Private constructor
private DataCache()
{
Expand Down Expand Up @@ -381,25 +404,23 @@ public DrawWayPointStep getDrawWayPointStep(Waypoint waypoint)
return waypointDrawSteps.getUnchecked(waypoint);
}
}
// public RGB getColor(Color color)
// {
// return getColor(color.getRGB());
// }
//
// public RGB getColor(int rgbInt)
// {
// synchronized (colors)
// {
// return colors.getUnchecked(rgbInt);
// }
// }

public ChunkMD getChunkMD(ChunkCoordIntPair coord)

public ChunkMD getChunkMD(long coordLong)
{

synchronized (chunkMetadata)
{
ChunkMD chunkMD = null;

ChunkCoordIntPair coord = longCoordMap.get(coordLong);
if (coord == null)
{
int x = (int) (coordLong & 4294967295L);
int z = (int) (coordLong >>> 32 & 4294967295L);
coord = new ChunkCoordIntPair(x, z);
longCoordMap.put(coordLong, coord);
}

try
{
chunkMD = chunkMetadata.getUnchecked(coord);
Expand Down Expand Up @@ -480,7 +501,7 @@ public void purge()
{
// Flush images, do syncronously to ensure it's done before cache invalidates
RegionImageCache.instance().flushToDisk(false);

longCoordMap.clear();
synchronized (managedCaches)
{
for (Cache cache : managedCaches.keySet())
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/journeymap/client/data/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static boolean playerIsUnderground(Minecraft mc, EntityPlayer player)
{
y = posY + 1;

ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
if (chunkMD != null)
{
if (chunkMD.ceiling(x & 15, z & 15) <= y)
Expand Down Expand Up @@ -91,10 +91,7 @@ public EntityDTO load(Class aClass) throws Exception
*/
private String getPlayerBiome(EntityPlayer player)
{
int x = (MathHelper.floor_double(player.posX) >> 4) & 15;
int z = (MathHelper.floor_double(player.posZ) >> 4) & 15;

ChunkMD playerChunk = DataCache.instance().getChunkMD(new ChunkCoordIntPair(player.chunkCoordX, player.chunkCoordZ));
ChunkMD playerChunk = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(player.chunkCoordX, player.chunkCoordZ));
if (playerChunk != null)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public BiomeGenBase getBiome(ChunkMD chunkMD, int x, int y, int z)
@Override
public BiomeGenBase getBiome(int x, int y, int z)
{
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
return getBiome(chunkMD, x, y, z);
}

Expand Down Expand Up @@ -449,7 +449,7 @@ class JmBlockAccess implements IBlockAccess
{
private Chunk getChunk(int x, int z)
{
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(x >> 4, z >> 4));
ChunkMD chunkMD = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
if (chunkMD != null && chunkMD.hasChunk())
{
return chunkMD.getChunk();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/journeymap/client/model/ChunkMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ public boolean canBlockSeeTheSky(int x, int y, int z)
return ForgeHelper.INSTANCE.canBlockSeeTheSky(getChunk(), x, y, z);
}

public long asLong()
{
return ChunkCoordIntPair.chunkXZ2Int(coord.chunkXPos, coord.chunkZPos);
}

public ChunkCoordIntPair getCoord()
{
return coord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void performTask(Minecraft mc, JourneymapClient jm, File jmWorldDir, bool
}

ChunkCoordIntPair coord = chunkIter.next();
ChunkMD chunkMd = DataCache.instance().getChunkMD(coord);
ChunkMD chunkMd = DataCache.instance().getChunkMD(ChunkCoordIntPair.chunkXZ2Int(coord.chunkXPos, coord.chunkZPos));
if (chunkMd != null && chunkMd.hasChunk())
{
try
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/journeymap/client/task/multi/RenderSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ protected Collection<ChunkCoordIntPair> getRenderAreaCoords()
if (primaryRenderCoords == null || primaryRenderCoords.isEmpty())
{
List<Offset> primaryOffsets = offsets.get(primaryRenderDistance);
primaryRenderCoords = new ArrayList<ChunkCoordIntPair>(primaryOffsets.size());
primaryRenderCoords = new ArrayList<>(primaryOffsets.size());
for (Offset offset : primaryOffsets)
{
ChunkCoordIntPair primaryCoord = offset.from(lastPlayerCoord);
primaryRenderCoords.add(primaryCoord);
dataCache.getChunkMD(primaryCoord);
long coord = ChunkCoordIntPair.chunkXZ2Int(primaryCoord.chunkXPos, primaryCoord.chunkZPos);
dataCache.getChunkMD(coord);
}
}

Expand All @@ -219,7 +220,8 @@ protected Collection<ChunkCoordIntPair> getRenderAreaCoords()
{
ChunkCoordIntPair secondaryCoord = offset.from(lastPlayerCoord);
renderCoords.add(secondaryCoord);
dataCache.getChunkMD(secondaryCoord);
long coord = ChunkCoordIntPair.chunkXZ2Int(secondaryCoord.chunkXPos, secondaryCoord.chunkZPos);
dataCache.getChunkMD(coord);
}

return renderCoords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import journeymap.client.ui.option.LocationFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.chunk.Chunk;

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

Expand Down