Skip to content

Fix Fold.takeEndBySeq for negative and large elements - #3267

Merged
harendra-kumar merged 2 commits into
masterfrom
splitops-fixes
Oct 21, 2026
Merged

harendra-kumar merged 2 commits into
masterfrom
splitops-fixes

Conversation

@harendra-kumar

Copy link
Copy Markdown
Member
  • When a short pattern is packed into a Word, a negative element set the upper bits, so with Int8 the pattern [1,-1] matched [-1,-1]. Each element is now masked to its own size.
  • The Karp-Rabin hash used for long patterns called fromEnum, which fails on Word64 values above maxBound :: Int. It now hashes the element bytes, as splitSepByOneOf does.

When the pattern fits in a machine word, Fold.takeEndBySeq packs the
elements into a Word using fromEnum. A negative fromEnum sets all the
upper bits, so for Int8 the pattern [1,-1] matched the input [-1,-1].
addToWord now masks each element to its size.

The Karp-Rabin hash used for longer patterns in Fold.takeEndBySeq and
Fold.takeEndBySeq_ called fromEnum, which fails on Word64 values above
maxBound :: Int. Use the byte hash from Streamly.Internal.Data.SeqMatch,
as splitSepByOneOf does. In takeEndBySeq use RingArray.replace_ as the
replaced element is not needed.

On GHC 9.14.1 the step of foldMany (takeEndBySeq ...) is now
specialised for every fold state, the state is no longer boxed for each
element. In the Data.Fold benchmarks, "takeEndBySeq suffix crlf"
allocates 56x less and is 2.8x faster, the 26 element pattern allocates
86x less and is 2.4x faster.
When the pattern fits in a machine word, the functions that emit the
buffered elements rebuild them from the packed word without sign
extension, toEnum 255 :: Int8 fails on the input [-1,3,4] with the
pattern [1,2]. Add a Known Issues note to the Haddock docs of
Fold.takeEndBySeq_ and of the Stream takeEndBySeq_, splitSepBySeq_,
splitEndBySeq_, dropMatches, replaceMatches and dropSuffix.
@harendra-kumar
harendra-kumar merged commit 1e6a1c0 into master Oct 21, 2026
18 of 19 checks passed
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