Skip to content

Commit

Permalink
Merge branch '1.20.4' into 1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Feb 3, 2025
2 parents 0774c36 + 8c2232d commit 6fdd7ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import xaeroplus.Globals;

import java.util.concurrent.ThreadLocalRandom;

public class DirectChunkHighlightDrawFeature implements ChunkHighlightDrawFeature {
private final DirectChunkHighlightProvider chunkHighlightProvider;
private final HighlightDrawBuffer drawBuffer = new HighlightDrawBuffer();
Expand Down Expand Up @@ -32,7 +34,7 @@ public Long2LongMap getChunkHighlights() {
public void render(boolean worldmap) {
Long2LongMap highlights = getChunkHighlights();
if (lastRefreshedHighlightCount != highlights.size()
&& System.currentTimeMillis() - drawBuffer.lastRefreshed > REFRESH_INTERVAL_MS) {
&& System.currentTimeMillis() - drawBuffer.lastRefreshed > REFRESH_INTERVAL_MS + ThreadLocalRandom.current().nextInt(0, 100)) {
this.invalidateCache();
lastRefreshedHighlightCount = highlights.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import it.unimi.dsi.fastutil.longs.*;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -95,19 +98,22 @@ private ListenableFuture<?> flushChunksOutsideWindow(final int windowRegionX, fi
var chunkXMax = regionCoordToChunkCoord(windowRegionX + windowRegionSize);
var chunkZMin = regionCoordToChunkCoord(windowRegionZ - windowRegionSize);
var chunkZMax = regionCoordToChunkCoord(windowRegionZ + windowRegionSize);
for (var it = Long2LongMaps.fastIterator(chunks); it.hasNext(); ) {
var entry = it.next();
final long chunkPos = entry.getLongKey();
// critical section in mc client tick thread
// it would have huge benefits if we could optimize this
// there is no cap on the size of the chunks map
// so this can require iterating through millions of entries
for (var it = chunks.keySet().longIterator(); it.hasNext(); ) {
var chunkPos = it.nextLong();
final int chunkX = ChunkUtils.longToChunkX(chunkPos);
final int chunkZ = ChunkUtils.longToChunkZ(chunkPos);
if (chunkX < chunkXMin
|| chunkX > chunkXMax
|| chunkZ < chunkZMin
|| chunkZ > chunkZMax) {
it.remove();
if (staleChunks.contains(chunkPos)) {
dataBuf.put(chunkPos, entry.getLongValue());
dataBuf.put(chunkPos, chunks.get(chunkPos));
}
it.remove();
}
}
return dbExecutor.submit(() -> database.insertHighlightList(dataBuf, dimension));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ChunkHighlightSavingCache implements ChunkHighlightCache, Closeable
// executor used for single threaded tasks that involve changing worlds and preparing the cache for operations
@NotNull private final ListeningExecutorService parentExecutor;
private final Map<ResourceKey<Level>, ChunkHighlightCacheDimensionHandler> dimensionCacheMap = new ConcurrentHashMap<>(3);
private final Queue<Runnable> taskQueue = new LinkedBlockingDeque<>();
private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<>();
Minecraft mc = Minecraft.getInstance();

public ChunkHighlightSavingCache(final @NotNull String databaseName) {
Expand Down Expand Up @@ -308,15 +308,16 @@ public int getMinimapRegionWindowSize() {
public void handleTick() {
if (!cacheReady.get()) return;
if (XaeroWorldMapCore.currentSession == null) return;
if (tickCounter % 1200 == 0) {
getAllCaches().forEach(ChunkHighlightCacheDimensionHandler::writeStaleHighlightsToDatabase);
}
// limit so we don't overflow
if (tickCounter > 2400) tickCounter = 0;
// reduce likelihood of all caches updating at the same time
// changing the window involves iterating through every chunk in the cache to find which are now outside the window
// which can be expensive if there are thousands of cache entries
// this does make the update interval setting kind of a lie, but its for the best
int jitter = ThreadLocalRandom.current().nextInt(0, 10);
// only update window on an interval
if (++tickCounter % Settings.REGISTRY.cacheWindowUpdateInterval.getAsInt() != 0) {
if (++tickCounter < Settings.REGISTRY.cacheWindowUpdateInterval.getAsInt() + jitter) {
return;
}
tickCounter = 0;

final ResourceKey<Level> mapDimension = Globals.getCurrentDimensionId();
final ResourceKey<Level> actualDimension = ChunkUtils.getActualDimension();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public void onXaeroWorldChange(XaeroWorldChangeEvent event) {
@EventHandler
public void onClientTickEvent(final ClientTickEvent.Post event) {
try {
// long before = System.nanoTime();
cache.handleTick();
// long after = System.nanoTime();
// long duration = after - before;
// if (duration > TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS)) {
// XaeroPlus.LOGGER.warn("Cache {} took {} ms to tick", dbName, TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS));
// }
} catch (final Exception e) {
XaeroPlus.LOGGER.error("Error handling tick event for cache: {} event: {}", dbName, event, e);
}
Expand Down

0 comments on commit 6fdd7ad

Please sign in to comment.