Skip to content

Commit

Permalink
Reset RegexIDFilter interner after recipes are loaded (#904)
Browse files Browse the repository at this point in the history
This avoids many RegexIDFilter instances remaining in memory
and holding on to match caches
  • Loading branch information
embeddedt authored Sep 26, 2024
1 parent 5c8b4cf commit c0f67bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -428,6 +429,8 @@ public void post(RecipeManagerKJS recipeManager, Map<ResourceLocation, JsonEleme
ConsoleJS.SERVER.info(r.getOrCreateId() + ": " + r.json);
}
}

RegexIDFilter.clearInternCache();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public class RegexIDFilter implements RecipeFilter {
private final Pattern pattern;
private final ConcurrentHashMap<ResourceLocation, Boolean> matchCache = new ConcurrentHashMap<>();

private static final Interner<RegexIDFilter> INTERNER = Interners.newStrongInterner();
private static Interner<RegexIDFilter> INTERNER;

static {
clearInternCache();
}

private RegexIDFilter(Pattern i) {
pattern = i;
Expand All @@ -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());
Expand Down

0 comments on commit c0f67bf

Please sign in to comment.