From 18bd887c8f730131270a44d32f7b3e93e0900feb Mon Sep 17 00:00:00 2001 From: Brian Fopiano Date: Fri, 27 Nov 2026 15:52:08 -0500 Subject: [PATCH] Evict old once-keys, defer caller lookup - Drop least recently seen once-key past the cap - Add supplier warnOnce that builds the message only when stated - Walk the caller stack only for stated resource warnings --- .../iris/pack/loading/ResourceLoader.java | 4 +- .../art/arcane/iris/spi/IrisLoggingTest.java | 35 ++++++++++---- .../java/art/arcane/iris/spi/IrisLogging.java | 46 +++++++++++++------ 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java b/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java index 183aaacdb..26d45054c 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java @@ -311,12 +311,12 @@ public class ResourceLoader implements MeteredCache { protected void warnMissingOnce(String name) { IrisLogging.warnOnce(manager.onceKey(MISSING_KEY_KIND + resourceTypeName, name), - "Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + () -> "Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); } protected void warnLiteralNullOnce(String action) { IrisLogging.warnOnce(manager.onceKey(LITERAL_NULL_KEY_KIND + resourceTypeName, action), - "Refusing " + resourceTypeName + " " + action + " for literal string \"null\" (called by " + callerHint() + ")"); + () -> "Refusing " + resourceTypeName + " " + action + " for literal string \"null\" (called by " + callerHint() + ")"); } /** diff --git a/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java b/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java index 5c270ec1c..cb3e24cb8 100644 --- a/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java +++ b/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java @@ -167,20 +167,37 @@ public class IrisLoggingTest { } @Test - public void aKeyPastTheCapIsStillStatedWhileClaimedKeysStayQuiet() { - String claimed = uniqueKey(); - assertTrue(IrisLogging.warnOnce(claimed, "claimed")); - for (int filler = 1; filler < IrisLogging.MAX_ONCE_KEYS; filler++) { + public void pastTheCapTheLeastRecentlySeenKeyIsDroppedAndWarnsAgain() { + String stale = uniqueKey(); + String recurring = uniqueKey(); + assertTrue(IrisLogging.warnOnce(stale, "stale")); + assertTrue(IrisLogging.warnOnce(recurring, "recurring")); + for (int filler = 2; filler < IrisLogging.MAX_ONCE_KEYS; filler++) { IrisLogging.warnOnce(uniqueKey(), "filler"); } - String unclaimable = uniqueKey(); + assertFalse(IrisLogging.warnOnce(recurring, "recurring")); + String newer = uniqueKey(); levels.clear(); - assertTrue(IrisLogging.warnOnce(unclaimable, "new condition")); - assertTrue(IrisLogging.warnOnce(unclaimable, "new condition")); - assertFalse(IrisLogging.warnOnce(claimed, "claimed")); + assertTrue(IrisLogging.warnOnce(newer, "new condition")); + assertFalse(IrisLogging.warnOnce(newer, "new condition")); + assertFalse(IrisLogging.warnOnce(recurring, "recurring")); + assertTrue(IrisLogging.warnOnce(stale, "stale")); - assertEquals(List.of(LogLevel.WARN, LogLevel.WARN, LogLevel.DEBUG), levels); + assertEquals(List.of(LogLevel.WARN, LogLevel.DEBUG, LogLevel.DEBUG, LogLevel.WARN), levels); + } + + @Test + public void aSuppliedMessageIsOnlyBuiltWhenStated() { + String key = uniqueKey(); + int[] built = {0}; + + assertTrue(IrisLogging.warnOnce(key, () -> "built " + ++built[0])); + assertFalse(IrisLogging.warnOnce(key, () -> "built " + ++built[0])); + + assertEquals(1, built[0]); + assertEquals(List.of(LogLevel.WARN), levels); + assertEquals(List.of("built 1"), messages); } private void bindCapturingPlatform() { diff --git a/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java b/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java index a95d60fa5..4d029c74b 100644 --- a/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java +++ b/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java @@ -18,9 +18,11 @@ package art.arcane.iris.spi; +import java.util.Collections; import java.util.IllegalFormatException; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Supplier; import java.util.regex.Pattern; /** @@ -36,7 +38,13 @@ import java.util.regex.Pattern; */ public final class IrisLogging { static final int MAX_ONCE_KEYS = 512; - private static final Set ONCE_KEYS = ConcurrentHashMap.newKeySet(); + private static final Map ONCE_KEYS = Collections.synchronizedMap( + new LinkedHashMap<>(64, 0.75F, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_ONCE_KEYS; + } + }); private static final Pattern LEGACY_COLOR = Pattern.compile("(?i)\\u00a7[0-9A-FK-ORX]"); private static final Pattern MINI_MESSAGE_TAG = Pattern.compile("(?i)\\n]{0,96})?>|<#[0-9a-f]{6}>"); @@ -88,8 +96,8 @@ public final class IrisLogging { * Logs at {@link LogLevel#WARN} the first time {@code key} is seen and at {@link LogLevel#DEBUG} every * time after, for a condition that is worth stating once but is reached per block, per sample or per * chunk. Returns true when the warning was the one that got stated, so a caller can attach a stack trace - * or other one-off detail to it. Once {@link #MAX_ONCE_KEYS} keys are claimed, a key not among them is - * stated at {@link LogLevel#WARN} every time: repeating is the lesser harm than hiding a new condition. + * or other one-off detail to it. At most {@link #MAX_ONCE_KEYS} keys are held; past that the least recently + * seen key is dropped and warns again the next time it comes up. * * @see #resetOnceKeys() */ @@ -97,6 +105,19 @@ public final class IrisLogging { return once(LogLevel.WARN, key, format, args); } + /** + * {@link #warnOnce(String, String, Object...)} for a message that is costly to build: {@code message} is + * only called when the warning is stated, and a repeat logs nothing. + */ + public static boolean warnOnce(String key, Supplier message) { + if (isRepeat(key)) { + return false; + } + + emit(LogLevel.WARN, message == null ? "null" : String.valueOf(message.get())); + return true; + } + /** * {@link #warnOnce(String, String, Object...)} at {@link LogLevel#ERROR}. */ @@ -210,7 +231,9 @@ public final class IrisLogging { return; } - ONCE_KEYS.removeIf(key -> key.startsWith(prefix)); + synchronized (ONCE_KEYS) { + ONCE_KEYS.keySet().removeIf(key -> key.startsWith(prefix)); + } } private static boolean once(LogLevel level, String key, String format, Object... args) { @@ -226,16 +249,11 @@ public final class IrisLogging { /** * The cap is what keeps a key space nobody bounded - a block name, a biome name, a mod id - from becoming - * a leak. Past it nothing new is claimed, so a key that could not be claimed never counts as a repeat: - * it is stated at its own level every time rather than hidden at debug. + * a leak. Eviction is least recently seen, so a condition that keeps recurring stays claimed while one + * that stopped makes room for new ones. */ private static boolean isRepeat(String key) { - String claimable = key == null ? "null" : key; - if (ONCE_KEYS.size() >= MAX_ONCE_KEYS) { - return ONCE_KEYS.contains(claimable); - } - - return !ONCE_KEYS.add(claimable); + return ONCE_KEYS.put(key == null ? "null" : key, Boolean.TRUE) != null; } private static void emit(LogLevel level, String message) {