Skip to content

Commit

Permalink
Slightly optimize fastmap cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jul 2, 2023
1 parent 384efd4 commit 658d4b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
19 changes: 9 additions & 10 deletions src/main/java/xaeroplus/mixin/client/MixinMapWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
import xaero.map.misc.Misc;
import xaero.map.region.*;
import xaeroplus.settings.XaeroPlusSettingRegistry;
import xaeroplus.util.ChunkUtils;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -54,15 +53,15 @@ public abstract class MixinMapWriter {
// insert our own limiter on new tiles being written but this one's keyed on the actual chunk
// tile "writes" also include a lot of extra operations and lookups before any writing is actually done
// when we remove existing limiters those extra operations add up to a lot of unnecessary cpu time
private final Cache<String, Instant> tileUpdateCache = Caffeine.newBuilder()
private final Cache<Long, Long> tileUpdateCache = Caffeine.newBuilder()
// I would usually expect even second long expiration here to be fine
// but there are some operations that make repeat invocations actually required
// perhaps another time ill rewrite those. Or make the cache lock more aware of when we don't have any new updates to write/load
// there's still alot of performance and efficiency on the table to regain
// but i think this is a good middle ground for now
.maximumSize(1000)
.maximumSize(10000)
.expireAfterWrite(5L, TimeUnit.SECONDS)
.<String, Instant>build();
.<Long, Long>build();
public long writeFreeSinceLastWrite = -1L;
@Shadow
private int X;
Expand Down Expand Up @@ -720,17 +719,17 @@ public void writeChunk(
final CallbackInfoReturnable<Boolean> cir) {
if (!XaeroPlusSettingRegistry.fastMapSetting.getValue()) return;

final String cacheable = chunkX + " " + chunkZ;
final Instant cacheValue = tileUpdateCache.getIfPresent(cacheable);
final Long cacheable = ChunkUtils.chunkPosToLong(chunkX, chunkZ);
final Long cacheValue = tileUpdateCache.getIfPresent(cacheable);
if (nonNull(cacheValue)) {
if (cacheValue.isBefore(Instant.now().minus(Duration.ofMillis((long) XaeroPlusSettingRegistry.mapWriterDelaySetting.getValue())))) {
tileUpdateCache.put(cacheable, Instant.now());
if (cacheValue < System.currentTimeMillis() - (long) XaeroPlusSettingRegistry.fastMapWriterDelaySetting.getValue()) {
tileUpdateCache.put(cacheable, System.currentTimeMillis());
} else {
cir.setReturnValue(false);
cir.cancel();
}
} else {
tileUpdateCache.put(cacheable, Instant.now());
tileUpdateCache.put(cacheable, System.currentTimeMillis());
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/xaeroplus/settings/XaeroPlusSettingRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public final class XaeroPlusSettingRegistry {
public static final XaeroPlusBooleanSetting fastMapSetting = XaeroPlusBooleanSetting.create("Fast Mapping",
"Increases mapping speed, might hurt FPS. Adjust rate limit and delay to regain FPS.",
true, SettingLocation.WORLD_MAP_MAIN);
public static final XaeroPlusFloatSetting mapWriterDelaySetting = XaeroPlusFloatSetting.create("Fast Mapping Delay",
10, 1000, 10,
"Fast Mapping must be enabled. \n " +
"This is roughly the delay in milliseconds between minimap update operations, both render and actual file writes.",
50, SettingLocation.WORLD_MAP_MAIN);
public static final XaeroPlusFloatSetting fastMapWriterDelaySetting = XaeroPlusFloatSetting.create("Fast Mapping Delay",
10, 1000, 10,
"Fast Mapping must be enabled. \n " +
"This is roughly the delay in milliseconds between minimap update operations, both render and actual file writes.",
250, SettingLocation.WORLD_MAP_MAIN);
public static final XaeroPlusFloatSetting fastMapMaxTilesPerCycle = XaeroPlusFloatSetting.create("Fast Mapping Rate Limit",
10, 120, 10,
"Fast Mapping must be enabled. \n " +
Expand Down

0 comments on commit 658d4b0

Please sign in to comment.