-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a LRU cache for filter string -> compiled SmartFilter object
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
common/src/main/java/dev/ftb/mods/ftbfiltersystem/util/FilterCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.ftb.mods.ftbfiltersystem.util; | ||
|
||
import dev.ftb.mods.ftbfiltersystem.api.FilterException; | ||
import dev.ftb.mods.ftbfiltersystem.api.filter.SmartFilter; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; | ||
|
||
/** | ||
* An LRU cache to store known filter strings and the SmartFilter objects created from them. | ||
*/ | ||
public enum FilterCache { | ||
INSTANCE; | ||
|
||
private static final int MAX_SIZE = 1000; | ||
private final Object2ObjectLinkedOpenHashMap<String,SmartFilter> cache = new Object2ObjectLinkedOpenHashMap<>(); | ||
|
||
public SmartFilter getOrCreateFilter(String filterStr) throws FilterException { | ||
if (cache.containsKey(filterStr)) { | ||
return cache.getAndMoveToFirst(filterStr); | ||
} else { | ||
SmartFilter filter; | ||
try { | ||
filter = FilterParser.parseRaw(filterStr); | ||
} catch (FilterException e) { | ||
filter = null; | ||
} | ||
if (cache.size() >= MAX_SIZE) { | ||
cache.removeLast(); | ||
} | ||
cache.put(filterStr, filter); | ||
return filter; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters