Skip to content

Commit

Permalink
Many ChunkCoordIntPair switched to long values for reduced object all…
Browse files Browse the repository at this point in the history
…ocations
  • Loading branch information
mysticdrew committed Mar 8, 2024
1 parent b79a59f commit 597324b
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 116 deletions.
7 changes: 6 additions & 1 deletion doc/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ <h1>JourneyMap ${version} for Minecraft ${mcversion}</h1>

<p>New in ${version}</p>
<ul>
<li>Performance: Cleaned up a bunch of unnecessary object allocations.</li>
<li>Performance: Cleaned up a bunch of unnecessary object allocations. (Better)</li>
<li>Fixed: scrollwheel calculation for lwjgl3</li>
<li>Fixed: unbound key errors</li>
<li>Fixed: Windows data path tokens if they are invalid</li>
<li>Fixed: Update checker urls</li>
<li>Fixed: Zip resource extraction.</li>
</ul>
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ apiPackage =
# Specify the configuration file for Forge's access transformers here. It must be placed into /src/main/resources/META-INF/
# There can be multiple files in a space-separated list.
# Example value: mymodid_at.cfg nei_at.cfg
accessTransformersFile = journeymap_at.cfg
accessTransformersFile =

# Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled!
usesMixins = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public ChunkRenderController()
SurfaceRenderer surfaceRenderer = new SurfaceRenderer();
overWorldSurfaceRenderer = surfaceRenderer;
overWorldCaveRenderer = new CaveRenderer(surfaceRenderer);
//standardRenderer = new ChunkTopoRenderer();
}

public boolean renderChunk(RegionCoord rCoord, MapType mapType, ChunkMD chunkMd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

public interface IChunkRenderer
{
public boolean render(final ChunkPainter g2D, final ChunkMD chunkStub, final Integer vSlice);
boolean render(final ChunkPainter g2D, final ChunkMD chunkStub, final Integer vSlice);

public void setStratumColors(Stratum stratum, int lightAttenuation, Integer waterColor, boolean waterAbove, boolean underground, boolean mapCaveLighting);
void setStratumColors(Stratum stratum, int lightAttenuation, Integer waterColor, boolean waterAbove, boolean underground, boolean mapCaveLighting);

public float[] getAmbientColor();
float[] getAmbientColor();

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.google.common.cache.*;
import journeymap.client.JourneymapClient;
import journeymap.client.cartography.IChunkRenderer;
import journeymap.client.cartography.MutableChunkCoordIntPair;
import journeymap.client.cartography.RGB;
import journeymap.client.cartography.Stratum;
import journeymap.client.data.DataCache;
Expand All @@ -33,10 +32,9 @@
*
* @author techbrew
*/
public abstract class BaseRenderer implements IChunkRenderer, RemovalListener<ChunkCoordIntPair, ChunkMD>
public abstract class BaseRenderer implements IChunkRenderer, RemovalListener<Long, ChunkMD>
{
public static final String PROP_WATER_HEIGHT = "waterHeight";
private static final MutableChunkCoordIntPair coordinates = new MutableChunkCoordIntPair(0, 0);
protected static final AlphaComposite ALPHA_OPAQUE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1F);
protected static final int COLOR_BLACK = Color.black.getRGB();
protected static final int COLOR_VOID = RGB.toInteger(17, 12, 25);
Expand Down Expand Up @@ -203,7 +201,7 @@ protected Float[][] populateSlopes(final ChunkMD chunkMd, Integer vSlice,
final SlopesCache chunkSlopes)
{

Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoord());
Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoordLong());
int y = 0, sliceMinY = 0, sliceMaxY = 0;
boolean isSurface = (vSlice == null);
Float slope, primarySlope, secondarySlope;
Expand Down Expand Up @@ -292,14 +290,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)
final long targetCoord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (targetCoord == ChunkCoordIntPair.chunkXZ2Int(chunkMd.getCoord().chunkXPos, chunkMd.getCoord().chunkZPos))
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
targetChunkMd = dataCache.getChunkMD(targetCoord);
}

if (targetChunkMd != null)
Expand Down Expand Up @@ -387,33 +385,14 @@ protected float getSlope(final ChunkMD chunkMd, final BlockMD blockMD, int x, In
return slope;
}

// /**
// * Get the height of the block at the x, z coordinate in the chunk, optionally ignoring NoShadow blocks.
// * Ignoring NoShadow blocks won't change the saved surfaceHeights.
// */
// public int getSurfaceBlockHeight(final ChunkMD chunkMd, int x, int z, boolean ignoreNoShadowBlocks)
// {
// int y = getSurfaceBlockHeight(chunkMd, x, z, ignoreWater);
// if(ignoreNoShadowBlocks)
// {
// BlockMD blockMD = dataCache.getBlockMD(chunkMd, x, y, z);
// while (y > 0 && blockMD.hasFlag(BlockMD.Flag.NoShadow) || blockMD.isAir() || (ignoreWater && (blockMD.isWater())))
// {
// y--;
// blockMD = dataCache.getBlockMD(chunkMd, x, y, z);
// }
// }
// return y;
// }

