on master help sign in
Improve performance of manyTill
f75b368Harendra Kumar
core/src/Streamly/Internal/Data/Parser/ParserD.hs+17−18
@@ -3506,9 +3506,10 @@ manyTillP :: -- Monad m =>
3506manyTillP _p1 _p2 _f = undefined
3506manyTillP _p1 _p2 _f = undefined
3507 -- D.toParserK $ D.manyTillP (D.fromParserK p1) (D.fromParserK p2) f
3507 -- D.toParserK $ D.manyTillP (D.fromParserK p1) (D.fromParserK p2) f
3508
3508
3509+{-# ANN type ManyTillState Fuse #-}
3509data ManyTillState fs sr sl
3510data ManyTillState fs sr sl
3510− = ManyTillR Int fs sr
3511+ = ManyTillR !Int !fs !sr
3511− | ManyTillL Int fs sl
3512+ | ManyTillL !fs !sl
3512
3513
3513-- | @manyTill chunking test f@ tries the parser @test@ on the input, if @test@
3514-- | @manyTill chunking test f@ tries the parser @test@ on the input, if @test@
3514-- fails it backtracks and tries @chunking@, after @chunking@ succeeds @test@ is
3515-- fails it backtracks and tries @chunking@, after @chunking@ succeeds @test@ is
@@ -3533,7 +3534,7 @@ manyTill (Parser stepL initialL extractL)
3533 scrutL fs p c d e = do
3534 scrutL fs p c d e = do
3534 resL <- initialL
3535 resL <- initialL
3535 case resL of
3536 case resL of
3536− IPartial sl -> return $ c (ManyTillL 0 fs sl)
3537+ IPartial sl -> return $ c (ManyTillL fs sl)
3537 IDone bl -> do
3538 IDone bl -> do
3538 fr <- fstep fs bl
3539 fr <- fstep fs bl
3539 case fr of
3540 case fr of
@@ -3568,25 +3569,25 @@ manyTill (Parser stepL initialL extractL)
3568 resL <- initialL
3569 resL <- initialL
3569 case resL of
3570 case resL of
3570 IPartial sl ->
3571 IPartial sl ->
3571− return $ Continue (cnt + 1) (ManyTillL 0 fs sl)
3572+ return $ Continue (cnt + 1) (ManyTillL fs sl)
3572 IDone bl -> do
3573 IDone bl -> do
3573 fr <- fstep fs bl
3574 fr <- fstep fs bl
3574 let cnt1 = cnt + 1
3575 let cnt1 = cnt + 1
3575− p = Partial cnt
3576− c = Continue cnt
3577− d = Done cnt
3578 case fr of
3576 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
3580 FL.Done fb -> return $ Done cnt1 fb
3584 FL.Done fb -> return $ Done cnt1 fb
3581 IError err -> return $ Error err
3585 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
3584 r <- stepL st a
3587 r <- stepL st a
3585 case r of
3588 case r of
3586− Partial n s -> return $ Partial n (ManyTillL 0 fs s)
3589+ Partial n s -> return $ Partial n (ManyTillL fs s)
3587− Continue n s -> do
3588− assertM(cnt + 1 - n >= 0)
3589− return $ Continue n (ManyTillL (cnt + 1 - n) fs s)
3590+ Continue n s -> return $ Continue n (ManyTillL fs s)
3590 Done n b -> do
3591 Done n b -> do
3591 fs1 <- fstep fs b
3592 fs1 <- fstep fs b
3592 case fs1 of
3593 case fs1 of
@@ -3595,7 +3596,7 @@ manyTill (Parser stepL initialL extractL)
3595 FL.Done b1 -> return $ Done n b1
3596 FL.Done b1 -> return $ Done n b1
3596 Error err -> return $ Error err
3597 Error err -> return $ Error err
3597
3598
3598− extract (ManyTillL cnt fs sR) = do
3599+ extract (ManyTillL fs sR) = do
3599 res <- extractL sR
3600 res <- extractL sR
3600 case res of
3601 case res of
3601 Done n b -> do
3602 Done n b -> do
@@ -3604,9 +3605,7 @@ manyTill (Parser stepL initialL extractL)
3604 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3605 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
3605 FL.Done c -> return (Done n c)
3606 FL.Done c -> return (Done n c)
3606 Error err -> return $ Error err
3607 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)
3610 Partial _ _ -> error "Partial in extract"
3609 Partial _ _ -> error "Partial in extract"
3611 extract (ManyTillR _ fs _) = fmap (Done 0) $ fextract fs
3610 extract (ManyTillR _ fs _) = fmap (Done 0) $ fextract fs
3612
3611