From 658d4b048f406cedae24f6d010c51dd9d4c0df08 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Sat, 1 Jul 2023 21:17:54 -0700 Subject: [PATCH] Slightly optimize fastmap cache --- .../mixin/client/MixinMapWriter.java | 19 +++++++++---------- .../settings/XaeroPlusSettingRegistry.java | 10 +++++----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/xaeroplus/mixin/client/MixinMapWriter.java b/src/main/java/xaeroplus/mixin/client/MixinMapWriter.java index 2fdbfefa..35ddef04 100644 --- a/src/main/java/xaeroplus/mixin/client/MixinMapWriter.java +++ b/src/main/java/xaeroplus/mixin/client/MixinMapWriter.java @@ -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; @@ -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 tileUpdateCache = Caffeine.newBuilder() + private final Cache 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) - .build(); + .build(); public long writeFreeSinceLastWrite = -1L; @Shadow private int X; @@ -720,17 +719,17 @@ public void writeChunk( final CallbackInfoReturnable 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()); } } diff --git a/src/main/java/xaeroplus/settings/XaeroPlusSettingRegistry.java b/src/main/java/xaeroplus/settings/XaeroPlusSettingRegistry.java index a0a54ee5..79d861a9 100644 --- a/src/main/java/xaeroplus/settings/XaeroPlusSettingRegistry.java +++ b/src/main/java/xaeroplus/settings/XaeroPlusSettingRegistry.java @@ -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 " +