improved performance of ++ for Bit/ByteVector and made other related performance improvements #19
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.
There's now a new constructor
Chunks
, in bothBitVector
andByteVector
. It uses the same idea as an (amortized) O(1) functional counter - when new chunks are added to the end, if they are more than half the size of the chunk immediately to the left, those chunks are combined. This maintains a sequence of balanced trees of exponentially decreasing sizes, so appends take amortized O(1) time, rather than O(log n). I also changed ByteVector to use a small (64 byte) buffer by default in++
, which provided some drastic performance improvements for the common case (without penalizing the other cases).Update: Also, I forgot to mention that
Buffer
no longer has the weird discontinuity in its performance if you look past the first chunk. Basically,Buffer
only handles providing the mutable tail, andChunks
takes care of appending in O(1). So a bufferedByteVector
still has logarithmic take/drop/get. This is why I felt pretty good about adding a small default buffer size.Here's some numbers from current master:
And here's some numbers from this branch:
It looks like
++
forBitVector
is close to the speed of::
onList
. That's to build up a million byteBitVector
, one byte at a type, using a left fold. ForByteVector
, it's much faster, as it gets to directly use the mutable (only 64 byte) array at the tail.Here's the code that produced that: