-
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?
Conversation
This optimization avoids unnecessary object allocations by not taking snapshots of the parsing state when the relevant nodes are unique.
According to JMH's JMHSamples_08_DeadCode, the correct way of benchmarking is either returning the result or consuming it with Blackholes.
final CommandContextBuilder<S> context = unique ? contextSoFar : contextSoFar.copy(); | ||
final StringReader reader = unique ? originalReader : new StringReader(originalReader); |
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
and originalReader
) here.
if (unique) { | ||
return parse; | ||
} |
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
, the potentials
will always contain only parse
and this parse
will be returned after exiting this for
loop, so we can return parse
here without allocating potentials
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Since potentials
will contain ParseResults<S>
up to the amount of relevantNodes.size()
, allocate that capacity in advance to ensure resizing never occurs.
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.
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.
I think this is less readable. But if it increase performance it's ok.
Overview
This optimization avoids unnecessary object allocations by not taking snapshots of the parsing state when the size of the relevant nodes is
1
. Parsing time becomes about 48.6 to 57.1% of the original.Benchmark Results
Before Optimization f20bede
ParsingBenchmarks.parse_
ParsingBenchmarks.parse_a1i
ParsingBenchmarks.parse_c
ParsingBenchmarks.parse_k1i
ParsingBenchmarks.parse_l123456789
After Optimization f4d27ab
ParsingBenchmarks.parse_
ParsingBenchmarks.parse_a1i
ParsingBenchmarks.parse_c
ParsingBenchmarks.parse_k1i
ParsingBenchmarks.parse_l123456789