Reduce the throughput on the contexts cache in ContextManager (#929)

This commit is contained in:
Luck
2018-05-12 01:34:34 +01:00
Unverified
parent 804c884d8b
commit a927ca659f
8 changed files with 245 additions and 85 deletions
@@ -0,0 +1,80 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.buffers;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
/**
* An expiring supplier extension.
*
* <p>The delegate supplier is only called on executions of {@link #get()} if the
* result isn't already calculated.</p>
*
* @param <T> the supplied type
*/
public abstract class ExpiringCache<T> implements Supplier<T> {
private final long durationNanos;
private volatile T value;
// when to expire. 0 means "not yet initialized".
private volatile long expirationNanos;
protected ExpiringCache(long duration, TimeUnit unit) {
this.durationNanos = unit.toNanos(duration);
}
@Nonnull
protected abstract T supply();
@Override
public T get() {
long nanos = this.expirationNanos;
long now = System.nanoTime();
if (nanos == 0 || now - nanos >= 0) {
synchronized (this) {
if (nanos == this.expirationNanos) { // recheck for lost race
// compute the value using the delegate
T t = supply();
this.value = t;
// reset expiration timer
nanos = now + this.durationNanos;
// In the very unlikely event that nanos is 0, set it to 1;
// no one will notice 1 ns of tardiness.
this.expirationNanos = (nanos == 0) ? 1 : nanos;
return t;
}
}
}
return this.value;
}
}
@@ -25,11 +25,8 @@
package me.lucko.luckperms.common.contexts;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.api.Contexts;
@@ -38,6 +35,7 @@ import me.lucko.luckperms.api.context.ContextCalculator;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.api.context.StaticContextCalculator;
import me.lucko.luckperms.common.buffers.ExpiringCache;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@@ -64,14 +62,14 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
private final List<ContextCalculator<? super T>> calculators = new CopyOnWriteArrayList<>();
private final List<StaticContextCalculator> staticCalculators = new CopyOnWriteArrayList<>();
// caches context lookups
private final LoadingCache<T, Contexts> lookupCache = Caffeine.newBuilder()
.expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick
.build(new Loader());
// caches the creation of cache instances. cache-ception.
// we want to encourage re-use of these instances, it's faster that way
private final LoadingCache<T, ContextsCache<T>> subjectCaches = Caffeine.newBuilder()
.weakKeys()
.build(key -> new ContextsCache<>(key, this));
// caches static context lookups
@SuppressWarnings("Guava")
private final Supplier<Contexts> staticLookupCache = Suppliers.memoizeWithExpiration(new StaticLoader(), 50L, TimeUnit.MILLISECONDS);
private final StaticLookupCache staticLookupCache = new StaticLookupCache();
protected AbstractContextManager(LuckPermsPlugin plugin, Class<T> subjectClass) {
this.plugin = plugin;
@@ -95,21 +93,20 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
@Override
public ImmutableContextSet getApplicableContext(T subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
// this is actually already immutable, but the Contexts method signature returns the interface.
// using the makeImmutable method is faster than casting
return getApplicableContexts(subject).getContexts().makeImmutable();
return getCacheFor(subject).getContextSet();
}
@Override
public Contexts getApplicableContexts(T subject) {
return getCacheFor(subject).getContexts();
}
@Override
public ContextsCache<T> getCacheFor(T subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
return this.lookupCache.get(subject);
return this.subjectCaches.get(subject);
}
@Override
@@ -181,52 +178,58 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
throw new NullPointerException("subject");
}
this.lookupCache.invalidate(subject);
this.subjectCaches.invalidate(subject);
}
private final class Loader implements CacheLoader<T, Contexts> {
@Override
public Contexts load(@Nonnull T subject) {
MutableContextSet accumulator = MutableContextSet.create();
Contexts calculate(T subject) {
MutableContextSet accumulator = MutableContextSet.create();
for (ContextCalculator<? super T> calculator : AbstractContextManager.this.calculators) {
try {
MutableContextSet ret = calculator.giveApplicableContext(subject, accumulator);
//noinspection ConstantConditions
if (ret == null) {
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
}
accumulator = ret;
} catch (Exception e) {
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating the context of subject " + subject);
e.printStackTrace();
for (ContextCalculator<? super T> calculator : AbstractContextManager.this.calculators) {
try {
MutableContextSet ret = calculator.giveApplicableContext(subject, accumulator);
//noinspection ConstantConditions
if (ret == null) {
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
}
accumulator = ret;
} catch (Exception e) {
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating the context of subject " + subject);
e.printStackTrace();
}
return formContexts(subject, accumulator.makeImmutable());
}
return formContexts(subject, accumulator.makeImmutable());
}
private final class StaticLoader implements Supplier<Contexts> {
@Override
public Contexts get() {
MutableContextSet accumulator = MutableContextSet.create();
private Contexts calculateStatic() {
MutableContextSet accumulator = MutableContextSet.create();
for (StaticContextCalculator calculator : AbstractContextManager.this.staticCalculators) {
try {
MutableContextSet ret = calculator.giveApplicableContext(accumulator);
//noinspection ConstantConditions
if (ret == null) {
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
}
accumulator = ret;
} catch (Exception e) {
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating static contexts");
e.printStackTrace();
for (StaticContextCalculator calculator : this.staticCalculators) {
try {
MutableContextSet ret = calculator.giveApplicableContext(accumulator);
//noinspection ConstantConditions
if (ret == null) {
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
}
accumulator = ret;
} catch (Exception e) {
this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating static contexts");
e.printStackTrace();
}
}
return formContexts(accumulator.makeImmutable());
return formContexts(accumulator.makeImmutable());
}
private final class StaticLookupCache extends ExpiringCache<Contexts> {
StaticLookupCache() {
super(50L, TimeUnit.MILLISECONDS);
}
@Nonnull
@Override
public Contexts supply() {
return calculateStatic();
}
}
@@ -65,6 +65,14 @@ public interface ContextManager<T> {
*/
Contexts getApplicableContexts(T subject);
/**
* Gets the cache instance for the given subject.
*
* @param subject the subject
* @return the cache
*/
ContextsCache<T> getCacheFor(T subject);
/**
* Gets the contexts from the static calculators in this manager.
*
@@ -0,0 +1,67 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.contexts;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.buffers.ExpiringCache;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
/**
* Extension of {@link AbstractContextManager} that implements an expiring lookup cache
* per player.
*
* @param <T> the player type
*/
public final class ContextsCache<T> extends ExpiringCache<Contexts> {
private final T subject;
private final AbstractContextManager<T> contextManager;
public ContextsCache(T subject, AbstractContextManager<T> contextManager) {
super(50L, TimeUnit.MILLISECONDS); // expire roughly every tick
this.subject = subject;
this.contextManager = contextManager;
}
@Nonnull
@Override
protected Contexts supply() {
return this.contextManager.calculate(this.subject);
}
public Contexts getContexts() {
return get();
}
public ImmutableContextSet getContextSet() {
// this is actually already immutable, but the Contexts method signature returns the interface.
// using the makeImmutable method is faster than casting
return get().getContexts().makeImmutable();
}
}