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 dda9680be5

Co-authored-by: virustotalop <virustotalop@gmail.com>

* Restore back to a LinkedHashMap

since iteration order isn't important anyways.

Co-authored-by: virustotalop <virustotalop@gmail.com>
This commit is contained in:
Daniel Ennis 2021-03-18 10:36:43 -04:00 committed by GitHub
parent 55f6e25c03
commit 242de3fe73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<S> implements Comparable<CommandNode<S>> {
private Map<String, CommandNode<S>> children = new LinkedHashMap<>();
private Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
private Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
private final Map<String, CommandNode<S>> children = new LinkedHashMap<>();
private final Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
private final Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
private final Predicate<S> requirement;
private final CommandNode<S> redirect;
private final RedirectModifier<S> modifier;
@ -88,8 +87,6 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
arguments.put(node.getName(), (ArgumentCommandNode<S, ?>) 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<S> consumer) {