From 21f5c2484744317d6998ca1739ad32f2f0c28418 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 20 Mar 2021 18:17:17 +0000 Subject: [PATCH] Optimize Bukkit/Nukkit ChildProcessor refresh (#2952) --- .../bukkit/calculator/ChildProcessor.java | 26 ++++++++++++------- .../nukkit/calculator/ChildProcessor.java | 26 ++++++++++++------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/ChildProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/ChildProcessor.java index 5e9e9b2f9..8ee84610b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/ChildProcessor.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/ChildProcessor.java @@ -33,8 +33,9 @@ import me.lucko.luckperms.common.calculator.result.TristateResult; import net.luckperms.api.util.Tristate; import java.util.Collections; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; /** * Permission Processor for Bukkits "child" permission system. @@ -43,6 +44,7 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class); private final LPBukkitPlugin plugin; + private final AtomicBoolean needsRefresh = new AtomicBoolean(false); private Map childPermissions = Collections.emptyMap(); public ChildProcessor(LPBukkitPlugin plugin) { @@ -51,23 +53,27 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi @Override public TristateResult hasPermission(String permission) { + if (this.needsRefresh.compareAndSet(true, false)) { + refresh(); + } return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED); } @Override public void refresh() { - Map builder = new ConcurrentHashMap<>(); - for (Map.Entry e : this.sourceMap.entrySet()) { - Map children = this.plugin.getPermissionMap().getChildPermissions(e.getKey(), e.getValue()); - for (Map.Entry child : children.entrySet()) { - builder.put(child.getKey(), RESULT_FACTORY.result(Tristate.of(child.getValue()), "parent: " + e.getKey())); - } - } - this.childPermissions = builder; + Map childPermissions = new HashMap<>(); + this.sourceMap.forEach((key, value) -> { + Map children = this.plugin.getPermissionMap().getChildPermissions(key, value); + children.forEach((childKey, childValue) -> { + childPermissions.put(childKey, RESULT_FACTORY.result(Tristate.of(childValue), "parent: " + key)); + }); + }); + this.childPermissions = childPermissions; + this.needsRefresh.set(false); } @Override public void invalidate() { - refresh(); + this.needsRefresh.set(true); } } diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/ChildProcessor.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/ChildProcessor.java index 2747645d6..75a2f63d0 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/ChildProcessor.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/ChildProcessor.java @@ -33,8 +33,9 @@ import me.lucko.luckperms.nukkit.LPNukkitPlugin; import net.luckperms.api.util.Tristate; import java.util.Collections; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; /** * Permission Processor for Nukkits "child" permission system. @@ -43,6 +44,7 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(ChildProcessor.class); private final LPNukkitPlugin plugin; + private final AtomicBoolean needsRefresh = new AtomicBoolean(false); private Map childPermissions = Collections.emptyMap(); public ChildProcessor(LPNukkitPlugin plugin) { @@ -51,23 +53,27 @@ public class ChildProcessor extends AbstractPermissionProcessor implements Permi @Override public TristateResult hasPermission(String permission) { + if (this.needsRefresh.compareAndSet(true, false)) { + refresh(); + } return this.childPermissions.getOrDefault(permission, TristateResult.UNDEFINED); } @Override public void refresh() { - Map builder = new ConcurrentHashMap<>(); - for (Map.Entry e : this.sourceMap.entrySet()) { - Map children = this.plugin.getPermissionMap().getChildPermissions(e.getKey(), e.getValue()); - for (Map.Entry child : children.entrySet()) { - builder.put(child.getKey(), RESULT_FACTORY.result(Tristate.of(child.getValue()), "parent: " + e.getKey())); - } - } - this.childPermissions = builder; + Map childPermissions = new HashMap<>(); + this.sourceMap.forEach((key, value) -> { + Map children = this.plugin.getPermissionMap().getChildPermissions(key, value); + children.forEach((childKey, childValue) -> { + childPermissions.put(childKey, RESULT_FACTORY.result(Tristate.of(childValue), "parent: " + key)); + }); + }); + this.childPermissions = childPermissions; + this.needsRefresh.set(false); } @Override public void invalidate() { - refresh(); + this.needsRefresh.set(true); } }