Skip to content

Commit

Permalink
Added caching to literal pattern compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 24, 2023
1 parent c1ed771 commit 899d5cc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -40,7 +41,7 @@ public PlayerPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final String identifier,
@NotNull final Function<@NotNull Player, @Nullable String> function) {
this.plugin = plugin;
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
this.function = function;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.eco.core.placeholder;

import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import com.willfp.eco.util.StringUtils;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -38,7 +39,7 @@ public final class PlayerStaticPlaceholder implements InjectablePlaceholder {
public PlayerStaticPlaceholder(@NotNull final String identifier,
@NotNull final Function<@NotNull Player, @Nullable String> function) {
this.identifier = "%" + identifier + "%";
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
this.function = function;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -39,7 +40,7 @@ public PlayerlessPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final String identifier,
@NotNull final Supplier<@Nullable String> function) {
this.plugin = plugin;
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
this.function = function;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.eco.core.placeholder;

import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import com.willfp.eco.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -37,7 +38,7 @@ public final class StaticPlaceholder implements InjectablePlaceholder {
public StaticPlaceholder(@NotNull final String identifier,
@NotNull final Supplier<@Nullable String> function) {
this.identifier = "%" + identifier + "%";
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
this.function = function;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.willfp.eco.core.placeholder.InjectablePlaceholder;
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -28,7 +29,7 @@ public abstract class SimpleInjectablePlaceholder implements InjectablePlacehold
*/
protected SimpleInjectablePlaceholder(@NotNull final String identifier) {
this.identifier = identifier;
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.placeholder.RegistrablePlaceholder;
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
import com.willfp.eco.util.PatternUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand Down Expand Up @@ -37,7 +38,7 @@ protected SimplePlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final String identifier) {
this.plugin = plugin;
this.identifier = identifier;
this.pattern = Pattern.compile(identifier, Pattern.LITERAL);
this.pattern = PatternUtils.compileLiteral(identifier);
}

@Override
Expand Down
35 changes: 35 additions & 0 deletions eco-api/src/main/java/com/willfp/eco/util/PatternUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.willfp.eco.util;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
* Utilities / API methods for patterns.
*/
public final class PatternUtils {
/**
* Cache of compiled literal patterns.
*/
private static final Cache<String, Pattern> LITERAL_PATTERN_CACHE = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();

/**
* Compile a literal pattern.
*
* @param pattern The pattern.
* @return The compiled pattern.
*/
@NotNull
public static Pattern compileLiteral(@NotNull final String pattern) {
return LITERAL_PATTERN_CACHE.get(pattern, (it) -> Pattern.compile(it, Pattern.LITERAL));
}

private PatternUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}

0 comments on commit 899d5cc

Please sign in to comment.