Improve performance of HTML parser on JVM #67
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The HTML parser incurs a significant slowdown as the nesting level
increases:
$ bloop run pine-bench-jvm -- slow [...] Benchmark: Parse HTML w/o attributes - depth=2: units: 7 iterations: 591733 run time: 3384 μs/it ± 7 - depth=6: units: 127 iterations: 11610 run time: 171586 μs/it ± 504 - depth=10: units: 2047 iterations: 56 run time: 36148809 μs/it ± 74820 - depth=14: units: 32767 iterations: 1 run time: 9353666666 μs/it ± 174704194 Summary: Unit growth: 18.1x, 16.1x, 16.0x Run time growth: 50.7x, 210.7x, 258.8x
This slowdown can be attributed to the
rest()
function inReader
.It calls
data.drop()
which on the JVM creates a copy of the stringrather than pointing to the same memory.
Scala.js'
drop()
implementation has the expected semantics such thatthe run time performance is roughly linear to the number of nodes in the
tree:
$ bloop run pine-bench-js -- slow [...] Benchmark: Parse HTML w/o attributes - depth=2: units: 7 iterations: 92592 run time: 21624 μs/it ± 128 - depth=6: units: 127 iterations: 5229 run time: 382531 μs/it ± 182 - depth=10: units: 2047 iterations: 312 run time: 6479611 μs/it ± 134542 - depth=14: units: 32767 iterations: 17 run time: 119013071 μs/it ± 1455066 Summary: Unit growth: 18.1x, 16.1x, 16.0x Run time growth: 17.7x, 16.9x, 18.4x
After applying the optimisations, the parser will behave similarly on
the JVM: