Repository navigation
Misc fixes in the Array/MutArray code - #3255
Merged
Merged
Conversation
harendra-kumar
commented
Oct 14, 2026
Member
- chunksOf (Array, MutArray and the Generic versions) emitted an empty array at the end when the stream length was a multiple of the chunk size, and one empty array for an empty stream.
- appendMax on a slice could return an array whose capacity went past its allocation, so appending to the result could corrupt memory. It also dropped any free space the array already had.
- createMinOf 0 wrote its first element into a zero-length allocation.
- createOf/createWithOf now give a capacity of at least n, which is what the docs said all along.
- splice and growTo/growBy/growExp no longer reallocate when the data fits exactly.
- Internal Array.unsnoc was returning the tail instead of the init.
- Smaller things: appendN is moved to the deprecated section, stale module names in the chunksOf error messages are fixed, and some doc equivalences that didn't typecheck are removed.
spliceWith reallocated the first array when the second array exactly filled its reserved space, the check was end + srcBytes >= bound instead of >. The result was a copy instead of the first array extended in place. This affects splice, spliceWith and spliceExp.
chunksOf emitted an empty array when the stream ended on a chunk boundary, e.g. chunksOf 3 on [1..9] gave [[1,2,3],[4,5,6],[7,8,9],[]], and [[]] for an empty stream. Fixed in both the unboxed and generic MutArray versions, the Array versions are defined using them.
The test suite mains for Data.Array and Data.MutArray ran only typeMain. arrayCommon and typeCommon were run only by the main function of the Type modules, which is not the main of any test suite. Remove that main and the imports and bindings used only by it. Move maxTestCount from the shared MutArray/Common.hs fragment to the Generic test modules, the only modules including it that use it.
unsafeAppendMax computed the bound of the result from the end of the input array. When the input array was reallocated its start became 0, so for a slice with a non-zero start the bound pointed beyond the new allocation, e.g. appending 3 Ints to a 2 Int slice at offset 5 gave a bound of 80 bytes on a 40 byte allocation. A later snoc on the result would write out of bounds. When the requested count needed less space than the array already had free, the bound was computed from the requested size and the returned array lost the remaining reserved capacity. Use at least the free space when computing the new bound.
When the array was full writeWithAs grew it to max (oldSize * 2) 1 bytes. For an empty array, e.g. createMinOf 0, that is 1 byte, which realloc rounds down to 0 bytes for any element larger than a byte, and the first element was written past the end of the allocation. Grow to at least one element instead.
growTo, growBy and growExp reallocated the array when the requested capacity was equal to the current capacity, the check was req < cap instead of req <= cap. This also made growTo 0 on an empty array reallocate, contrary to its documentation.
createOf and createWithOf were documented to return an array of capacity at least n, but unsafeCreateWithOf sets the bound to the end of the written elements, dropping the unused capacity. Document the actual capacity. Keeping the capacity in unsafeCreateWithOf showed a large slowdown of createOf in the CI benchmarks, add a PERF note with the measurements. Remove the createWithOf equivalence with appendN and the unsafeCreateWithOf equivalence with unsafeAppendN, both had their arguments swapped. createMinOf claimed the capacity is at least the initial count, but the result is resized with rightSize. Remove the Pre-release tag from Array.chunksOf and MutArray.chunksOf, both are exported from the released modules.
The modules Streamly.Internal.Data.MutArray.Mut.Type and Streamly.Internal.Data.Array.Generic.Mut.Type no longer exist.
unsnoc [1,2,3] returned (fromList [2,3], 3) instead of (fromList [1,2], 3).
harendra-kumar
force-pushed
the
array-fixes
branch
from
October 14, 2026 20:45
fea14a2 to
38cc25b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.