From b8f2a19085cf6bbe65d7c5a95123cbd1bf802837 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 13 Jun 2021 04:44:37 -0400 Subject: [PATCH] Remove unnecessary literals map Literals have some special handling in Brigadier, but as it turns out, it's also possible to derive what is and isn't a literal from the existing children. This reduces the amount of memory used to store a full 1.16.x command tree by about 18%. --- .../java/com/mojang/brigadier/tree/CommandNode.java | 12 ++++++------ 1 file changed, 6 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 47f8e3d..2436ef1 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -25,13 +25,13 @@ import java.util.function.Predicate; public abstract class CommandNode implements Comparable> { 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; private final boolean forks; private Command command; + private boolean hasLiterals = false; protected CommandNode(final Command command, final Predicate requirement, final CommandNode redirect, final RedirectModifier modifier, final boolean forks) { this.command = command; @@ -82,7 +82,7 @@ public abstract class CommandNode implements Comparable> { } else { children.put(node.getName(), node); if (node instanceof LiteralCommandNode) { - literals.put(node.getName(), (LiteralCommandNode) node); + hasLiterals = true; } else if (node instanceof ArgumentCommandNode) { arguments.put(node.getName(), (ArgumentCommandNode) node); } @@ -151,16 +151,16 @@ public abstract class CommandNode implements Comparable> { protected abstract String getSortedKey(); public Collection> getRelevantNodes(final StringReader input) { - if (literals.size() > 0) { + if (hasLiterals) { final int cursor = input.getCursor(); while (input.canRead() && input.peek() != ' ') { input.skip(); } final String text = input.getString().substring(cursor, input.getCursor()); input.setCursor(cursor); - final LiteralCommandNode literal = literals.get(text); - if (literal != null) { - return Collections.singleton(literal); + final CommandNode node = children.get(text); + if (node instanceof LiteralCommandNode) { + return Collections.singleton(node); } else { return arguments.values(); }