on master help sign in
Improve performance of manyTill
commitf75b368f3ee33704af16e3b34e3afe5439563d8e authorHarendra Kumar <harendra@composewell.com> dateMon 27 Feb 2023, 12:15 (4 years ago) parentecb378a
1 file changed+17−18
core/src/Streamly/Internal/Data/Parser/ParserD.hs+17−18
core/src/Streamly/Internal/Data/Parser/ParserD.hs+17−18
@@ -3506,9 +3506,10 @@ manyTillP :: -- Monad m =>
35063506manyTillP _p1 _p2 _f = undefined
35073507 -- D.toParserK $ D.manyTillP (D.fromParserK p1) (D.fromParserK p2) f
35083508
3509+{-# ANN type ManyTillState Fuse #-}
35093510data ManyTillState fs sr sl
3510− = ManyTillR Int fs sr
3511− | ManyTillL Int fs sl
3511+ = ManyTillR !Int !fs !sr
3512+ | ManyTillL !fs !sl
35123513
35133514-- | @manyTill chunking test f@ tries the parser @test@ on the input, if @test@
35143515-- fails it backtracks and tries @chunking@, after @chunking@ succeeds @test@ is
@@ -3533,7 +3534,7 @@ manyTill (Parser stepL initialL extractL)
35333534 scrutL fs p c d e = do
35343535 resL <- initialL
35353536 case resL of
3536− IPartial sl -> return $ c (ManyTillL 0 fs sl)
3537+ IPartial sl -> return $ c (ManyTillL fs sl)
35373538 IDone bl -> do
35383539 fr <- fstep fs bl
35393540 case fr of
@@ -3568,25 +3569,25 @@ manyTill (Parser stepL initialL extractL)
35683569 resL <- initialL
35693570 case resL of
35703571 IPartial sl ->
3571− return $ Continue (cnt + 1) (ManyTillL 0 fs sl)
3572+ return $ Continue (cnt + 1) (ManyTillL fs sl)
35723573 IDone bl -> do
35733574 fr <- fstep fs bl
35743575 let cnt1 = cnt + 1
3575− p = Partial cnt
3576− c = Continue cnt
3577− d = Done cnt
35783576 case fr of
3579− FL.Partial fs1 -> scrutR fs1 p c d Error
3577+ FL.Partial fs1 ->
3578+ scrutR
3579+ fs1
3580+ (Partial cnt1)
3581+ (Continue cnt1)
3582+ (Done cnt1)
3583+ Error
35803584 FL.Done fb -> return $ Done cnt1 fb
35813585 IError err -> return $ Error err
3582− -- XXX the cnt is being used only by the assert
3583− step (ManyTillL cnt fs st) a = do
3586+ step (ManyTillL fs st) a = do
35843587 r <- stepL st a
35853588 case r of
3586− Partial n s -> return $ Partial n (ManyTillL 0 fs s)
3587− Continue n s -> do
3588− assertM(cnt + 1 - n >= 0)
3589− return $ Continue n (ManyTillL (cnt + 1 - n) fs s)
3589+ Partial n s -> return $ Partial n (ManyTillL fs s)
3590+ Continue n s -> return $ Continue n (ManyTillL fs s)
35903591 Done n b -> do
35913592 fs1 <- fstep fs b
35923593 case fs1 of
@@ -3595,7 +3596,7 @@ manyTill (Parser stepL initialL extractL)
35953596 FL.Done b1 -> return $ Done n b1
35963597 Error err -> return $ Error err
35973598
3598− extract (ManyTillL cnt fs sR) = do
3599+ extract (ManyTillL fs sR) = do
35993600 res <- extractL sR
36003601 case res of
36013602 Done n b -> do
@@ -3604,9 +3605,7 @@ manyTill (Parser stepL initialL extractL)
36043605 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
36053606 FL.Done c -> return (Done n c)
36063607 Error err -> return $ Error err
3607− Continue n s -> do
3608− assertM(n == cnt)
3609− return $ Continue n (ManyTillL 0 fs s)
3608+ Continue n s -> return $ Continue n (ManyTillL fs s)
36103609 Partial _ _ -> error "Partial in extract"
36113610 extract (ManyTillR _ fs _) = fmap (Done 0) $ fextract fs
36123611