Performance improvement (addresses #15) #17
Closed
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.
This update implements some lower-level functions to improve the performance of standard heap operations on
SwiftPriorityQueue
and adds performance tests forinit
,pop
,push
andremove
. Hopefully the improvement is sufficient to close #15.A naive implementation replacing
sink
andswim
with versions that can be called from insideArray.withUnsafeMutableBufferPointer(_:)
already gave promising performance forinit
,pop
andpush
. (See forked master branch. This might represent a good compromise solution if the final code is deemed to be too complex.)A more in-depth rewrite of the internal code (with inspiration from libcxx/include/algorithm) gave further performance improvements, although the resulting code is less "easy" to understand. In an effort to aid readability, the new code is extensively (some might say excessively!) marked up.
Building a
SwiftPriorityQueue
from anArray
of 100,000 values went from 17.8 ms to 4.1 ms.Emptying a
SwiftPriorityQueue
with 100,000 values usingpop()
went from 2.11 s to 0.36 s.Calling
remove(_:)
on 100 random items from aSwiftPriorityQueue
with 100,000 values went from 2.36 s to 0.277 s.Building a
SwiftPriorityQueue
by callingpush(_:)
100,000 times went from 118 ms to 149 ms. Not certain what happened there! Further investigation may be required, but in any case, the performance improvement inpop()
more than compensates in normal usage wherepop()
andpush(_:)
are called equally often.