From 242de3fe7322372c15f388da6353c2c72f733306 Mon Sep 17 00:00:00 2001 From: Daniel Ennis Date: Thu, 18 Mar 2021 10:36:43 -0400 Subject: [PATCH] Don't sort children on node addition to save on performance (#68) * Use a Tree Map for children to save on performance Everytime a child is added to the CommandNode, the children was sorted. This action is extremely heavy with large node trees such as Minecraft. From what I can see, sort order was not even needed by type since the dispatcher parse checks argument and literal nodes instead. Testing on /bossbar command seemed to have no impact to behavior. Credit to https://github.com/PaperMC/Paper/commit/dda9680be520bba8cc8b4a20072af42bd8420e7a Co-authored-by: virustotalop * Restore back to a LinkedHashMap since iteration order isn't important anyways. Co-authored-by: virustotalop --- src/main/java/com/mojang/brigadier/tree/CommandNode.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java index 15b584a..47f8e3d 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -22,12 +22,11 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; -import java.util.stream.Collectors; public abstract class CommandNode implements Comparable> { - private Map> children = new LinkedHashMap<>(); - private Map> literals = new LinkedHashMap<>(); - private Map> arguments = new LinkedHashMap<>(); + private final Map> children = new LinkedHashMap<>(); + private final Map> literals = new LinkedHashMap<>(); + private final Map> arguments = new LinkedHashMap<>(); private final Predicate requirement; private final CommandNode redirect; private final RedirectModifier modifier; @@ -88,8 +87,6 @@ public abstract class CommandNode implements Comparable> { arguments.put(node.getName(), (ArgumentCommandNode) node); } } - - children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } public void findAmbiguities(final AmbiguityConsumer consumer) {