From 44b0bcd776679c94c163690bbd206fb70630169e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 9 Mar 2025 13:34:47 +0700 Subject: [PATCH] Use nested caches for enormous placements instead of weak hash map Should recycle memory better --- .../worldgen/placement/EnormousPlacement.kt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/placement/EnormousPlacement.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/placement/EnormousPlacement.kt index b2580eef4..7c84d119e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/placement/EnormousPlacement.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/placement/EnormousPlacement.kt @@ -66,20 +66,22 @@ class EnormousPlacement( } } - private val level2cache = WeakHashMap>() - private val lock = ReentrantLock() + private val level2cache = Caffeine.newBuilder() + .scheduler(Scheduler.systemScheduler()) + .executor(Util.backgroundExecutor()) + .expireAfterAccess(Duration.ofMinutes(10)) + .weakKeys() + .build>() private fun getCache(level: WorldGenLevel): Cache { - return lock.withLock { - level2cache.computeIfAbsent(level) { - Caffeine.newBuilder() - .scheduler(Scheduler.systemScheduler()) - .executor(Util.backgroundExecutor()) - .maximumSize(16384L) - .expireAfterWrite(Duration.ofMinutes(5)) - .softValues() - .build() - } + return level2cache.get(level) { + Caffeine.newBuilder() + .scheduler(Scheduler.systemScheduler()) + .executor(Util.backgroundExecutor()) + .maximumSize(16384L) + .expireAfterWrite(Duration.ofMinutes(5)) + .softValues() + .build() } }