Skip to content

Commit

Permalink
Cache lowercase versions of inputs and literals to avoid repeated cal…
Browse files Browse the repository at this point in the history
…ls to toLowerCase().

Thank you to Spottedleaf.
  • Loading branch information
slicedlime committed Mar 23, 2021
1 parent 242de3f commit 60a94e5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/mojang/brigadier/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,13 @@ public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResult

final String fullInput = parse.getReader().getString();
final String truncatedInput = fullInput.substring(0, cursor);
final String truncatedInputLowerCase = truncatedInput.toLowerCase();
@SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
int i = 0;
for (final CommandNode<S> node : parent.getChildren()) {
CompletableFuture<Suggestions> future = Suggestions.empty();
try {
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, truncatedInputLowerCase, start));
} catch (final CommandSyntaxException ignored) {
}
futures[i++] = future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public Boolean parse(final StringReader reader) throws CommandSyntaxException {

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
if ("true".startsWith(builder.getRemaining().toLowerCase())) {
if ("true".startsWith(builder.getRemainingLowerCase())) {
builder.suggest("true");
}
if ("false".startsWith(builder.getRemaining().toLowerCase())) {
if ("false".startsWith(builder.getRemainingLowerCase())) {
builder.suggest("false");
}
return builder.buildFuture();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@

public class SuggestionsBuilder {
private final String input;
private final String inputLowerCase;
private final int start;
private final String remaining;
private final String remainingLowerCase;
private final List<Suggestion> result = new ArrayList<>();

public SuggestionsBuilder(final String input, final int start) {
public SuggestionsBuilder(final String input, final String inputLowerCase, final int start) {
this.input = input;
this.inputLowerCase = inputLowerCase;
this.start = start;
this.remaining = input.substring(start);
this.remainingLowerCase = inputLowerCase.substring(start);
}

public SuggestionsBuilder(final String input, final int start) {
this(input, input.toLowerCase(), start);
}

public String getInput() {
Expand All @@ -34,6 +42,10 @@ public String getRemaining() {
return remaining;
}

public String getRemainingLowerCase() {
return remainingLowerCase;
}

public Suggestions build() {
return Suggestions.create(input, result);
}
Expand Down Expand Up @@ -74,10 +86,10 @@ public SuggestionsBuilder add(final SuggestionsBuilder other) {
}

public SuggestionsBuilder createOffset(final int start) {
return new SuggestionsBuilder(input, start);
return new SuggestionsBuilder(input, inputLowerCase, start);
}

public SuggestionsBuilder restart() {
return new SuggestionsBuilder(input, start);
return createOffset(start);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal;
private final String literalLowerCase;

public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
super(command, requirement, redirect, modifier, forks);
this.literal = literal;
this.literalLowerCase = literal.toLowerCase();
}

public String getLiteral() {
Expand Down Expand Up @@ -66,7 +68,7 @@ private int parse(final StringReader reader) {

@Override
public CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
if (literal.toLowerCase().startsWith(builder.getRemaining().toLowerCase())) {
if (literalLowerCase.startsWith(builder.getRemainingLowerCase())) {
return builder.suggest(literal).buildFuture();
} else {
return Suggestions.empty();
Expand Down

0 comments on commit 60a94e5

Please sign in to comment.