on master help sign in
Add some notes about parseBreak op in ArrayStream
commit3bab5b790c6c0beb613ca7560808484bd137d43b authorHarendra Kumar <harendra@composewell.com> dateWed 27 Dec 2023, 02:36 (3 years ago) parentf50789e
1 file changed+35−5
core/src/Streamly/Internal/Data/Array/Stream.hs+35−5
core/src/Streamly/Internal/Data/Array/Stream.hs+35−5
@@ -36,10 +36,11 @@ module Streamly.Internal.Data.Array.Stream
3636 -- could also be implemented using parseBreak.
3737 , foldBreak
3838 , foldBreakD
39− -- The byte level parseBreak cannot work efficiently. Because the stream
40− -- will have to be a StreamK for backtracking, StreamK at byte level would
41− -- not be efficient.
42− , parseBreak -- StreamK.parseBreakChunks
39+ -- This is chunked parseBreak. A byte level parseBreak cannot work
40+ -- efficiently. Because the stream will have to be a StreamK for
41+ -- backtracking, StreamK at byte level would not be efficient.
42+ -- parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
43+ , parseBreak
4344 -- , parseBreakD
4445 -- , foldManyChunks
4546 -- , parseManyChunks
@@ -77,6 +78,8 @@ where
7778import Data.Bifunctor (second)
7879import Control.Exception (assert)
7980import Control.Monad.IO.Class (MonadIO(..))
81+-- import Data.Bifunctor (first)
82+-- import Data.Either (fromRight)
8083import Data.Proxy (Proxy(..))
8184import Data.Word (Word8)
8285import Streamly.Internal.Data.Unbox (Unbox(..))
@@ -103,6 +106,7 @@ import qualified Streamly.Internal.Data.Fold.Type as FL (Fold(..), Step(..))
103106import qualified Streamly.Internal.Data.Parser as PR
104107import qualified Streamly.Internal.Data.Parser as PRD
105108 (Parser(..), Initial(..))
109+-- import qualified Streamly.Internal.Data.ParserK as ParserK
106110import qualified Streamly.Internal.Data.Stream as D
107111import qualified Streamly.Internal.Data.StreamK as K
108112
@@ -403,6 +407,17 @@ foldBreakK (FL.Fold fstep initial _ final) stream = do
403407--
404408-- > foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
405409--
410+-- Instead of using this we can adapt the fold to ParserK and use
411+-- parseBreakChunks instead. ParserK allows composing using Monad as well.
412+--
413+-- @
414+-- foldBreak f s =
415+-- fmap (first (fromRight undefined))
416+-- $ K.parseBreakChunks (ParserK.adaptC (PR.fromFold f)) s
417+-- @
418+--
419+-- We can compare perf and remove this one or define it in terms of that.
420+--
406421-- /Internal/
407422--
408423{-# INLINE_NORMAL foldBreak #-}
@@ -411,8 +426,14 @@ foldBreak ::
411426 => Fold m a b
412427 -> StreamK m (A.Array a)
413428 -> m (b, StreamK m (A.Array a))
414−-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
415429foldBreak = foldBreakK
430+--
431+-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
432+--
433+-- foldBreak f s =
434+-- fmap (first (fromRight undefined))
435+-- $ K.parseBreakChunks (ParserK.adaptC (PR.fromFold f)) s
436+--
416437-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
417438-- rule to rewrite runArrayFoldBreak to fold.
418439-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
@@ -799,6 +820,14 @@ parseBreakK (PRD.Parser pstep initial extract) stream = do
799820-- | Parse an array stream using the supplied 'Parser'. Returns the parse
800821-- result and the unconsumed stream. Throws 'ParseError' if the parse fails.
801822--
823+-- >> parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
824+--
825+-- This is redundant and we can just use parseBreakChunks, as ParserK can be
826+-- composed using Monad. The only advantage of this is that we do not need to
827+-- adapt.
828+--
829+-- We can compare perf and remove this one or define it in terms of that.
830+--
802831-- /Internal/
803832--
804833{-# INLINE_NORMAL parseBreak #-}
@@ -812,6 +841,7 @@ parseBreak p s =
812841 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
813842-}
814843parseBreak = parseBreakK
844+-- parseBreak p = K.parseBreakChunks (ParserK.adaptC p)
815845
816846-------------------------------------------------------------------------------
817847-- Elimination - Running Array Folds and parsers