on master help sign in
Specialize the case of 1 element backtracking
dfe3e45Harendra Kumar
core/src/Streamly/Internal/Data/Stream/StreamD/Eliminate.hs+25
@@ -178,6 +178,7 @@ parseBreak (PRD.Parser pstep initial extract) stream@(Stream step state) = do
178 pRes <- pstep pst x
178 pRes <- pstep pst x
179 case pRes of
179 case pRes of
180 PR.Partial 0 pst1 -> go SPEC s (List []) pst1
180 PR.Partial 0 pst1 -> go SPEC s (List []) pst1
181+ PR.Partial 1 pst1 -> go1 SPEC s x pst1
181 PR.Partial n pst1 -> do
182 PR.Partial n pst1 -> do
182 assert (n <= length (x:getList buf)) (return ())
183 assert (n <= length (x:getList buf)) (return ())
183 let src0 = Prelude.take n (x:getList buf)
184 let src0 = Prelude.take n (x:getList buf)
@@ -201,6 +202,30 @@ parseBreak (PRD.Parser pstep initial extract) stream@(Stream step state) = do
201 Skip s -> go SPEC s buf pst
202 Skip s -> go SPEC s buf pst
202 Stop -> goStop buf pst
203 Stop -> goStop buf pst
203
204
205+ go1 _ s x !pst = do
206+ pRes <- pstep pst x
207+ case pRes of
208+ PR.Partial 0 pst1 ->
209+ go SPEC s (List []) pst1
210+ PR.Partial 1 pst1 -> do
211+ go1 SPEC s x pst1
212+ PR.Partial n _ ->
213+ error $ "parseBreak: parser bug, go1: Partial n = " ++ show n
214+ PR.Continue 0 pst1 ->
215+ go SPEC s (List [x]) pst1
216+ PR.Continue 1 pst1 ->
217+ go1 SPEC s x pst1
218+ PR.Continue n _ -> do
219+ error $ "parseBreak: parser bug, go1: Continue n = " ++ show n
220+ PR.Done 0 b -> do
221+ return (b, Stream step s)
222+ PR.Done 1 b -> do
223+ return (b, StreamD.cons x (Stream step s))
224+ PR.Done n _ -> do
225+ error $ "parseBreak: parser bug, go1: Done n = " ++ show n
226+ PR.Error err ->
227+ throwM $ ParseError err
228+
204 gobuf !_ s buf (List []) !pst = go SPEC s buf pst
229 gobuf !_ s buf (List []) !pst = go SPEC s buf pst
205 gobuf !_ s buf (List (x:xs)) !pst = do
230 gobuf !_ s buf (List (x:xs)) !pst = do
206 pRes <- pstep pst x
231 pRes <- pstep pst x