Skip to content

Misc fixes in the Array/MutArray code - #3255

Merged
harendra-kumar merged 10 commits into
masterfrom
array-fixes
Oct 14, 2026
Merged

harendra-kumar merged 10 commits into
masterfrom
array-fixes

Conversation

@harendra-kumar

Copy link
Copy Markdown
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
harendra-kumar merged commit 38cc25b into master Oct 14, 2026
16 of 20 checks passed
@harendra-kumar
harendra-kumar deleted the array-fixes branch October 21, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant