Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary literals map #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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%.
  • Loading branch information
astei committed Jun 13, 2021
commit b8f2a19085cf6bbe65d7c5a95123cbd1bf802837
12 changes: 6 additions & 6 deletions src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
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;
private final boolean forks;
private Command<S> command;
private boolean hasLiterals = false;

protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
this.command = command;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void addChild(final CommandNode<S> node) {
} else {
children.put(node.getName(), node);
if (node instanceof LiteralCommandNode) {
literals.put(node.getName(), (LiteralCommandNode<S>) node);
hasLiterals = true;
} else if (node instanceof ArgumentCommandNode) {
arguments.put(node.getName(), (ArgumentCommandNode<S, ?>) node);
}
Expand Down Expand Up @@ -151,16 +151,16 @@ public Predicate<S> getRequirement() {
protected abstract String getSortedKey();

public Collection<? extends CommandNode<S>> 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<S> literal = literals.get(text);
if (literal != null) {
return Collections.singleton(literal);
final CommandNode<S> node = children.get(text);
if (node instanceof LiteralCommandNode<?>) {
return Collections.singleton(node);
} else {
return arguments.values();
}
Expand Down