Cache if display text contains individual placeholders or not

This commit is contained in:
filoghost 2021-09-15 11:14:30 +02:00
parent 0a80d91d5b
commit 33c2693172
3 changed files with 33 additions and 17 deletions

View File

@ -25,6 +25,8 @@ class DisplayText {
private boolean allowPlaceholders;
private @Nullable String globalText;
private boolean containsPlaceholderAPIPattern;
private @Nullable Boolean containsIndividualPlaceholders;
private long lastPlaceholderRegistryVersion;
DisplayText(PlaceholderTracker placeholderTracker) {
this.placeholderTracker = placeholderTracker;
@ -34,7 +36,15 @@ class DisplayText {
if (!allowPlaceholders || unreplacedText == null) {
return false;
}
return containsPlaceholderAPIPattern || placeholderTracker.containsIndividualPlaceholders(unreplacedText); // TODO cache boolean value based on registry
if (containsPlaceholderAPIPattern) {
return true;
}
long currentPlaceholderRegistryVersion = placeholderTracker.getRegistryVersion();
if (containsIndividualPlaceholders == null || lastPlaceholderRegistryVersion != currentPlaceholderRegistryVersion) {
containsIndividualPlaceholders = placeholderTracker.containsIndividualPlaceholders(unreplacedText);
lastPlaceholderRegistryVersion = currentPlaceholderRegistryVersion;
}
return containsIndividualPlaceholders;
}
void setUnreplacedText(@Nullable String text) {
@ -42,6 +52,7 @@ class DisplayText {
globalText = null;
containsPlaceholderAPIPattern = unreplacedText != null
&& unreplacedText.anyLiteralPartMatch(PlaceholderAPIHook::containsPlaceholderPattern);
containsIndividualPlaceholders = null;
}
@Nullable String getUnreplacedText() {

View File

@ -8,12 +8,12 @@ package me.filoghost.holographicdisplays.plugin.placeholder.registry;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Table;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholder;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholderFactory;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholder;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderFactory;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholder;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholderFactory;
import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderOccurrence;
@ -23,18 +23,20 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class PlaceholderRegistry {
private final Table<PlaceholderIdentifier, PluginName, PlaceholderExpansion> placeholderExpansions;
private Runnable changeListener;
private final AtomicLong version;
public PlaceholderRegistry() {
this.placeholderExpansions = HashBasedTable.create();
this.version = new AtomicLong();
}
public void setChangeListener(Runnable changeListener) {
this.changeListener = changeListener;
public long getVersion() {
return version.get();
}
public void registerIndividualPlaceholderReplacer(
@ -68,19 +70,19 @@ public class PlaceholderRegistry {
private void registerExpansion(PlaceholderExpansion expansion) {
placeholderExpansions.put(expansion.getIdentifier(), expansion.getPluginName(), expansion);
changeListener.run();
version.incrementAndGet();
}
public void unregisterAll(Plugin plugin) {
placeholderExpansions.column(new PluginName(plugin)).clear();
changeListener.run();
version.incrementAndGet();
}
public void unregister(Plugin plugin, String identifier) {
placeholderExpansions.remove(new PlaceholderIdentifier(identifier), new PluginName(plugin));
changeListener.run();
version.incrementAndGet();
}
public @Nullable PlaceholderExpansion find(PlaceholderOccurrence textOccurrence) {

View File

@ -29,22 +29,21 @@ public class PlaceholderTracker {
// the corresponding entry is removed from the map automatically.
private final WeakHashMap<PlaceholderOccurrence, TrackedPlaceholder> activePlaceholders;
private volatile boolean registryChanged;
private long lastRegistryVersion;
public PlaceholderTracker(PlaceholderRegistry registry, TickClock tickClock) {
this.registry = registry;
this.tickClock = tickClock;
this.exceptionHandler = new PlaceholderExceptionHandler(tickClock);
this.activePlaceholders = new WeakHashMap<>();
registry.setChangeListener(() -> registryChanged = true);
}
public void update() {
if (!registryChanged) {
long currentRegistryVersion = registry.getVersion();
if (lastRegistryVersion == currentRegistryVersion) {
return;
}
registryChanged = false;
lastRegistryVersion = currentRegistryVersion;
// Remove entries whose placeholder expansion sources are outdated
activePlaceholders.entrySet().removeIf(entry -> {
@ -109,11 +108,15 @@ public class PlaceholderTracker {
}
}
public boolean containsIndividualPlaceholders(@NotNull StringWithPlaceholders textWithPlaceholders) {
return textWithPlaceholders.anyPlaceholderMatch(occurrence -> {
public boolean containsIndividualPlaceholders(@NotNull StringWithPlaceholders stringWithPlaceholders) {
return stringWithPlaceholders.anyPlaceholderMatch(occurrence -> {
PlaceholderExpansion placeholderExpansion = registry.find(occurrence);
return placeholderExpansion != null && placeholderExpansion.isIndividual();
});
}
public long getRegistryVersion() {
return registry.getVersion();
}
}