-
Notifications
You must be signed in to change notification settings - Fork 396
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
Optimize parsing performance #135
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,13 +354,15 @@ private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader | |
Map<CommandNode<S>, CommandSyntaxException> errors = null; | ||
List<ParseResults<S>> potentials = null; | ||
final int cursor = originalReader.getCursor(); | ||
final Collection<? extends CommandNode<S>> relevantNodes = node.getRelevantNodes(originalReader); | ||
final boolean unique = relevantNodes.size() == 1; | ||
|
||
for (final CommandNode<S> child : node.getRelevantNodes(originalReader)) { | ||
for (final CommandNode<S> child : relevantNodes) { | ||
if (!child.canUse(source)) { | ||
continue; | ||
} | ||
final CommandContextBuilder<S> context = contextSoFar.copy(); | ||
final StringReader reader = new StringReader(originalReader); | ||
final CommandContextBuilder<S> context = unique ? contextSoFar : contextSoFar.copy(); | ||
final StringReader reader = unique ? originalReader : new StringReader(originalReader); | ||
try { | ||
try { | ||
child.parse(reader, context); | ||
|
@@ -391,16 +393,23 @@ private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader | |
return new ParseResults<>(context, parse.getReader(), parse.getExceptions()); | ||
} else { | ||
final ParseResults<S> parse = parseNodes(child, reader, context); | ||
if (unique) { | ||
return parse; | ||
} | ||
Comment on lines
+403
to
+405
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
if (potentials == null) { | ||
potentials = new ArrayList<>(1); | ||
potentials = new ArrayList<>(relevantNodes.size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
} | ||
potentials.add(parse); | ||
} | ||
} else { | ||
final ParseResults<S> parse = new ParseResults<>(context, reader, Collections.emptyMap()); | ||
if (unique) { | ||
return parse; | ||
} | ||
if (potentials == null) { | ||
potentials = new ArrayList<>(1); | ||
potentials = new ArrayList<>(relevantNodes.size()); | ||
} | ||
potentials.add(new ParseResults<>(context, reader, Collections.emptyMap())); | ||
potentials.add(parse); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is less readable. But if it increase performance it's ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
unique
, we do not need to take snapshots of the parsing state (contextSoFar
andoriginalReader
) here.