/**
* Added because getHeight() sometimes returns an air block.
* Returns the value in the height map at this x, z coordinate in the chunk, disregarding
* blocks that shouldn't be used as the top block.
*/
public Integer getSurfaceBlockHeight(final ChunkMD chunkMd, int x, int z, final HeightsCache chunkHeights)
{
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoord());
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoordLong());
if (heights == null)
{
// Not in cache anymore
Expand Down Expand Up @@ -500,14 +479,14 @@ public int getSurfaceBlockHeight(final ChunkMD chunkMd, int x, int z, BlockCoord
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().chunkXPos)
final long targetCoord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (targetCoord == ChunkCoordIntPair.chunkXZ2Int(chunkMd.getCoord().chunkXPos, chunkMd.getCoord().chunkZPos))
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
targetChunkMd = dataCache.getChunkMD(targetCoord);
}

if (targetChunkMd != null)
Expand Down Expand Up @@ -587,16 +566,16 @@ protected HashMap<String, Serializable> getColumnProperties(ChunkMD chunkMD, int
/**
* Cache for storing block heights in a 2-dimensional array, keyed to chunk coordinates.
*/
public class HeightsCache extends ForwardingLoadingCache<ChunkCoordIntPair, Integer[][]>
public class HeightsCache extends ForwardingLoadingCache<Long, Integer[][]>
{
final LoadingCache<ChunkCoordIntPair, Integer[][]> internal;
final LoadingCache<Long, Integer[][]> internal;

protected HeightsCache(String name)
{
this.internal = getCacheBuilder().build(new CacheLoader<ChunkCoordIntPair, Integer[][]>()
this.internal = getCacheBuilder().build(new CacheLoader<Long, Integer[][]>()
{
@Override
public Integer[][] load(ChunkCoordIntPair key) throws Exception
public Integer[][] load(Long key) throws Exception
{
//JourneyMap.getLogger().info("Initialized heights for chunk " + key);
return new Integer[16][16];
Expand All @@ -606,7 +585,7 @@ public Integer[][] load(ChunkCoordIntPair key) throws Exception
}

@Override
protected LoadingCache<ChunkCoordIntPair, Integer[][]> delegate()
protected LoadingCache<Long, Integer[][]> delegate()
{
return internal;
}
Expand All @@ -615,16 +594,16 @@ protected LoadingCache<ChunkCoordIntPair, Integer[][]> delegate()
/**
* Cache for storing block slopes in a 2-dimensional array, keyed to chunk coordinates.
*/
public class SlopesCache extends ForwardingLoadingCache<ChunkCoordIntPair, Float[][]>
public class SlopesCache extends ForwardingLoadingCache<Long, Float[][]>
{
final LoadingCache<ChunkCoordIntPair, Float[][]> internal;
final LoadingCache<Long, Float[][]> internal;

protected SlopesCache(String name)
{
this.internal = getCacheBuilder().build(new CacheLoader<ChunkCoordIntPair, Float[][]>()
this.internal = getCacheBuilder().build(new CacheLoader<Long, Float[][]>()
{
@Override
public Float[][] load(ChunkCoordIntPair key) throws Exception
public Float[][] load(Long key) throws Exception
{
//JourneyMap.getLogger().info("Initialized slopes for chunk " + key);
return new Float[16][16];
Expand All @@ -634,7 +613,7 @@ public Float[][] load(ChunkCoordIntPair key) throws Exception
}

@Override
protected LoadingCache<ChunkCoordIntPair, Float[][]> delegate()
protected LoadingCache<Long, Float[][]> delegate()
{
return internal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ protected boolean paintStrata(final Strata strata, final ChunkPainter painter, f
protected Integer getSliceBlockHeight(final ChunkMD chunkMd, final int x, final Integer vSlice, final int z, final int sliceMinY, final int sliceMaxY,
final HeightsCache chunkHeights)
{
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoord());
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoordLong());
if (blockSliceHeights == null)
{
return null;
Expand Down Expand Up @@ -455,11 +455,11 @@ protected int getSliceLightLevel(ChunkMD chunkMd, int x, int y, int z, boolean a
}

@Override
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
public void onRemoval(RemovalNotification<Long, ChunkMD> notification)
{
synchronized (chunkLock)
{
ChunkCoordIntPair coord = notification.getKey();
long coord = notification.getKey();
for (HeightsCache heightsCache : chunkSliceHeights)
{
if (heightsCache != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void updateOptions()
protected Integer getSliceBlockHeight(final ChunkMD chunkMd, final int x, final Integer vSlice, final int z, final int sliceMinY, final int sliceMaxY,
final HeightsCache chunkHeights)
{
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoord());
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoordLong());
if (blockSliceHeights == null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ protected boolean paintStrata(final Strata strata, final ChunkPainter dayG2d, fi
}

@Override
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
public void onRemoval(RemovalNotification<Long, ChunkMD> notification)
{
synchronized (chunkLock)
{
ChunkCoordIntPair coord = notification.getKey();
long coord = notification.getKey();
chunkSurfaceHeights.invalidate(coord);
chunkSurfaceSlopes.invalidate(coord);
columnPropertiesCache.invalidate(coord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected boolean renderSurface(final ChunkPainter painter, final ChunkMD chunkM

public Integer getSurfaceBlockHeight(final ChunkMD chunkMd, int x, int z, final HeightsCache chunkHeights)
{
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoord());
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoordLong());
if (heights == null)
{
// Not in cache anymore
Expand Down Expand Up @@ -297,7 +297,7 @@ protected Float[][] populateSlopes(final ChunkMD chunkMd, Integer vSlice,
BlockCoordIntPair offsetS = new BlockCoordIntPair(0, 1);
BlockCoordIntPair offsetE = new BlockCoordIntPair(1, 0);

Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoord());
Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoordLong());
float h;
Float slope;
float hN, hW, hE, hS;
Expand Down Expand Up @@ -385,11 +385,11 @@ protected int getBaseBlockColor(final BlockMD blockMD, int x, int y, int z)
}

@Override
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
public void onRemoval(RemovalNotification<Long, ChunkMD> notification)
{
synchronized (chunkLock)
{
ChunkCoordIntPair coord = notification.getKey();
long coord = notification.getKey();
chunkSurfaceHeights.invalidate(coord);
chunkSurfaceSlopes.invalidate(coord);
columnPropertiesCache.invalidate(coord);
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/journeymap/client/data/DataCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class DataCache
final LoadingCache<EntityLivingBase, EntityDTO> entityDTOs;
final Cache<String, RegionCoord> regionCoords;
final Cache<String, MapType> mapTypes;
final LoadingCache<ChunkCoordIntPair, ChunkMD> chunkMetadata;
final ProxyRemovalListener<ChunkCoordIntPair, ChunkMD> chunkMetadataRemovalListener;
final LoadingCache<Long, ChunkMD> chunkMetadata;
final ProxyRemovalListener<Long, ChunkMD> chunkMetadataRemovalListener;
final HashMap<Cache, String> managedCaches = new HashMap<Cache, String>();
final WeakHashMap<Cache, String> privateCaches = new WeakHashMap<Cache, String>();
private final int chunkCacheExpireSeconds = 30;
Expand Down Expand Up @@ -101,8 +101,11 @@ private DataCache()
regionImageSets = RegionImageCache.initRegionImageSetsCache(getCacheBuilder());
managedCaches.put(regionImageSets, "RegionImageSet");

chunkMetadataRemovalListener = new ProxyRemovalListener<ChunkCoordIntPair, ChunkMD>();
chunkMetadata = getCacheBuilder().expireAfterAccess(chunkCacheExpireSeconds, TimeUnit.SECONDS).removalListener(chunkMetadataRemovalListener).build(new ChunkMD.SimpleCacheLoader());
chunkMetadataRemovalListener = new ProxyRemovalListener<>();
chunkMetadata = getCacheBuilder()
.expireAfterAccess(chunkCacheExpireSeconds, TimeUnit.SECONDS)
.removalListener(chunkMetadataRemovalListener)
.build(new ChunkMD.SimpleCacheLoader());
managedCaches.put(chunkMetadata, "ChunkMD");

regionCoords = getCacheBuilder().expireAfterAccess(chunkCacheExpireSeconds, TimeUnit.SECONDS).build();
Expand Down Expand Up @@ -395,7 +398,7 @@ public DrawWayPointStep getDrawWayPointStep(Waypoint waypoint)
// }
// }

public ChunkMD getChunkMD(ChunkCoordIntPair coord)
public ChunkMD getChunkMD(long coord)
{
synchronized (chunkMetadata)
{
Expand Down Expand Up @@ -427,19 +430,12 @@ public void addChunkMD(ChunkMD chunkMD)
{
synchronized (chunkMetadata)
{
chunkMetadata.put(chunkMD.getCoord(), chunkMD);
}
}

public Set<ChunkCoordIntPair> getCachedChunkCoordinates()
{
synchronized (chunkMetadata)
{
return chunkMetadata.asMap().keySet();
chunkMetadata.put(ChunkCoordIntPair.chunkXZ2Int(chunkMD.getCoord().chunkXPos, chunkMD.getCoord().chunkZPos), chunkMD);
}
}

public void invalidateChunkMD(ChunkCoordIntPair coord)
public void invalidateChunkMD(long coord)
{
synchronized (chunkMetadata)
{
Expand All @@ -455,18 +451,14 @@ public void invalidateChunkMDCache()
}
}

public void addChunkMDListener(RemovalListener<ChunkCoordIntPair, ChunkMD> listener)
public void addChunkMDListener(RemovalListener<Long, ChunkMD> listener)
{
synchronized (chunkMetadataRemovalListener)
{
chunkMetadataRemovalListener.addDelegateListener(listener);
}
}

// public void resetBlockMetadata()
// {
// BlockMD.reset();
// }

public LoadingCache<RegionImageSet.Key, RegionImageSet> getRegionImageSets()
{
Expand Down
4 changes: 2 additions & 2 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 @@ -94,7 +94,7 @@ 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
Loading

0 comments on commit 597324b

Please sign in to comment.