Improve permission lookup speed (slightly)

This commit is contained in:
Luck 2016-10-26 21:35:44 +01:00
parent 1c15f8462f
commit 0198068a13
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
14 changed files with 94 additions and 64 deletions

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.bukkit;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.bukkit.calculators.AttachmentProcessor;
@ -31,9 +32,6 @@ import me.lucko.luckperms.bukkit.inject.LPPermissible;
import me.lucko.luckperms.common.calculators.*;
import me.lucko.luckperms.common.users.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@AllArgsConstructor
@ -41,23 +39,23 @@ public class BukkitCalculatorFactory implements CalculatorFactory {
private final LPBukkitPlugin plugin;
@Override
public PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map) {
public PermissionCalculator build(Contexts contexts, User user) {
UUID uuid = plugin.getUuidCache().getExternalUUID(user.getUuid());
List<PermissionProcessor> processors = new ArrayList<>(5);
processors.add(new MapProcessor(map));
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor());
processors.add(new AttachmentProcessor(() -> {
LPPermissible permissible = Injector.getPermissible(uuid);
return permissible == null ? null : permissible.getAttachmentPermissions();
}));
if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new WildcardProcessor(map));
processors.add(new WildcardProcessor());
}
if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new RegexProcessor(map));
processors.add(new RegexProcessor());
}
processors.add(new DefaultsProcessor(contexts.isOp(), plugin.getDefaultsProvider()));
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors);
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build());
}
}

View File

@ -48,4 +48,9 @@ public class AttachmentProcessor implements PermissionProcessor {
return pai == null ? Tristate.UNDEFINED : Tristate.fromBoolean(pai.getValue());
}
@Override
public void updateBacking(Map<String, Boolean> map) {
// Do nothing, this doesn't use the backing
}
}

View File

@ -28,6 +28,8 @@ import me.lucko.luckperms.common.calculators.PermissionProcessor;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;
import java.util.Map;
@AllArgsConstructor
public class DefaultsProcessor implements PermissionProcessor {
private final boolean isOp;
@ -43,4 +45,9 @@ public class DefaultsProcessor implements PermissionProcessor {
Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission);
return defPerm == null ? Tristate.UNDEFINED : Tristate.fromBoolean(defPerm.getDefault().getValue(isOp));
}
@Override
public void updateBacking(Map<String, Boolean> map) {
// Do nothing, this doesn't use the backing
}
}

View File

@ -22,30 +22,27 @@
package me.lucko.luckperms.bungee;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.calculators.*;
import me.lucko.luckperms.common.users.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@AllArgsConstructor
public class BungeeCalculatorFactory implements CalculatorFactory {
private final LPBungeePlugin plugin;
@Override
public PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map) {
List<PermissionProcessor> processors = new ArrayList<>(3);
processors.add(new MapProcessor(map));
public PermissionCalculator build(Contexts contexts, User user) {
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor());
if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new WildcardProcessor(map));
processors.add(new WildcardProcessor());
}
if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new RegexProcessor(map));
processors.add(new RegexProcessor());
}
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors);
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build());
}
}

View File

@ -53,7 +53,8 @@ public class PermissionCache implements PermissionData {
public PermissionCache(Contexts contexts, User user, CalculatorFactory calculatorFactory) {
permissions = new ConcurrentHashMap<>();
calculator = calculatorFactory.build(contexts, user, permissions);
calculator = calculatorFactory.build(contexts, user);
calculator.updateBacking(permissions); // Initial setup.
}
@Override
@ -64,6 +65,7 @@ public class PermissionCache implements PermissionData {
public void setPermissions(Map<String, Boolean> permissions) {
this.permissions.clear();
this.permissions.putAll(permissions);
calculator.updateBacking(this.permissions);
invalidateCache();
}

View File

@ -25,13 +25,11 @@ package me.lucko.luckperms.common.calculators;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.users.User;
import java.util.Map;
/**
* Creates a calculator instance given a set of contexts
*/
public interface CalculatorFactory {
PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map);
PermissionCalculator build(Contexts contexts, User user);
}

View File

@ -22,21 +22,23 @@
package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate;
import java.util.Map;
@AllArgsConstructor
public class MapProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
private Map<String, Boolean> map = null;
@Override
public Tristate hasPermission(String permission) {
Boolean b = map.get(permission);
return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b);
}
@Override
public void updateBacking(Map<String, Boolean> map) {
if (this.map == null) {
this.map = map;
}
}
}

