Added guava caches to performance-sensitive components

This commit is contained in:
Auxilor
2022-02-01 10:44:19 +00:00
parent 885a7835ea
commit 70ef99e875
9 changed files with 97 additions and 32 deletions

View File

@@ -6,6 +6,9 @@ group 'com.willfp'
version rootProject.version
dependencies {
// API Guava
api 'com.google.guava:guava:31.0.1-jre'
// Adventure
compileOnly 'net.kyori:adventure-platform-bukkit:4.0.0'
compileOnly 'net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT'
@@ -15,7 +18,6 @@ dependencies {
compileOnly 'org.apache.maven:maven-artifact:3.8.1'
compileOnly 'com.comphenix.protocol:ProtocolLib:4.6.1-SNAPSHOT'
compileOnly 'com.google.code.gson:gson:2.8.8'
compileOnly 'org.apache.commons:commons-lang3:3.0'
}
java {

View File

@@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
/**
@@ -141,4 +142,21 @@ public class PlaceholderEntry {
public void register() {
PlaceholderManager.registerPlaceholder(this);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof PlaceholderEntry entry)) {
return false;
}
return Objects.equals(this.getIdentifier(), entry.getIdentifier())
&& Objects.equals(this.getPlugin(), entry.getPlugin());
}
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier(), this.getPlugin());
}
}

View File

@@ -1,5 +1,8 @@
package com.willfp.eco.core.integrations.placeholder;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.EcoPlugin;
import org.bukkit.entity.Player;
@@ -12,6 +15,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Class to handle placeholder integrations.
@@ -27,6 +31,21 @@ public final class PlaceholderManager {
*/
private static final Set<PlaceholderIntegration> REGISTERED_INTEGRATIONS = new HashSet<>();
/**
* Placeholder Cache.
*/
private static final LoadingCache<EntryWithPlayer, String> PLACEHOLDER_CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
.build(
new CacheLoader<>() {
@Override
@NotNull
public String load(@NotNull final EntryWithPlayer key) {
return key.entry.getResult(key.player);
}
}
);
/**
* Register a new placeholder integration.
*
@@ -94,7 +113,7 @@ public final class PlaceholderManager {
return "";
}
return entry.getResult(player);
return PLACEHOLDER_CACHE.getUnchecked(new EntryWithPlayer(entry, player));
}
/**
@@ -128,6 +147,11 @@ public final class PlaceholderManager {
return found;
}
private static record EntryWithPlayer(@NotNull PlaceholderEntry entry,
@Nullable Player player) {
}
private PlaceholderManager() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -1,5 +1,8 @@
package com.willfp.eco.util;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonSyntaxException;
@@ -20,6 +23,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -66,6 +70,45 @@ public final class StringUtils {
.emitLegacyHoverEvent()
.build();
/**
* Json -> Legacy Cache.
*/
private static final LoadingCache<String, String> JSON_TO_LEGACY = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.SECONDS)
.build(
new CacheLoader<>() {
@Override
@NotNull
public String load(@NotNull final String json) {
try {
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
} catch (JsonSyntaxException e) {
return json;
}
}
}
);
/**
* Legacy -> Json Cache.
*/
private static final LoadingCache<String, String> LEGACY_TO_JSON = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.SECONDS)
.build(
new CacheLoader<>() {
@Override
@NotNull
public String load(@NotNull final String legacy) {
return GSON_COMPONENT_SERIALIZER.serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
LEGACY_COMPONENT_SERIALIZER.deserialize(legacy)
)
);
}
}
);
/**
* Color map.
*/
@@ -423,11 +466,8 @@ public final class StringUtils {
if (legacy == null) {
processed = "";
}
return GSON_COMPONENT_SERIALIZER.serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
LEGACY_COMPONENT_SERIALIZER.deserialize(processed)
)
);
return LEGACY_TO_JSON.getUnchecked(processed);
}
/**
@@ -442,12 +482,7 @@ public final class StringUtils {
return "";
}
try {
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
} catch (JsonSyntaxException e) {
return json;
}
return JSON_TO_LEGACY.getUnchecked(json);
}
/**

View File

@@ -7,6 +7,5 @@ dependencies {
compileOnly 'org.reflections:reflections:0.9.12'
compileOnly 'net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT'
compileOnly 'net.kyori:adventure-platform-bukkit:4.0.0'
compileOnly 'com.google.guava:guava:31.0.1-jre'
compileOnly 'org.objenesis:objenesis:3.2'
}

View File

@@ -40,7 +40,6 @@ dependencies {
compileOnly 'com.bgsoftware:SuperiorSkyblockAPI:1.8.3'
compileOnly 'com.github.MilkBowl:VaultAPI:1.7'
compileOnly 'world.bentobox:bentobox:1.17.3-SNAPSHOT'
compileOnly 'com.google.guava:guava:31.0.1-jre'
compileOnly 'com.iridium:IridiumSkyblock:3.1.2'
compileOnly 'com.github.WhipDevelopment:CrashClaim:f9cd7d92eb'
compileOnly 'com.wolfyscript.wolfyutilities:wolfyutilities:3.16.0.0'

View File

@@ -1,6 +0,0 @@
package com.willfp.eco.internal.spigot.data
import com.willfp.eco.core.data.keys.PersistentDataKey
import com.willfp.eco.internal.spigot.data.storage.DataHandler
import java.util.UUID

View File

@@ -23,22 +23,17 @@ class EcoProfileHandler(
val data = mutableMapOf<PersistentDataKey<*>, Any>()
return if (uuid == serverProfileUUID) {
val profile = EcoServerProfile(data, handler)
loaded[uuid] = profile
profile
} else {
val profile = EcoPlayerProfile(data, uuid, handler)
loaded[uuid] = profile
profile
}
val profile = if (uuid == serverProfileUUID)
EcoServerProfile(data, handler) else EcoPlayerProfile(data, uuid, handler)
loaded[uuid] = profile
return profile
}
override fun load(uuid: UUID): PlayerProfile {
return loadGenericProfile(uuid) as PlayerProfile
}
override fun loadServerProfile(): ServerProfile {
return loadGenericProfile(serverProfileUUID) as ServerProfile
}

View File

@@ -57,5 +57,4 @@ libraries:
- 'mysql:mysql-connector-java:8.0.25'
- 'com.google.guava:guava:31.0.1-jre'
- 'com.zaxxer:HikariCP:5.0.0'
- 'org.apache.commons:commons-lang3:3.0'
- 'org.objenesis:objenesis:3.2'