Skip to content

Commit 31e59e1

Browse files
committed
Hand-written concat for better performance.
1 parent 21a0a33 commit 31e59e1

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/Streaming/Prelude.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,18 @@ chain f = loop where
557557
-}
558558

559559
concat :: (Monad m, Foldable.Foldable f) => Stream (Of (f a)) m r -> Stream (Of a) m r
560-
concat str = for str each
561-
{-# INLINE concat #-}
560+
concat = loop
561+
where
562+
loop str = case str of
563+
Return r -> Return r
564+
Effect m -> Effect (fmap loop m)
565+
Step (lst :> as) ->
566+
let inner [] = loop as
567+
inner (x:rest) = Step (x :> inner rest)
568+
in inner (Foldable.toList lst)
569+
{-# INLINABLE concat #-}
570+
-- The above hand-written loop is ~20% faster than the 'for' implementation
571+
-- concat str = for str each
562572

563573
{-| The natural @cons@ for a @Stream (Of a)@.
564574

0 commit comments

Comments
 (0)