Skip to content

Commit 3cbc0a6

Browse files
Mask packed elements and hash bytes in Fold.takeEndBySeq
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 called fromEnum, which fails on Word64 values above maxBound :: Int. Use the byte hash from Streamly.Internal.Data.SeqMatch, as splitSepByOneOf does. Use RingArray.replace_ as the replaced element is not needed.
1 parent ddc9afd commit 3cbc0a6

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎core/src/Streamly/Internal/Data/Fold/Combinators.hs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,8 @@ takeEndBySeq patArr (Fold fstep finitial fextract ffinal) =
13951395
-- Word pattern related
13961396
elemBits = SIZE_OF(a) * 8
13971397

1398+
proxy = Proxy :: Proxy a
1399+
13981400
wordMask :: Word
13991401
wordMask = (1 `shiftL` (elemBits * patLen)) - 1
14001402

@@ -1403,20 +1405,18 @@ takeEndBySeq patArr (Fold fstep finitial fextract ffinal) =
14031405

14041406
addToWord = SeqMatch.addToWord elemBits
14051407

1406-
-- For Rabin-Karp search
1408+
-- For Rabin-Karp search, hashes of the element bytes
14071409
coeff = SeqMatch.hashPower patLen
14081410

1409-
addCksum cksum a = SeqMatch.addHash cksum (fromIntegral (fromEnum a))
1410-
1411-
deltaCksum cksum old new =
1412-
addCksum cksum new - coeff * fromIntegral (fromEnum old)
1411+
deltaCksum cksum oldHash newHash =
1412+
SeqMatch.addHash cksum newHash - coeff * oldHash
14131413

14141414
-- XXX shall we use a random starting hash or 1 instead of 0?
14151415
-- XXX Need to keep this cached across fold calls in foldmany
14161416
-- XXX We may need refold to inject the cached state instead of
14171417
-- initializing the state every time.
14181418
-- XXX Allocation of ring buffer should also be done once
1419-
patHash = Array.foldl' addCksum 0 patArr
1419+
patHash = SeqMatch.arrayHash patArr
14201420

14211421
step (SplitOnSeqEmpty s) x = do
14221422
res <- fstep s x
@@ -1464,7 +1464,7 @@ takeEndBySeq patArr (Fold fstep finitial fextract ffinal) =
14641464
, arrStart = 0
14651465
, arrEnd = patBytes
14661466
}
1467-
let ringHash = Array.foldl' addCksum 0 arr
1467+
let ringHash = SeqMatch.arrayHash arr
14681468
if ringHash == patHash && Array.byteEq arr patArr
14691469
then Done <$> ffinal s1
14701470
else return $ Partial $ SplitOnSeqKRLoop s1 ringHash mba 0
@@ -1480,8 +1480,10 @@ takeEndBySeq patArr (Fold fstep finitial fextract ffinal) =
14801480
, ringSize = patBytes
14811481
, ringHead = offset
14821482
}
1483-
(rb1, old :: a) <- liftIO (RingArray.replace rb x)
1484-
let ringHash = deltaCksum cksum old x
1483+
oldHash <- liftIO $ SeqMatch.elemHashAt proxy mba offset
1484+
rb1 <- liftIO (RingArray.replace_ rb x)
1485+
newHash <- liftIO $ SeqMatch.elemHashAt proxy mba offset
1486+
let ringHash = deltaCksum cksum oldHash newHash
14851487
let rh1 = ringHead rb1
14861488
matches <-
14871489
if ringHash == patHash

‎core/src/Streamly/Internal/Data/SeqMatch.hs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ import Streamly.Internal.System.IO (unsafeInlineIO)
4646
elemMask :: Int -> Word
4747
elemMask bits = (1 `shiftL` bits) - 1
4848

49-
-- | @addToWord bits word a@ shifts @word@ left by @bits@ and puts
50-
-- @fromEnum a@ in the vacated bits.
49+
-- | @addToWord bits word a@ shifts @word@ left by @bits@ and puts the low
50+
-- @bits@ bits of @fromEnum a@ in the vacated bits.
5151
{-# INLINE addToWord #-}
5252
addToWord :: Enum a => Int -> Word -> a -> Word
53-
addToWord bits wd a = (wd `shiftL` bits) .|. fromIntegral (fromEnum a)
53+
addToWord bits wd a =
54+
(wd `shiftL` bits) .|. (fromIntegral (fromEnum a) .&. elemMask bits)
5455

5556
-- | @wordToElem bits word@ decodes the element packed by 'addToWord' in the
5657
-- low @bits@ bits of @word@, @bits@ must be less than the word size.

‎test/Streamly/Test/Data/Fold/Combinators.hs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,10 @@ main = hspec $ do
590590
it "slide2" slide2
591591
it "takeEndBySeq" takeEndBySeq
592592
it "takeEndBySeq_" takeEndBySeq_
593-
xit "takeEndBySeq signed elements" takeEndBySeqSigned
593+
it "takeEndBySeq signed elements" takeEndBySeqSigned
594594
xit "takeEndBySeq_ signed elements" takeEndBySeq_Signed
595595
xit "takeEndBySeq_ unit" takeEndBySeq_Unit
596-
xit "takeEndBySeq Word64" takeEndBySeqWord64
596+
it "takeEndBySeq Word64" takeEndBySeqWord64
597597
xit "takeEndBySeq_ Word64" takeEndBySeq_Word64
598598
it "distributeScan" distributeScan
599599
it "distributeScan last input" distributeScanLast

0 commit comments

Comments
 (0)