From c0f67bf14c8f722709d714af5dd29608329bb839 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:21:21 -0400 Subject: [PATCH] Reset RegexIDFilter interner after recipes are loaded (#904) This avoids many RegexIDFilter instances remaining in memory and holding on to match caches --- .../latvian/mods/kubejs/recipe/RecipesKubeEvent.java | 3 +++ .../mods/kubejs/recipe/filter/RegexIDFilter.java | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java index ee617e57a..59888fd19 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java @@ -19,6 +19,7 @@ import dev.latvian.mods.kubejs.recipe.filter.IDFilter; import dev.latvian.mods.kubejs.recipe.filter.OrFilter; import dev.latvian.mods.kubejs.recipe.filter.RecipeFilter; +import dev.latvian.mods.kubejs.recipe.filter.RegexIDFilter; import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo; import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema; import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaStorage; @@ -428,6 +429,8 @@ public void post(RecipeManagerKJS recipeManager, Map matchCache = new ConcurrentHashMap<>(); - private static final Interner INTERNER = Interners.newStrongInterner(); + private static Interner INTERNER; + + static { + clearInternCache(); + } private RegexIDFilter(Pattern i) { pattern = i; @@ -24,6 +28,10 @@ public static RegexIDFilter of(Pattern i) { return INTERNER.intern(new RegexIDFilter(i)); } + public static void clearInternCache() { + INTERNER = Interners.newStrongInterner(); + } + @Override public boolean test(Context cx, RecipeLikeKJS recipe) { return matchCache.computeIfAbsent(recipe.kjs$getOrCreateId(), location -> pattern.matcher(location.toString()).find());