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; package me.lucko.luckperms.bukkit;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.bukkit.calculators.AttachmentProcessor; 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.calculators.*;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
@AllArgsConstructor @AllArgsConstructor
@ -41,23 +39,23 @@ public class BukkitCalculatorFactory implements CalculatorFactory {
private final LPBukkitPlugin plugin; private final LPBukkitPlugin plugin;
@Override @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()); UUID uuid = plugin.getUuidCache().getExternalUUID(user.getUuid());
List<PermissionProcessor> processors = new ArrayList<>(5); ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor(map)); processors.add(new MapProcessor());
processors.add(new AttachmentProcessor(() -> { processors.add(new AttachmentProcessor(() -> {
LPPermissible permissible = Injector.getPermissible(uuid); LPPermissible permissible = Injector.getPermissible(uuid);
return permissible == null ? null : permissible.getAttachmentPermissions(); return permissible == null ? null : permissible.getAttachmentPermissions();
})); }));
if (plugin.getConfiguration().isApplyingWildcards()) { if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new WildcardProcessor(map)); processors.add(new WildcardProcessor());
} }
if (plugin.getConfiguration().isApplyingRegex()) { if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new RegexProcessor(map)); processors.add(new RegexProcessor());
} }
processors.add(new DefaultsProcessor(contexts.isOp(), plugin.getDefaultsProvider())); 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()); 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.Bukkit;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
public class DefaultsProcessor implements PermissionProcessor { public class DefaultsProcessor implements PermissionProcessor {
private final boolean isOp; private final boolean isOp;
@ -43,4 +45,9 @@ public class DefaultsProcessor implements PermissionProcessor {
Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission); Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission);
return defPerm == null ? Tristate.UNDEFINED : Tristate.fromBoolean(defPerm.getDefault().getValue(isOp)); 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; package me.lucko.luckperms.bungee;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.calculators.*; import me.lucko.luckperms.common.calculators.*;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
public class BungeeCalculatorFactory implements CalculatorFactory { public class BungeeCalculatorFactory implements CalculatorFactory {
private final LPBungeePlugin plugin; private final LPBungeePlugin plugin;
@Override @Override
public PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map) { public PermissionCalculator build(Contexts contexts, User user) {
List<PermissionProcessor> processors = new ArrayList<>(3); ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor(map)); processors.add(new MapProcessor());
if (plugin.getConfiguration().isApplyingWildcards()) { if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new WildcardProcessor(map)); processors.add(new WildcardProcessor());
} }
if (plugin.getConfiguration().isApplyingRegex()) { 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) { public PermissionCache(Contexts contexts, User user, CalculatorFactory calculatorFactory) {
permissions = new ConcurrentHashMap<>(); permissions = new ConcurrentHashMap<>();
calculator = calculatorFactory.build(contexts, user, permissions); calculator = calculatorFactory.build(contexts, user);
calculator.updateBacking(permissions); // Initial setup.
} }
@Override @Override
@ -64,6 +65,7 @@ public class PermissionCache implements PermissionData {
public void setPermissions(Map<String, Boolean> permissions) { public void setPermissions(Map<String, Boolean> permissions) {
this.permissions.clear(); this.permissions.clear();
this.permissions.putAll(permissions); this.permissions.putAll(permissions);
calculator.updateBacking(this.permissions);
invalidateCache(); invalidateCache();
} }

View File

@ -25,13 +25,11 @@ package me.lucko.luckperms.common.calculators;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import java.util.Map;
/** /**
* Creates a calculator instance given a set of contexts * Creates a calculator instance given a set of contexts
*/ */
public interface CalculatorFactory { 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; package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import java.util.Map; import java.util.Map;
@AllArgsConstructor
public class MapProcessor implements PermissionProcessor { public class MapProcessor implements PermissionProcessor {
private Map<String, Boolean> map = null;
@Getter
private final Map<String, Boolean> map;
@Override @Override
public Tristate hasPermission(String permission) { public Tristate hasPermission(String permission) {
Boolean b = map.get(permission); Boolean b = map.get(permission);
return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b); 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 me.lucko.luckperms.common.LuckPermsPlugin;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Calculates and caches permissions * Calculates and caches permissions
@ -76,4 +77,10 @@ public class PermissionCalculator {
return Tristate.UNDEFINED; 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 me.lucko.luckperms.api.Tristate;
import java.util.Map;
public interface PermissionProcessor { public interface PermissionProcessor {
Tristate hasPermission(String permission); Tristate hasPermission(String permission);
void updateBacking(Map<String, Boolean> map);
} }

View File

@ -22,28 +22,20 @@
package me.lucko.luckperms.common.calculators; package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.constants.Patterns;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@AllArgsConstructor
public class RegexProcessor implements PermissionProcessor { public class RegexProcessor implements PermissionProcessor {
private Map<String, Boolean> regexPermissions = new ConcurrentHashMap<>();
@Getter
private final Map<String, Boolean> map;
@Override @Override
public Tristate hasPermission(String permission) { public Tristate hasPermission(String permission) {
for (Map.Entry<String, Boolean> e : map.entrySet()) { for (Map.Entry<String, Boolean> e : regexPermissions.entrySet()) {
if (!e.getKey().startsWith("r=") && !e.getKey().startsWith("R=")) { Pattern p = Patterns.compile(e.getKey());
continue;
}
Pattern p = Patterns.compile(e.getKey().substring(2));
if (p == null) { if (p == null) {
continue; continue;
} }
@ -55,4 +47,16 @@ public class RegexProcessor implements PermissionProcessor {
return Tristate.UNDEFINED; 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; package me.lucko.luckperms.common.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import java.util.Map; import java.util.Map;
@AllArgsConstructor
public class WildcardProcessor implements PermissionProcessor { public class WildcardProcessor implements PermissionProcessor {
private Map<String, Boolean> map = null;
@Getter
private final Map<String, Boolean> map;
@Override @Override
public Tristate hasPermission(String permission) { public Tristate hasPermission(String permission) {
@ -65,4 +60,11 @@ public class WildcardProcessor implements PermissionProcessor {
return Tristate.UNDEFINED; 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; package me.lucko.luckperms.sponge;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.common.calculators.*; 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.calculators.SpongeWildcardProcessor;
import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.LuckPermsService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
public class SpongeCalculatorFactory implements CalculatorFactory { public class SpongeCalculatorFactory implements CalculatorFactory {
private final LPSpongePlugin plugin; private final LPSpongePlugin plugin;
@Override @Override
public PermissionCalculator build(Contexts contexts, User user, Map<String, Boolean> map) { public PermissionCalculator build(Contexts contexts, User user) {
List<PermissionProcessor> processors = new ArrayList<>(5); ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor(map)); processors.add(new MapProcessor());
if (plugin.getConfiguration().isApplyingWildcards()) { if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new SpongeWildcardProcessor(map)); processors.add(new SpongeWildcardProcessor());
processors.add(new WildcardProcessor(map)); processors.add(new WildcardProcessor());
} }
if (plugin.getConfiguration().isApplyingRegex()) { if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new RegexProcessor(map)); processors.add(new RegexProcessor());
} }
processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContexts()))); 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.service.context.Context;
import org.spongepowered.api.util.Tristate; import org.spongepowered.api.util.Tristate;
import java.util.Map;
import java.util.Set; import java.util.Set;
import static me.lucko.luckperms.sponge.service.LuckPermsService.convertTristate; 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; 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; package me.lucko.luckperms.sponge.calculators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.calculators.PermissionProcessor; import me.lucko.luckperms.common.calculators.PermissionProcessor;
import java.util.Map; import java.util.Map;
@AllArgsConstructor
public class SpongeWildcardProcessor implements PermissionProcessor { public class SpongeWildcardProcessor implements PermissionProcessor {
private Map<String, Boolean> map = null;
@Getter
private final Map<String, Boolean> map;
@Override @Override
public Tristate hasPermission(String permission) { public Tristate hasPermission(String permission) {
@ -56,4 +51,11 @@ public class SpongeWildcardProcessor implements PermissionProcessor {
return Tristate.UNDEFINED; return Tristate.UNDEFINED;
} }
@Override
public void updateBacking(Map<String, Boolean> map) {
if (this.map == null) {
this.map = map;
}
}
} }