Skip to content

Commit

Permalink
Revert "Many ChunkCoordIntPair switched to long values for reduced ob…
Browse files Browse the repository at this point in the history
…ject allocations"

This reverts commit 597324b
  • Loading branch information
mysticdrew committed Mar 9, 2024
1 parent 755f77a commit 9360731
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 66 deletions.
1 change: 0 additions & 1 deletion doc/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ <h1>JourneyMap ${version} for Minecraft ${mcversion}</h1>

<p>New in ${version}</p>
<ul>
<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>
Expand Down
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 =
accessTransformersFile = journeymap_at.cfg

# 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,6 +37,7 @@ 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
{
boolean render(final ChunkPainter g2D, final ChunkMD chunkStub, final Integer vSlice);
public boolean render(final ChunkPainter g2D, final ChunkMD chunkStub, final Integer vSlice);

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package journeymap.client.cartography;

import net.minecraft.world.ChunkCoordIntPair;

public class MutableChunkCoordIntPair extends ChunkCoordIntPair {

public MutableChunkCoordIntPair(int p_i1947_1_, int p_i1947_2_) {
super(p_i1947_1_, p_i1947_2_);

}

public MutableChunkCoordIntPair setChunkXPos(int chunkXPos){
this.chunkXPos = chunkXPos;
return this;
}
public MutableChunkCoordIntPair setChunkZPos(int chunkZPos){
this.chunkZPos = chunkZPos;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -32,9 +33,10 @@
*
* @author techbrew
*/
public abstract class BaseRenderer implements IChunkRenderer, RemovalListener<Long, ChunkMD>
public abstract class BaseRenderer implements IChunkRenderer, RemovalListener<ChunkCoordIntPair, 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 @@ -201,7 +203,7 @@ protected Float[][] populateSlopes(final ChunkMD chunkMd, Integer vSlice,
final SlopesCache chunkSlopes)
{

Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoordLong());
Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoord());
int y = 0, sliceMinY = 0, sliceMaxY = 0;
boolean isSurface = (vSlice == null);
float slope, primarySlope, secondarySlope;
Expand Down Expand Up @@ -289,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;
final long targetCoord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (targetCoord == ChunkCoordIntPair.chunkXZ2Int(chunkMd.getCoord().chunkXPos, chunkMd.getCoord().chunkZPos))

if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkZPos)
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(targetCoord);
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
}

if (targetChunkMd != null)
Expand Down Expand Up @@ -384,14 +386,33 @@ 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.getCoordLong());
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoord());
if (heights == null)
{
// Not in cache anymore
Expand Down Expand Up @@ -478,14 +499,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;
final long targetCoord = ChunkCoordIntPair.chunkXZ2Int(blockX >> 4, blockZ >> 4);
if (targetCoord == ChunkCoordIntPair.chunkXZ2Int(chunkMd.getCoord().chunkXPos, chunkMd.getCoord().chunkZPos))

if (blockX >> 4 == chunkMd.getCoord().chunkXPos && blockZ >> 4 == chunkMd.getCoord().chunkXPos)
{
targetChunkMd = chunkMd;
}
else
{
targetChunkMd = dataCache.getChunkMD(targetCoord);
targetChunkMd = dataCache.getChunkMD(coordinates.setChunkXPos(blockX >> 4).setChunkZPos(blockZ >> 4));
}

if (targetChunkMd != null)
Expand Down Expand Up @@ -565,16 +586,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<Long, Integer[][]>
public class HeightsCache extends ForwardingLoadingCache<ChunkCoordIntPair, Integer[][]>
{
final LoadingCache<Long, Integer[][]> internal;
final LoadingCache<ChunkCoordIntPair, Integer[][]> internal;

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

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

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

@Override
protected LoadingCache<Long, Float[][]> delegate()
protected LoadingCache<ChunkCoordIntPair, 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.getCoordLong());
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoord());
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<Long, ChunkMD> notification)
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
{
synchronized (chunkLock)
{
long coord = notification.getKey();
ChunkCoordIntPair 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.getCoordLong());
Integer[][] blockSliceHeights = chunkHeights.getUnchecked(chunkMd.getCoord());
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<Long, ChunkMD> notification)
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
{
synchronized (chunkLock)
{
long coord = notification.getKey();
ChunkCoordIntPair 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.getCoordLong());
Integer[][] heights = chunkHeights.getUnchecked(chunkMd.getCoord());
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.getCoordLong());
Float[][] slopes = chunkSlopes.getUnchecked(chunkMd.getCoord());
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<Long, ChunkMD> notification)
public void onRemoval(RemovalNotification<ChunkCoordIntPair, ChunkMD> notification)
{
synchronized (chunkLock)
{
long coord = notification.getKey();
ChunkCoordIntPair coord = notification.getKey();
chunkSurfaceHeights.invalidate(coord);
chunkSurfaceSlopes.invalidate(coord);
columnPropertiesCache.invalidate(coord);
Expand Down
26 changes: 15 additions & 11 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<Long, ChunkMD> chunkMetadata;
final ProxyRemovalListener<Long, ChunkMD> chunkMetadataRemovalListener;
final LoadingCache<ChunkCoordIntPair, ChunkMD> chunkMetadata;
final ProxyRemovalListener<ChunkCoordIntPair, ChunkMD> chunkMetadataRemovalListener;
final Map<Cache, String> managedCaches = new HashMap<>();
final Map<Cache, String> privateCaches = new WeakHashMap<>();
private final int chunkCacheExpireSeconds = 30;
Expand Down Expand Up @@ -100,11 +100,8 @@ private DataCache()
regionImageSets = RegionImageCache.initRegionImageSetsCache(getCacheBuilder());
managedCaches.put(regionImageSets, "RegionImageSet");

chunkMetadataRemovalListener = new ProxyRemovalListener<>();
chunkMetadata = getCacheBuilder()
.expireAfterAccess(chunkCacheExpireSeconds, TimeUnit.SECONDS)
.removalListener(chunkMetadataRemovalListener)
.build(new ChunkMD.SimpleCacheLoader());
chunkMetadataRemovalListener = new ProxyRemovalListener<ChunkCoordIntPair, ChunkMD>();
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 @@ -397,7 +394,7 @@ public DrawWayPointStep getDrawWayPointStep(Waypoint waypoint)
// }
// }

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

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

public void invalidateChunkMD(long coord)
public void invalidateChunkMD(ChunkCoordIntPair coord)
{
synchronized (chunkMetadata)
{
Expand All @@ -450,7 +454,7 @@ public void invalidateChunkMDCache()
}
}

public void addChunkMDListener(RemovalListener<Long, ChunkMD> listener)
public void addChunkMDListener(RemovalListener<ChunkCoordIntPair, ChunkMD> listener)
{
synchronized (chunkMetadataRemovalListener)
{
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(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
ChunkMD chunkMD = DataCache.instance().getChunkMD(new ChunkCoordIntPair(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(ChunkCoordIntPair.chunkXZ2Int(player.chunkCoordX, player.chunkCoordZ));
ChunkMD playerChunk = DataCache.instance().getChunkMD(new ChunkCoordIntPair(player.chunkCoordX, player.chunkCoordZ));
if (playerChunk != null)
{
try
Expand Down
Loading

0 comments on commit 9360731

Please sign in to comment.