From 6f47b461ee4cca54aa7e7ed96481784a637b893c Mon Sep 17 00:00:00 2001 From: BonsUnleashed Date: Sun, 4 Oct 2026 03:35:16 +0200 Subject: [PATCH] Stop the cloned section cache cleanup at the first entry that has not expired --- .../client/world/cloned/ClonedChunkSectionCache.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSectionCache.java b/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSectionCache.java index 2efde82e2..023b4170c 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSectionCache.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSectionCache.java @@ -27,8 +27,15 @@ public ClonedChunkSectionCache(Level world) { public synchronized void cleanup() { this.time = getMonotonicTimeSource(); - this.positionToEntry.values() - .removeIf(entry -> this.time > (entry.getLastUsedTimestamp() + MAX_CACHE_DURATION)); + + // The map is kept in order of last use, as acquire() moves each entry it returns to the end and stamps it with + // the current time. The expired entries are therefore always at the front, so stop at the first one which has + // not expired rather than checking every entry. + var iterator = this.positionToEntry.values().iterator(); + + while (iterator.hasNext() && this.time > (iterator.next().getLastUsedTimestamp() + MAX_CACHE_DURATION)) { + iterator.remove(); + } } @Nullable