Skip to content

Commit

Permalink
Fix performance bottlenecking introduced by broken optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jan 24, 2025
1 parent 021902e commit 118542c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions server/src/main/java/com/soulfiremc/server/data/GsonDataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
*/
package com.soulfiremc.server.data;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.soulfiremc.server.util.SFHelpers;
import com.soulfiremc.server.util.structs.GsonInstance;
import net.kyori.adventure.key.Key;

import java.io.IOException;
import java.util.HashMap;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

public class GsonDataHelper {
Expand All @@ -46,7 +50,15 @@ public Key read(JsonReader in) throws IOException {
return Key.key(key);
}
};
private static final Map<String, JsonArray> LOADED_DATA = new HashMap<>();
private static final LoadingCache<String, JsonArray> LOADED_DATA = Caffeine.newBuilder()
.expireAfterAccess(Duration.ofSeconds(1))
.build(file -> {
try {
return GsonInstance.GSON.fromJson(SFHelpers.getResourceAsString(file), JsonArray.class);
} catch (Exception e) {
throw new RuntimeException("Failed to load data file " + file, e);
}
});
private static final Function<Map<Class<?>, Object>, Gson> GSON_FACTORY = (typeAdapters) -> {
var builder = new GsonBuilder()
.registerTypeAdapter(Key.class, RESOURCE_KEY_ADAPTER)
Expand All @@ -66,19 +78,7 @@ public static <T> T fromJson(String dataFile, String dataKey, Class<T> clazz) {
public static <T> T fromJson(String dataFile, String dataKey, Class<T> clazz,
Map<Class<?>, Object> typeAdapters) {
var gson = createGson(typeAdapters);
var array =
LOADED_DATA.computeIfAbsent(
dataFile,
file -> {
var data = new JsonArray();
try {
data =
gson.fromJson(SFHelpers.getResourceAsString(file), JsonArray.class);
} catch (Exception e) {
throw new RuntimeException("Failed to load data file " + file, e);
}
return data;
});
var array = Objects.requireNonNull(LOADED_DATA.get(dataFile));
for (var element : array) {
if (element.getAsJsonObject().get("key").getAsString().equals(dataKey)) {
return gson.fromJson(element, clazz);
Expand Down

0 comments on commit 118542c

Please sign in to comment.