Improve Efficiency Of Chunk #3831
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.
Following up on discussion on Discord, this PR improves the efficiency of operations on concatenated chunks. The basic problem is that right now almost all chunk operations are implemented in terms of accessing each index of the chunk. Indexed access is relatively efficient because of balanced concatenation and using underlying arrays, but because the data structure is not actually a flat array indexed access is not nearly as efficient as direct iteration, resulting in terrible performance when iterating over concatenated chunks.
We can address this in many cases by just using
foreachinstead of accessing each index. For example, here is a comparison withChunkand other data types formapandfoldLefton concatenated chunks (created by repeatedly concatenating 1,000 chunks of 1,000 elements each). The first set of results is for the current implementation. The second set of results is for a fully materialized chunk. The third set of results is usingforeachinstead of indexed access.The current implementation is orders of magnitude slower than other collections versus just using
foreachis as fast or faster than other collections. Right now I have only done this formapandfoldLeftbut I think we need to go through and make sure no operations onChunk, as opposed to theArrsubtype are doing indexed access and instead are all iterating usingforeachoriterator.Current Chunk
Materialized Chunk
Iteration Instead of Indexed Access
The current implementation is orders of magnitude slower than other collection types