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
This commit is contained in:
Brian Fopiano committed 2026-11-27 15:52:08 -05:00
1 parent a5918c17a7
commit 18bd887c8f
3 files changed
+60 -25

No files matched your search

@@ -311,12 +311,12 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
protected void warnMissingOnce(String name) { protected void warnMissingOnce(String name) {
IrisLogging.warnOnce(manager.onceKey(MISSING_KEY_KIND + resourceTypeName, 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) { protected void warnLiteralNullOnce(String action) {
IrisLogging.warnOnce(manager.onceKey(LITERAL_NULL_KEY_KIND + resourceTypeName, 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() + ")");
} }
/** /**
@@ -167,20 +167,37 @@ public class IrisLoggingTest {
} }
@Test @Test
public void aKeyPastTheCapIsStillStatedWhileClaimedKeysStayQuiet() { public void pastTheCapTheLeastRecentlySeenKeyIsDroppedAndWarnsAgain() {
String claimed = uniqueKey(); String stale = uniqueKey();
assertTrue(IrisLogging.warnOnce(claimed, "claimed")); String recurring = uniqueKey();
for (int filler = 1; filler < IrisLogging.MAX_ONCE_KEYS; filler++) { assertTrue(IrisLogging.warnOnce(stale, "stale"));
assertTrue(IrisLogging.warnOnce(recurring, "recurring"));
for (int filler = 2; filler < IrisLogging.MAX_ONCE_KEYS; filler++) {
IrisLogging.warnOnce(uniqueKey(), "filler"); IrisLogging.warnOnce(uniqueKey(), "filler");
} }
String unclaimable = uniqueKey(); assertFalse(IrisLogging.warnOnce(recurring, "recurring"));
String newer = uniqueKey();
levels.clear(); levels.clear();
assertTrue(IrisLogging.warnOnce(unclaimable, "new condition")); assertTrue(IrisLogging.warnOnce(newer, "new condition"));
assertTrue(IrisLogging.warnOnce(unclaimable, "new condition")); assertFalse(IrisLogging.warnOnce(newer, "new condition"));
assertFalse(IrisLogging.warnOnce(claimed, "claimed")); 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() { private void bindCapturingPlatform() {
@@ -18,9 +18,11 @@
package art.arcane.iris.spi; package art.arcane.iris.spi;
import java.util.Collections;
import java.util.IllegalFormatException; import java.util.IllegalFormatException;
import java.util.Set; import java.util.LinkedHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@@ -36,7 +38,13 @@ import java.util.regex.Pattern;
*/ */
public final class IrisLogging { public final class IrisLogging {
static final int MAX_ONCE_KEYS = 512; static final int MAX_ONCE_KEYS = 512;
private static final Set<String> ONCE_KEYS = ConcurrentHashMap.newKeySet(); private static final Map<String, Boolean> ONCE_KEYS = Collections.synchronizedMap(
new LinkedHashMap<>(64, 0.75F, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Boolean> eldest) {
return size() > MAX_ONCE_KEYS;
}
});
private static final Pattern LEGACY_COLOR = Pattern.compile("(?i)\\u00a7[0-9A-FK-ORX]"); private static final Pattern LEGACY_COLOR = Pattern.compile("(?i)\\u00a7[0-9A-FK-ORX]");
private static final Pattern MINI_MESSAGE_TAG = Pattern.compile("(?i)</?(?:reset|bold|b|italic|i|underlined?|u|strikethrough|st|obfuscated?|obf|black|dark_blue|dark_green|dark_aqua|dark_red|dark_purple|gold|gray|dark_gray|blue|green|aqua|red|light_purple|yellow|white|gradient|font|hover|click|rainbow)(?::[^>\\n]{0,96})?>|<#[0-9a-f]{6}>"); private static final Pattern MINI_MESSAGE_TAG = Pattern.compile("(?i)</?(?:reset|bold|b|italic|i|underlined?|u|strikethrough|st|obfuscated?|obf|black|dark_blue|dark_green|dark_aqua|dark_red|dark_purple|gold|gray|dark_gray|blue|green|aqua|red|light_purple|yellow|white|gradient|font|hover|click|rainbow)(?::[^>\\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 * 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 * 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 * 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 * or other one-off detail to it. At most {@link #MAX_ONCE_KEYS} keys are held; past that the least recently
* stated at {@link LogLevel#WARN} every time: repeating is the lesser harm than hiding a new condition. * seen key is dropped and warns again the next time it comes up.
* *
* @see #resetOnceKeys() * @see #resetOnceKeys()
*/ */
@@ -97,6 +105,19 @@ public final class IrisLogging {
return once(LogLevel.WARN, key, format, args); 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<String> 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}. * {@link #warnOnce(String, String, Object...)} at {@link LogLevel#ERROR}.
*/ */
@@ -210,7 +231,9 @@ public final class IrisLogging {
return; 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) { 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 * 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: * a leak. Eviction is least recently seen, so a condition that keeps recurring stays claimed while one
* it is stated at its own level every time rather than hidden at debug. * that stopped makes room for new ones.
*/ */
private static boolean isRepeat(String key) { private static boolean isRepeat(String key) {
String claimable = key == null ? "null" : key; return ONCE_KEYS.put(key == null ? "null" : key, Boolean.TRUE) != null;
if (ONCE_KEYS.size() >= MAX_ONCE_KEYS) {
return ONCE_KEYS.contains(claimable);
}
return !ONCE_KEYS.add(claimable);
} }
private static void emit(LogLevel level, String message) { private static void emit(LogLevel level, String message) {