View File

@ -30,6 +30,7 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.LuckPermsPlugin;
import java.util.List;
import java.util.Map;
/**
* Calculates and caches permissions
@ -76,4 +77,10 @@ public class PermissionCalculator {
return Tristate.UNDEFINED;
}
public synchronized void updateBacking(Map<String, Boolean> map) {
for (PermissionProcessor processor : processors) {
processor.updateBacking(map);
}
}
}

View File

@ -24,8 +24,11 @@ package me.lucko.luckperms.common.calculators;
import me.lucko.luckperms.api.Tristate;
import java.util.Map;
public interface PermissionProcessor {
Tristate hasPermission(String permission);
void updateBacking(Map<String, Boolean> map);
}

View File

@ -22,28 +22,20 @@
package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.constants.Patterns;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
@AllArgsConstructor
public class RegexProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
private Map<String, Boolean> regexPermissions = new ConcurrentHashMap<>();
@Override
public Tristate hasPermission(String permission) {
for (Map.Entry<String, Boolean> e : map.entrySet()) {
if (!e.getKey().startsWith("r=") && !e.getKey().startsWith("R=")) {
continue;
}
Pattern p = Patterns.compile(e.getKey().substring(2));
for (Map.Entry<String, Boolean> e : regexPermissions.entrySet()) {
Pattern p = Patterns.compile(e.getKey());
if (p == null) {
continue;
}
@ -55,4 +47,16 @@ public class RegexProcessor implements PermissionProcessor {
return Tristate.UNDEFINED;
}
@Override
public void updateBacking(Map<String, Boolean> map) {
regexPermissions.clear();
for (Map.Entry<String, Boolean> e : map.entrySet()) {
if (!e.getKey().startsWith("r=") && !e.getKey().startsWith("R=")) {
continue;
}
regexPermissions.put(e.getKey().substring(2), e.getValue());
}
}
}

View File

@ -22,17 +22,12 @@
package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate;
import java.util.Map;
@AllArgsConstructor
public class WildcardProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
private Map<String, Boolean> map = null;
@Override
public Tristate hasPermission(String permission) {
@ -65,4 +60,11 @@ public class WildcardProcessor implements PermissionProcessor {
return Tristate.UNDEFINED;
}
@Override
public void updateBacking(Map<String, Boolean> map) {
if (this.map == null) {
this.map = map;
}
}
}

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.sponge;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.calculators.*;
@ -30,27 +31,23 @@ import me.lucko.luckperms.sponge.calculators.DefaultsProcessor;
import me.lucko.luckperms.sponge.calculators.SpongeWildcardProcessor;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@AllArgsConstructor
public class SpongeCalculatorFactory implements CalculatorFactory {
private final LPSpongePlugin plugin;
@Override
public PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map) {
List<PermissionProcessor> processors = new ArrayList<>(5);
processors.add(new MapProcessor(map));
public PermissionCalculator build(Contexts contexts, User user) {
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor());
if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new SpongeWildcardProcessor(map));
processors.add(new WildcardProcessor(map));
processors.add(new SpongeWildcardProcessor());
processors.add(new WildcardProcessor());
}
if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new RegexProcessor(map));
processors.add(new RegexProcessor());
}
processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContexts())));
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors);
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build());
}
}

View File

@ -28,6 +28,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsService;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.util.Tristate;
import java.util.Map;
import java.util.Set;
import static me.lucko.luckperms.sponge.service.LuckPermsService.convertTristate;
@ -51,4 +52,9 @@ public class DefaultsProcessor implements PermissionProcessor {
return me.lucko.luckperms.api.Tristate.UNDEFINED;
}
@Override
public void updateBacking(Map<String, Boolean> map) {
// Do nothing, this doesn't use the backing
}
}

View File

@ -22,18 +22,13 @@
package me.lucko.luckperms.sponge.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.calculators.PermissionProcessor;
import java.util.Map;
@AllArgsConstructor
public class SpongeWildcardProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
private Map<String, Boolean> map = null;
@Override
public Tristate hasPermission(String permission) {
@ -56,4 +51,11 @@ public class SpongeWildcardProcessor implements PermissionProcessor {
return Tristate.UNDEFINED;
}
@Override
public void updateBacking(Map<String, Boolean> map) {
if (this.map == null) {
this.map = map;
}
}
}