on master help sign in
Bug fix in StreamD.splitOnSeq karp-rabin match
commite49a37d8b4b470e2c85a3534ae758df8a1494e0d authoradithyaov <adithya@composewell.com> dateSat 7 Nov 2020, 20:25 (6 years ago) parente57424d
1 file changed+16−22
src/Streamly/Internal/Data/Stream/StreamD.hs+16−22
src/Streamly/Internal/Data/Stream/StreamD.hs+16−22
@@ -1801,6 +1801,7 @@ data SplitOnSeqState rb rh ck w fs s b x =
18011801
18021802 | SplitOnSeqKRInit Int s rb !rh
18031803 | SplitOnSeqKRLoop fs s rb !rh !ck
1804+ | SplitOnSeqKRCheck fs s rb !rh
18041805 | SplitOnSeqKRDone Int !fs rb !rh
18051806
18061807{-# INLINE_NORMAL splitOnSeq #-}
@@ -1983,27 +1984,15 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
19831984 then do
19841985 let fold = RB.unsafeFoldRing (RB.ringBound rb)
19851986 let !ringHash = fold addCksum 0 rb
1987+ fs <- initial
19861988 if ringHash == patHash
1987− then do
1988− r <- initial >>= done
1989− let next = SplitOnSeqKRInit 0 s rb rh1
1990− -- XXX We need a direct yield here otherwise GHC is not
1991− -- able to fuse the code. This GHC issue needs to be
1992− -- investigated. We should know why.
1993− return $ Yield r next
1994− else do
1995− fs <- initial
1996− skip $ SplitOnSeqKRLoop fs s rb rh1 ringHash
1989+ then skip $ SplitOnSeqKRCheck fs s rb rh1
1990+ else skip $ SplitOnSeqKRLoop fs s rb rh1 ringHash
19971991 else skip $ SplitOnSeqKRInit (idx + 1) s rb rh1
19981992 Skip s -> skip $ SplitOnSeqKRInit idx s rb rh
19991993 Stop -> do
20001994 fs <- initial
2001− let rh1 = RB.moveBy (0 - idx) rb (rh :: Ptr a)
2002− if idx /= 0
2003− then skip $ SplitOnSeqKRDone idx fs rb rh1
2004− else do
2005− r <- done fs
2006− skip $ SplitOnSeqYield r SplitOnSeqDone
1995+ skip $ SplitOnSeqKRDone idx fs rb (RB.startOf rb)
20071996
20081997 -- XXX The recursive "go" is more efficient than the state based recursion
20091998 -- code commented out below. Perhaps its more efficient because of
@@ -2021,13 +2010,10 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
20212010 old <- liftIO $ peek rh
20222011 let cksum1 = deltaCksum cksum old x
20232012 fs1 <- fstep fs old
2013+ rh1 <- liftIO (RB.unsafeInsert rb rh x)
20242014 if (cksum1 == patHash)
2025− then do
2026− r <- done fs1
2027− skip $ SplitOnSeqYield r $ SplitOnSeqKRInit 0 s rb rh
2028− else do
2029− rh1 <- liftIO (RB.unsafeInsert rb rh x)
2030− go SPEC fs1 s rh1 cksum1
2015+ then skip $ SplitOnSeqKRCheck fs1 s rb rh1
2016+ else go SPEC fs1 s rh1 cksum1
20312017 Skip s -> go SPEC fs s rh cksum
20322018 Stop -> skip $ SplitOnSeqKRDone patLen fs rb rh
20332019
@@ -2056,6 +2042,14 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
20562042 Skip s -> skip $ SplitOnSeqKRLoop fs s rb rh cksum
20572043 Stop -> skip $ SplitOnSeqKRDone patLen fs rb rh
20582044 -}
2045+
2046+ stepOuter _ (SplitOnSeqKRCheck fs st rb rh) = do
2047+ if RB.unsafeEqArray rb rh patArr
2048+ then do
2049+ r <- done fs
2050+ let next = SplitOnSeqKRInit 0 st rb (RB.startOf rb)
2051+ skip $ SplitOnSeqYield r next
2052+ else skip $ SplitOnSeqKRLoop fs st rb rh patHash
20592053 stepOuter _ (SplitOnSeqKRDone 0 fs _ _) = do
20602054 r <- done fs
20612055 skip $ SplitOnSeqYield r SplitOnSeqDone