Skip to content

Commit

Permalink
Replace memoization (caffeine lib -> hashmap)
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonEggBedrockBreaking committed Dec 24, 2023
1 parent a1327a6 commit 9f21277
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
include(modImplementation(fabricApi.module("fabric-key-binding-api-v1", project.fabric_version)))
include(modImplementation("net.caffeinemc:CaffeineConfig:${project.caffeine_config_version}"))
include(modImplementation("com.h2database:h2:${project.h2_version}"))
include(modImplementation("com.github.ben-manes.caffeine:caffeine:${project.caffeine_version}"))
include(modImplementation("com.moandjiezana.toml:toml4j:${project.toml4j_version}"))
modImplementation("com.terraformersmc:modmenu:${project.modmenu_version}")
}
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ archives_base_name = vanilla_disable
# Dependencies
caffeine_config_version=1.3.0+1.17
h2_version=2.2.224
caffeine_version=3.1.8
toml4j_version=0.7.2
modmenu_version=9.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class VanillaDisableConfig implements ModInitializer {
public static boolean autoMigration = true;
public static boolean worldLoadingScreen = true;
public static long cache = 10_000_000L;
private static String PATH = "";
private static Properties data;

Expand All @@ -39,7 +38,6 @@ private static void updateConfig() {

autoMigration = Boolean.parseBoolean(properties.getProperty("auto_migration", String.valueOf(autoMigration)));
worldLoadingScreen = Boolean.parseBoolean(properties.getProperty("world_loading_screen", String.valueOf(worldLoadingScreen)));
cache = Long.parseLong(properties.getProperty("cache", String.valueOf(cache)));

updateProperties(properties);

Expand All @@ -49,7 +47,6 @@ private static void updateConfig() {
public static void resetConfig() {
autoMigration = Boolean.parseBoolean(data.getProperty("auto_migration"));
worldLoadingScreen = Boolean.parseBoolean(data.getProperty("world_loading_screen"));
cache = Long.parseLong(data.getProperty("cache"));
}

public static void saveConfig() {
Expand All @@ -59,7 +56,6 @@ public static void saveConfig() {
private static void updateProperties(Properties data) {
data.setProperty("auto_migration", String.valueOf(autoMigration));
data.setProperty("world_loading_screen", String.valueOf(worldLoadingScreen));
data.setProperty("cache", String.valueOf(cache));

try {
data.store(new FileOutputStream(PATH), null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package uk.debb.vanilla_disable.data.command;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import it.unimi.dsi.fastutil.objects.*;
import net.minecraft.commands.Commands;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -92,7 +90,7 @@ public class CommandDataHandler {
public static Registry<StatType<?>> statTypeRegistry;
public static boolean migrated = false;
public static boolean shouldMigrate = true;
private static Cache<String, Object> cache = null;
private static final Object2ObjectMap<String, Object> memo = new Object2ObjectOpenHashMap<>();
private static Connection connection;
private static Statement statement;
private static String PATH;
Expand Down Expand Up @@ -984,8 +982,6 @@ public static void handleDatabase() {
} catch (SQLException | IOException e) {
throw new RuntimeException(e);
}

cache = Caffeine.newBuilder().maximumSize(VanillaDisableConfig.cache).build();
}

/**
Expand Down Expand Up @@ -1034,7 +1030,7 @@ public static void writeToFile(String command) {
*/
public static void setValue(String table, String row, String column, String value, boolean isString) {
if (table.isEmpty() || row.isEmpty() || column.isEmpty() || value.isEmpty()) return;
invalidateCaches();
memo.clear();
if (isString) {
value = "'" + value + "'";
}
Expand All @@ -1057,7 +1053,7 @@ public static void setValue(String table, String row, String column, String valu
*/
public static void setAll(String table, String column, String value, boolean isString) {
if (table.isEmpty() || column.isEmpty() || value.isEmpty()) return;
invalidateCaches();
memo.clear();
if (isString) {
value = "'" + value + "'";
}
Expand All @@ -1081,7 +1077,7 @@ public static void setAll(String table, String column, String value, boolean isS
*/
public static void setMatching(String table, String column, String value, boolean isString, String pattern) {
if (table.isEmpty() || column.isEmpty() || value.isEmpty() || pattern.isEmpty()) return;
invalidateCaches();
memo.clear();
if (pattern.contains(";") || pattern.contains("SELECT") || pattern.contains("ALTER")) {
throw new RuntimeException("SQL injection attempted. Command not executed.");
}
Expand Down Expand Up @@ -1135,7 +1131,7 @@ private static boolean getBoolean(String table, String row, String column) {
return bool;
} catch (SQLException | NullPointerException ignored) {
}
invalidateCaches();
memo.clear();
return Boolean.parseBoolean(getDefault(table, row, column));
}

Expand All @@ -1148,8 +1144,13 @@ private static boolean getBoolean(String table, String row, String column) {
* @return The value.
*/
public static boolean getCachedBoolean(String table, String row, String column) {
String cacheKey = table + "-" + row + "-" + column;
return (boolean) cache.get(cacheKey, key -> getBoolean(table, row, column));
String cacheKey = "getCachedBoolean-" + table + "-" + row + "-" + column;
if (memo.containsKey(cacheKey)) {
return (boolean) memo.get(cacheKey);
}
boolean bool = getBoolean(table, row, column);
memo.put(cacheKey, bool);
return bool;
}

/**
Expand All @@ -1169,7 +1170,7 @@ private static int getInt(String table, String row, String column) {
return integer;
} catch (SQLException | NullPointerException ignored) {
}
invalidateCaches();
memo.clear();
return Integer.parseInt(getDefault(table, row, column));
}

Expand All @@ -1182,8 +1183,13 @@ private static int getInt(String table, String row, String column) {
* @return The value.
*/
public static int getCachedInt(String table, String row, String column) {
String cacheKey = table + "-" + row + "-" + column;
return (int) cache.get(cacheKey, key -> getInt(table, row, column));
String cacheKey = "getCachedInt-" + table + "-" + row + "-" + column;
if (memo.containsKey(cacheKey)) {
return (int) memo.get(cacheKey);
}
int integer = getInt(table, row, column);
memo.put(cacheKey, integer);
return integer;
}

/**
Expand All @@ -1203,7 +1209,7 @@ private static double getDouble(String table, String row, String column) {
return dbl;
} catch (SQLException | NullPointerException ignored) {
}
invalidateCaches();
memo.clear();
return Double.parseDouble(getDefault(table, row, column));
}

Expand All @@ -1216,8 +1222,13 @@ private static double getDouble(String table, String row, String column) {
* @return The value.
*/
public static double getCachedDouble(String table, String row, String column) {
String cacheKey = table + "-" + row + "-" + column;
return (double) cache.get(cacheKey, key -> getDouble(table, row, column));
String cacheKey = "getCachedDouble-" + table + "-" + row + "-" + column;
if (memo.containsKey(cacheKey)) {
return (double) memo.get(cacheKey);
}
double real = getDouble(table, row, column);
memo.put(cacheKey, real);
return real;
}

/**
Expand All @@ -1238,7 +1249,7 @@ private static String getString(String table, String row, String column) {
return str;
} catch (SQLException | NullPointerException ignored) {
}
invalidateCaches();
memo.clear();
return getDefault(table, row, column);
}

Expand All @@ -1251,8 +1262,13 @@ private static String getString(String table, String row, String column) {
* @return The value.
*/
public static String getCachedString(String table, String row, String column) {
String cacheKey = table + "-" + row + "-" + column;
return (String) cache.get(cacheKey, key -> getString(table, row, column));
String cacheKey = "getCachedString-" + table + "-" + row + "-" + column;
if (memo.containsKey(cacheKey)) {
return (String) memo.get(cacheKey);
}
String string = getString(table, row, column);
memo.put(cacheKey, string);
return string;
}

/**
Expand Down Expand Up @@ -1297,14 +1313,20 @@ private static Ingredient getBreedingItems(String row) {
* @return The value.
*/
public static Ingredient getCachedBreedingItems(String row) {
return (Ingredient) cache.get(row, key -> getBreedingItems(row));
String cacheKey = "getBreedingItems-" + row;
if (memo.containsKey(cacheKey)) {
return (Ingredient) memo.get(cacheKey);
}
Ingredient ingredient = getBreedingItems(row);
memo.put(cacheKey, ingredient);
return ingredient;
}

/**
* Reset all tables in the database by deleting the sql script and resetting the in-memory db.
*/
public static void resetAll() {
invalidateCaches();
memo.clear();
if (!new File(PATH).exists()) return;
if (!new File(PATH).delete()) {
throw new RuntimeException("Could not delete file " + PATH);
Expand All @@ -1326,7 +1348,7 @@ public static void resetAll() {
* @param db The table to reset.
*/
public static void resetOne(String db) {
invalidateCaches();
memo.clear();
if (!new File(PATH).exists()) return;
try {
List<String> lines = FileUtils.readLines(new File(PATH), Charset.defaultCharset())
Expand All @@ -1347,7 +1369,7 @@ public static void resetOne(String db) {
* @param cols The columns to reset.
*/
public static void resetPartial(String db, ObjectSet<String> cols) {
invalidateCaches();
memo.clear();
if (!new File(PATH).exists()) return;
try {
List<String> lines = FileUtils.readLines(new File(PATH), Charset.defaultCharset())
Expand All @@ -1372,7 +1394,7 @@ public static void resetPartial(String db, ObjectSet<String> cols) {
* @param count The number of commands to undo.
*/
public static void undo(int count) {
invalidateCaches();
memo.clear();
if (!new File(PATH).exists()) return;
try {
List<String> lines = FileUtils.readLines(new File(PATH), Charset.defaultCharset());
Expand All @@ -1388,13 +1410,4 @@ public static void undo(int count) {
throw new RuntimeException(e);
}
}

/**
* Invalidate caches if they aren't null.
*/
private static void invalidateCaches() {
if (cache != null) {
cache.invalidateAll();
}
}
}

0 comments on commit 9f21277

Please sign in to comment.