From 52fdd4387bb8c1b5ea8b1d00b7c35daa8f689f2d Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 01:56:02 +0530 Subject: [PATCH 01/15] Move low-level benchmarks from Generate to Type --- .../Benchmark/Data/Stream/Generate.hs | 71 ------------------- .../Streamly/Benchmark/Data/Stream/Type.hs | 68 ++++++++++++++++++ 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs index 87de9ed405..c9306b137b 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs @@ -6,12 +6,9 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE RankNTypes #-} -{-# OPTIONS_GHC -Wno-orphans #-} - #ifdef __HADDOCK_VERSION__ #undef INSPECTION #endif @@ -30,19 +27,15 @@ import Test.Inspection import Control.DeepSeq (NFData(..)) import Control.Monad.IO.Class (MonadIO) -import Data.Functor.Identity (Identity(..)) import Streamly.Internal.Data.Stream (Stream) import Streamly.Internal.Data.Time.Units (AbsTime) import System.Random (randomRIO) -import qualified GHC.Exts as GHC -import qualified Streamly.Internal.Data.Fold as Fold import qualified Streamly.Internal.Data.Stream as Stream import Test.Tasty.Bench import Stream.Common hiding (benchIO) import Streamly.Benchmark.Common -import qualified Prelude import Prelude hiding (repeat, replicate, iterate) @@ -59,10 +52,6 @@ withDrain :: (Int -> Stream IO a) -> IO () withDrain f = withRandomIntIO $ \n -> drain (f n) -- XXX should we use rnf to evaluate the result? -{-# INLINE withDrainPure #-} -withDrainPure :: (Int -> Stream Identity a) -> IO () -withDrainPure f = withRandomIntIO $ \n -> return $! runIdentity $ drain (f n) - {-# INLINE fromListM #-} fromListM :: Monad m => [m a] -> Stream m a fromListM = Stream.sequence . Stream.fromList @@ -78,46 +67,6 @@ inspect $ 'sourceFromListM `hasNoType` ''Fold.Step inspect $ 'sourceFromListM `hasNoType` ''SPEC #endif -{-# INLINE sourceIsList #-} -sourceIsList :: Int -> IO () -sourceIsList value = withDrainPure $ \n -> GHC.fromList [n..n+value] - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'sourceIsList -inspect $ 'sourceIsList `hasNoType` ''Stream.Step -inspect $ 'sourceIsList `hasNoType` ''Fold.Step -inspect $ 'sourceIsList `hasNoType` ''SPEC -#endif - -{-# INLINE sourceIsString #-} -sourceIsString :: Int -> IO () -sourceIsString value = withDrainPure $ \n -> - GHC.fromString (Prelude.replicate (n + value) 'a') - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'sourceIsString -inspect $ 'sourceIsString `hasNoType` ''Stream.Step -inspect $ 'sourceIsString `hasNoType` ''Fold.Step -inspect $ 'sourceIsString `hasNoType` ''SPEC -#endif - -{-# INLINE readInstance #-} -readInstance :: String -> Stream Identity Int -readInstance str = - let r = reads str - in case r of - [(x,"")] -> x - _ -> error "readInstance: no parse" - --- For comparisons -{-# INLINE readInstanceList #-} -readInstanceList :: String -> [Int] -readInstanceList str = - let r = reads str - in case r of - [(x,"")] -> x - _ -> error "readInstance: no parse" - {-# INLINE replicate #-} replicate :: Int -> IO () replicate value = withDrain (Stream.replicate value) @@ -343,8 +292,6 @@ o_1_space_generation value = , benchIO "fracFromThenTo" $ sourceFracFromThenTo value , benchIO "fracFromTo" $ sourceFracFromTo value , benchIO "fromListM" $ sourceFromListM value - , benchIO "IsList.fromList" $ sourceIsList value - , benchIO "IsString.fromString" $ sourceIsString value , benchIO "enumerateFrom" $ enumerateFrom value , benchIO "enumerateFromTo" $ enumerateFromTo value , benchIO "enumerateFromThen" $ enumerateFromThen value @@ -365,23 +312,6 @@ o_1_space_generation value = ] ] -instance NFData a => NFData (Stream Identity a) where - {-# INLINE rnf #-} - rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs - -o_n_heap_generation :: Int -> [Benchmark] -o_n_heap_generation value = - [ bgroup "buffered" - -- Buffers the output of show/read. - -- XXX can the outputs be streaming? Can we have special read/show - -- style type classes, readM/showM supporting streaming effects? - [ bench "readsPrec pure streams" $ - nf (readInstance . mkString) value - , bench "readsPrec Haskell lists" $ - nf (readInstanceList . mkListString) value - ] - ] - ------------------------------------------------------------------------------- -- Main ------------------------------------------------------------------------------- @@ -392,4 +322,3 @@ o_n_heap_generation value = benchmarks :: Int -> [(SpaceComplexity, Benchmark)] benchmarks size = map (SpaceO_1,) (o_1_space_generation size) - ++ map (HeapO_n,) (o_n_heap_generation size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Type.hs b/benchmark/Streamly/Benchmark/Data/Stream/Type.hs index 91f9024f6a..dcfe2ecb83 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Type.hs @@ -10,6 +10,8 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE RankNTypes #-} +{-# OPTIONS_GHC -Wno-orphans #-} + #ifdef __HADDOCK_VERSION__ #undef INSPECTION #endif @@ -45,6 +47,8 @@ import Streamly.Internal.Data.Stream (Stream) import Streamly.Data.Unfold (Unfold) import System.Random (randomRIO) +import qualified GHC.Exts as GHC + import qualified Streamly.Internal.Data.Fold as FL import qualified Streamly.Internal.Data.Fold as Fold import qualified Streamly.Internal.Data.Refold.Type as Refold @@ -71,6 +75,10 @@ withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f withDrain :: (Int -> Stream IO a) -> IO () withDrain f = withRandomIntIO $ \n -> drain (f n) +{-# INLINE withDrainPure #-} +withDrainPure :: (Int -> Stream Identity a) -> IO () +withDrainPure f = withRandomIntIO $ \n -> return $! runIdentity $ drain (f n) + {-# INLINE withStream #-} withStream :: Int -> (Stream IO Int -> IO b) -> IO b withStream value f = withRandomIntIO (f . sourceUnfoldrM value) @@ -118,11 +126,70 @@ inspect $ 'sourceFromTuple `hasNoType` ''Fold.Step inspect $ 'sourceFromTuple `hasNoType` ''SPEC #endif +{-# INLINE sourceIsList #-} +sourceIsList :: Int -> IO () +sourceIsList value = withDrainPure $ \n -> GHC.fromList [n..n+value] + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'sourceIsList +inspect $ 'sourceIsList `hasNoType` ''Stream.Step +inspect $ 'sourceIsList `hasNoType` ''Fold.Step +inspect $ 'sourceIsList `hasNoType` ''SPEC +#endif + +{-# INLINE sourceIsString #-} +sourceIsString :: Int -> IO () +sourceIsString value = withDrainPure $ \n -> + GHC.fromString (Prelude.replicate (n + value) 'a') + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'sourceIsString +inspect $ 'sourceIsString `hasNoType` ''Stream.Step +inspect $ 'sourceIsString `hasNoType` ''Fold.Step +inspect $ 'sourceIsString `hasNoType` ''SPEC +#endif + +{-# INLINE readInstance #-} +readInstance :: String -> Stream Identity Int +readInstance str = + let r = reads str + in case r of + [(x,"")] -> x + _ -> error "readInstance: no parse" + +-- For comparisons +{-# INLINE readInstanceList #-} +readInstanceList :: String -> [Int] +readInstanceList str = + let r = reads str + in case r of + [(x,"")] -> x + _ -> error "readInstance: no parse" + +instance NFData a => NFData (Stream Identity a) where + {-# INLINE rnf #-} + rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs + o_1_space_generation :: Int -> [Benchmark] o_1_space_generation value = [ bgroup "generation" [ benchIO "fromList" $ sourceFromList value , benchIO "fromTuple" $ sourceFromTuple value + , benchIO "IsList.fromList" $ sourceIsList value + , benchIO "IsString.fromString" $ sourceIsString value + ] + ] + +o_n_heap_generation :: Int -> [Benchmark] +o_n_heap_generation value = + [ bgroup "buffered" + -- Buffers the output of show/read. + -- XXX can the outputs be streaming? Can we have special read/show + -- style type classes, readM/showM supporting streaming effects? + [ bench "readsPrec pure streams" $ + nf (readInstance . mkString) value + , bench "readsPrec Haskell lists" $ + nf (readInstanceList . mkListString) value ] ] @@ -1362,6 +1429,7 @@ benchmarks :: Int -> [(SpaceComplexity, Benchmark)] benchmarks size = -- Construction map (SpaceO_1,) (o_1_space_generation size) + ++ map (HeapO_n,) (o_n_heap_generation size) -- Elimination ++ map (SpaceO_1,) (o_1_space_elimination_folds size) ++ map (HeapO_n,) (o_n_heap_elimination_foldl size) From e20b2917c6ba23b2fda7febb8b6e577ebd4c62a1 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 15:27:47 +0530 Subject: [PATCH 02/15] Keep only elimination module benchmarks in Eliminate.hs --- .../Benchmark/Data/Stream/Eliminate.hs | 506 +----------------- .../Benchmark/Data/Stream/Generate.hs | 1 + .../Streamly/Benchmark/Data/Stream/Lift.hs | 16 + .../Benchmark/Data/Stream/Transform.hs | 24 + .../Streamly/Benchmark/Data/Stream/Type.hs | 425 ++++++++++++++- 5 files changed, 469 insertions(+), 503 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs b/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs index acf4eb83f5..2919f35078 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs @@ -6,11 +6,7 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE RankNTypes #-} - -{-# OPTIONS_GHC -Wno-orphans #-} #ifdef __HADDOCK_VERSION__ #undef INSPECTION @@ -24,13 +20,9 @@ module Stream.Eliminate (benchmarks) where import Control.Monad ((>=>)) -import Data.Functor ((<&>)) -import Control.DeepSeq (NFData(..)) import Data.Functor.Identity (Identity(..), runIdentity) import System.Random (randomRIO) -import qualified Data.Foldable as F -import qualified GHC.Exts as GHC import qualified Streamly.Internal.Data.Fold as Fold #ifdef INSPECTION @@ -39,6 +31,7 @@ import Test.Inspection #endif import Stream.Common hiding (benchIO) +import Stream.Type (benchIO, withStream, withPureStream) import Streamly.Internal.Data.Stream (Stream) import qualified Streamly.Internal.Data.Stream as S @@ -48,350 +41,6 @@ import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!), lookup, repeat, minimum, maximum, product, last, mapM_, init) import qualified Prelude -------------------------------------------------------------------------------- --- Helpers -------------------------------------------------------------------------------- - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomInt #-} -withRandomInt :: (Int -> b) -> IO b -withRandomInt f = randomRIO (1, 1 :: Int) <&> f - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = randomRIO (1, 1) >>= f . sourceUnfoldrM value - -{-# INLINE withPureStream #-} -withPureStream :: Int -> (Stream Identity Int -> b) -> IO b -withPureStream value f = randomRIO (1, 1) <&> (f . sourceUnfoldr value) - -------------------------------------------------------------------------------- --- Elimination -------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- --- Foldable Instance -------------------------------------------------------------------------------- - -instance NFData a => NFData (Stream Identity a) where - {-# INLINE rnf #-} - rnf xs = runIdentity $ S.fold (Fold.foldl' (\_ x -> rnf x) ()) xs - -{-# INLINE foldableFoldl' #-} -foldableFoldl' :: Int -> Int -> Int -foldableFoldl' value n = - F.foldl' (+) 0 (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableFoldl' -inspect $ 'foldableFoldl' `hasNoType` ''S.Step -#endif - -{-# INLINE foldableFoldrElem #-} -foldableFoldrElem :: Int -> Int -> Bool -foldableFoldrElem value n = - F.foldr (\x xs -> x == value || xs) - False - (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableFoldrElem -inspect $ 'foldableFoldrElem `hasNoType` ''S.Step -inspect $ 'foldableFoldrElem `hasNoType` ''Fold.Step -inspect $ 'foldableFoldrElem `hasNoType` ''SPEC -#endif - -{-# INLINE foldableSum #-} -foldableSum :: Int -> Int -> Int -foldableSum value n = - Prelude.sum (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableSum -inspect $ 'foldableSum `hasNoType` ''S.Step -inspect $ 'foldableSum `hasNoType` ''Fold.Step -inspect $ 'foldableSum `hasNoType` ''SPEC -#endif - -{-# INLINE foldableProduct #-} -foldableProduct :: Int -> Int -> Int -foldableProduct value n = - Prelude.product (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableProduct -inspect $ 'foldableProduct `hasNoType` ''S.Step -inspect $ 'foldableProduct `hasNoType` ''Fold.Step -inspect $ 'foldableProduct `hasNoType` ''SPEC -#endif - -{-# INLINE _foldableNull #-} -_foldableNull :: Int -> Int -> Bool -_foldableNull value n = - Prelude.null (sourceUnfoldr value n :: Stream Identity Int) - -{-# INLINE foldableElem #-} -foldableElem :: Int -> Int -> Bool -foldableElem value n = - value `Prelude.elem` (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableElem -inspect $ 'foldableElem `hasNoType` ''S.Step -inspect $ 'foldableElem `hasNoType` ''Fold.Step -inspect $ 'foldableElem `hasNoType` ''SPEC -#endif - -{-# INLINE foldableNotElem #-} -foldableNotElem :: Int -> Int -> Bool -foldableNotElem value n = - value `Prelude.notElem` (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableNotElem -inspect $ 'foldableNotElem `hasNoType` ''S.Step -inspect $ 'foldableNotElem `hasNoType` ''Fold.Step -inspect $ 'foldableNotElem `hasNoType` ''SPEC -#endif - -{-# INLINE foldableFind #-} -foldableFind :: Int -> Int -> Maybe Int -foldableFind value n = - F.find (== (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableFind -inspect $ 'foldableFind `hasNoType` ''S.Step -inspect $ 'foldableFind `hasNoType` ''Fold.Step -inspect $ 'foldableFind `hasNoType` ''SPEC -#endif - -{-# INLINE foldableAll #-} -foldableAll :: Int -> Int -> Bool -foldableAll value n = - Prelude.all (<= (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableAll -inspect $ 'foldableAll `hasNoType` ''S.Step -inspect $ 'foldableAll `hasNoType` ''Fold.Step -inspect $ 'foldableAll `hasNoType` ''SPEC -#endif - -{- HLINT ignore "Use any"-} -{-# INLINE foldableAny #-} -foldableAny :: Int -> Int -> Bool -foldableAny value n = - Prelude.any (> (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableAny -inspect $ 'foldableAny `hasNoType` ''S.Step -inspect $ 'foldableAny `hasNoType` ''Fold.Step -inspect $ 'foldableAny `hasNoType` ''SPEC -#endif - -{- HLINT ignore "Use all"-} -{-# INLINE foldableAnd #-} -foldableAnd :: Int -> Int -> Bool -foldableAnd value n = - Prelude.and $ fmap - (<= (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableAnd -inspect $ 'foldableAnd `hasNoType` ''S.Step -inspect $ 'foldableAnd `hasNoType` ''Fold.Step -inspect $ 'foldableAnd `hasNoType` ''SPEC -#endif - -{- HLINT ignore "Use any"-} -{-# INLINE foldableOr #-} -foldableOr :: Int -> Int -> Bool -foldableOr value n = - Prelude.or $ fmap - (> (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableOr -inspect $ 'foldableOr `hasNoType` ''S.Step -inspect $ 'foldableOr `hasNoType` ''Fold.Step -inspect $ 'foldableOr `hasNoType` ''SPEC -#endif - -{-# INLINE foldableLength #-} -foldableLength :: Int -> Int -> Int -foldableLength value n = - Prelude.length (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableLength -inspect $ 'foldableLength `hasNoType` ''S.Step -inspect $ 'foldableLength `hasNoType` ''Fold.Step -inspect $ 'foldableLength `hasNoType` ''SPEC -#endif - -{-# INLINE foldableMin #-} -foldableMin :: Int -> Int -> Int -foldableMin value n = - Prelude.minimum (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableMin -inspect $ 'foldableMin `hasNoType` ''S.Step -inspect $ 'foldableMin `hasNoType` ''Fold.Step -inspect $ 'foldableMin `hasNoType` ''SPEC -#endif - -{-# INLINE ordInstanceMin #-} -ordInstanceMin :: Int -> Int -> () -ordInstanceMin value n = - let src = sourceUnfoldr value n - in runIdentity $ drain $ min src src - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'ordInstanceMin -inspect $ 'ordInstanceMin `hasNoType` ''S.Step -inspect $ 'ordInstanceMin `hasNoType` ''Fold.Step -inspect $ 'ordInstanceMin `hasNoType` ''SPEC -#endif - -{-# INLINE foldableMax #-} -foldableMax :: Int -> Int -> Int -foldableMax value n = - Prelude.maximum (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableMax -inspect $ 'foldableMax `hasNoType` ''S.Step -inspect $ 'foldableMax `hasNoType` ''Fold.Step -inspect $ 'foldableMax `hasNoType` ''SPEC -#endif - -{-# INLINE foldableMinBy #-} -foldableMinBy :: Int -> Int -> Int -foldableMinBy value n = - F.minimumBy compare (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableMinBy -inspect $ 'foldableMinBy `hasNoType` ''S.Step -inspect $ 'foldableMinBy `hasNoType` ''Fold.Step -inspect $ 'foldableMinBy `hasNoType` ''SPEC -#endif - -{-# INLINE foldableListMinBy #-} -foldableListMinBy :: Int -> Int -> Int -foldableListMinBy value n = F.minimumBy compare [1..value+n] - --- Not inspection-tested: 'foldableListMinBy' folds a plain list, not a stream. - -{-# INLINE foldableMaxBy #-} -foldableMaxBy :: Int -> Int -> Int -foldableMaxBy value n = - F.maximumBy compare (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableMaxBy -inspect $ 'foldableMaxBy `hasNoType` ''S.Step -inspect $ 'foldableMaxBy `hasNoType` ''Fold.Step -inspect $ 'foldableMaxBy `hasNoType` ''SPEC -#endif - -{-# INLINE foldableToList #-} -foldableToList :: Int -> Int -> [Int] -foldableToList value n = - F.toList (sourceUnfoldr value n :: Stream Identity Int) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableToList -inspect $ 'foldableToList `hasNoType` ''S.Step -inspect $ 'foldableToList `hasNoType` ''Fold.Step -inspect $ 'foldableToList `hasNoType` ''SPEC -#endif - -{-# INLINE foldableMapM_ #-} -foldableMapM_ :: Int -> Int -> IO () -foldableMapM_ value n = - F.mapM_ (\_ -> return ()) (sourceUnfoldr value n :: Stream Identity Int) - --- 'foldableMapM_' is the only 'Foldable' benchmark that is polymorphic in the --- monad; the rest already bake a concrete 'Stream Identity' source and are --- inspected directly. -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableMapM_ -inspect $ 'foldableMapM_ `hasNoType` ''S.Step -inspect $ 'foldableMapM_ `hasNoType` ''Fold.Step -inspect $ 'foldableMapM_ `hasNoType` ''SPEC -#endif - -{-# INLINE foldableSequence_ #-} -foldableSequence_ :: Int -> Int -> IO () -foldableSequence_ value n = - F.sequence_ (sourceUnfoldrAction value n :: Stream Identity (IO Int)) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldableSequence_ -inspect $ 'foldableSequence_ `hasNoType` ''S.Step -inspect $ 'foldableSequence_ `hasNoType` ''Fold.Step -inspect $ 'foldableSequence_ `hasNoType` ''SPEC -#endif - -{-# INLINE _foldableMsum #-} -_foldableMsum :: Int -> Int -> IO Int -_foldableMsum value n = - F.msum (sourceUnfoldrAction value n :: Stream Identity (IO Int)) - -{-# INLINE showInstance #-} -showInstance :: Int -> IO String -showInstance value = withPureStream value show - -o_1_space_elimination_foldable :: Int -> [Benchmark] -o_1_space_elimination_foldable value = - [ bgroup "foldable" - -- Foldable instance - [ benchIO "foldl'" $ withRandomInt (foldableFoldl' value) - , benchIO "foldrElem" $ withRandomInt (foldableFoldrElem value) - -- , benchIO "null" $ withRandomInt (_foldableNull value) - , benchIO "elem" $ withRandomInt (foldableElem value) - , benchIO "length" $ withRandomInt (foldableLength value) - , benchIO "sum" $ withRandomInt (foldableSum value) - , benchIO "product" $ withRandomInt (foldableProduct value) - , benchIO "minimum" $ withRandomInt (foldableMin value) - , benchIO "min (ord)" $ withRandomInt (ordInstanceMin value) - , benchIO "maximum" $ withRandomInt (foldableMax value) - , benchIO "minimumBy" $ withRandomInt (foldableMinBy value) - , benchIO "maximumBy" $ withRandomInt (foldableMaxBy value) - , benchIO "minimumByList" $ withRandomInt (foldableListMinBy value) - , benchIO "length . toList" $ - withRandomInt (Prelude.length . foldableToList value) - , benchIO "notElem" $ withRandomInt (foldableNotElem value) - , benchIO "find" $ withRandomInt (foldableFind value) - , benchIO "all" $ withRandomInt (foldableAll value) - , benchIO "any" $ withRandomInt (foldableAny value) - , benchIO "and" $ withRandomInt (foldableAnd value) - , benchIO "or" $ withRandomInt (foldableOr value) - - -- Applicative and Traversable operations - -- TBD: traverse_ - , benchIO "mapM_" $ withRandomIntIO (foldableMapM_ value) - -- TBD: for_ - -- TBD: forM_ - , benchIO "sequence_" $ withRandomIntIO (foldableSequence_ value) - -- TBD: sequenceA_ - -- TBD: asum - -- XXX needs to be fixed, results are in ns - -- , benchIOSink1 "msum" (foldableMsum value) - ] - ] - ------------------------------------------------------------------------------- -- Stream folds ------------------------------------------------------------------------------- @@ -400,17 +49,6 @@ o_1_space_elimination_foldable value = -- Reductions ------------------------------------------------------------------------------- -{-# INLINE toNull #-} -toNull :: Int -> IO () -toNull value = withStream value S.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'toNull -inspect $ 'toNull `hasNoType` ''S.Step -inspect $ 'toNull `hasNoType` ''Fold.Step -inspect $ 'toNull `hasNoType` ''SPEC -#endif - {-# INLINE streamInit #-} streamInit :: Int -> IO () streamInit value = withStream value (S.init >=> Prelude.mapM_ S.drain) @@ -463,10 +101,6 @@ inspect $ hasNoTypeClasses 'foldl1'ReduceIdentity inspect $ 'foldl1'ReduceIdentity `hasNoType` ''S.Step #endif -{-# INLINE _head #-} -_head :: Monad m => Stream m Int -> m (Maybe Int) -_head = S.head - {-# INLINE elem #-} elem :: Int -> IO Bool elem value = withStream value (S.elem (value + 1)) @@ -566,28 +200,6 @@ inspect $ 'findM `hasNoType` ''Fold.Step inspect $ 'findM `hasNoType` ''SPEC #endif -{-# INLINE findIndex #-} -findIndex :: Int -> IO (Maybe Int) -findIndex value = withStream value (S.head . S.findIndices (== (value + 1))) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'findIndex -inspect $ 'findIndex `hasNoType` ''S.Step -inspect $ 'findIndex `hasNoType` ''Fold.Step -inspect $ 'findIndex `hasNoType` ''SPEC -#endif - -{-# INLINE elemIndex #-} -elemIndex :: Int -> IO (Maybe Int) -elemIndex value = withStream value (S.head . S.elemIndices (value + 1)) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'elemIndex -inspect $ 'elemIndex `hasNoType` ''S.Step -inspect $ 'elemIndex `hasNoType` ''Fold.Step -inspect $ 'elemIndex `hasNoType` ''SPEC -#endif - {-# INLINE maximum #-} maximum :: Int -> IO (Maybe Int) maximum value = withStream value S.maximum @@ -665,17 +277,6 @@ inspect $ 'the `hasNoType` ''Fold.Step inspect $ 'the `hasNoType` ''SPEC #endif -{-# INLINE drainN #-} -drainN :: Int -> IO () -drainN value = withStream value (S.fold (Fold.drainN value)) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'drainN -inspect $ 'drainN `hasNoType` ''S.Step -inspect $ 'drainN `hasNoType` ''Fold.Step -inspect $ 'drainN `hasNoType` ''SPEC -#endif - {-# INLINE indexOp #-} indexOp :: Int -> IO (Maybe Int) indexOp value = withStream value (S.!! value) @@ -699,23 +300,6 @@ inspect $ 'lookupNever `hasNoType` ''Fold.Step inspect $ 'lookupNever `hasNoType` ''SPEC #endif -{-# INLINE generalizeInner #-} -generalizeInner :: Int -> IO Int -generalizeInner value = - withPureStream value $ - runIdentity . S.fold Fold.length . S.generalizeInner - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'generalizeInner -inspect $ 'generalizeInner `hasNoType` ''S.Step -inspect $ 'generalizeInner `hasNoType` ''Fold.Step -inspect $ 'generalizeInner `hasNoType` ''SPEC -#endif - -{-# INLINE drainPure #-} -drainPure :: Int -> IO () -drainPure value = withPureStream value $ runIdentity . drain - o_1_space_elimination_folds :: Int -> [Benchmark] o_1_space_elimination_folds value = [ bgroup "elimination" @@ -736,14 +320,8 @@ o_1_space_elimination_folds value = -- deconstruction , benchIO "mapM_" $ mapM_ value , benchIO "last" $ streamLast value - , benchIO "length . generalizeInner" $ generalizeInner value - , benchIO "toNull" $ toNull value , benchIO "init" $ streamInit value - -- draining - , benchIO "drainN" $ drainN value - , benchIO "drain (pure)" $ drainPure value - -- this is too fast, causes all benchmarks reported in ns -- , benchIO "head" $ ... , benchIO "length" $ length value @@ -760,37 +338,12 @@ o_1_space_elimination_folds value = -- , benchIO "lookupFirst" $ ... , benchIO "lookupNever" $ lookupNever value , benchIO "(!!)" $ indexOp value - , benchIO "findIndex" $ findIndex value - , benchIO "elemIndex" $ elemIndex value - -- this is too fast, causes all benchmarks reported in ns - -- , benchIO "null" $ ... , benchIO "elem" $ elem value , benchIO "notElem" $ notElem value , benchIO "all" $ all value , benchIO "any" $ any value , benchIO "and" $ and value , benchIO "or" $ or value - - -- length is used to check for foldr/build fusion - , benchIO "length . IsList.toList" $ - withPureStream value (Prelude.length . GHC.toList) - ] - ] - --- For comparisons -{-# INLINE showInstanceList #-} -showInstanceList :: [Int] -> String -showInstanceList = show - -o_n_heap_elimination_buffered :: Int -> [Benchmark] -o_n_heap_elimination_buffered value = - [ bgroup "buffered" - -- Buffers the output of show/read. - -- XXX can the outputs be streaming? Can we have special read/show - -- style type classes, readM/showM supporting streaming effects? - [ bench "showsPrec Haskell lists" $ nf showInstanceList (mkList value) - -- XXX This is not o-1-space for GHC-8.10 - , benchIO "showsPrec pure streams" $ showInstance value ] ] @@ -829,52 +382,6 @@ o_n_space_elimination_toList value = -- Multi-stream folds ------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --- Multi-stream pure -------------------------------------------------------------------------------- - -{-# INLINE eqInstance #-} -eqInstance :: Int -> IO Bool -eqInstance value = withPureStream value $ \src -> src == src - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'eqInstance -inspect $ 'eqInstance `hasNoType` ''S.Step -inspect $ 'eqInstance `hasNoType` ''Fold.Step -inspect $ 'eqInstance `hasNoType` ''SPEC -#endif - -{-# INLINE eqInstanceNotEq #-} -eqInstanceNotEq :: Int -> IO Bool -eqInstanceNotEq value = withPureStream value $ \src -> src /= src - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'eqInstanceNotEq -inspect $ 'eqInstanceNotEq `hasNoType` ''S.Step -inspect $ 'eqInstanceNotEq `hasNoType` ''Fold.Step -inspect $ 'eqInstanceNotEq `hasNoType` ''SPEC -#endif - -{-# INLINE ordInstance #-} -ordInstance :: Int -> IO Bool -ordInstance value = withPureStream value $ \src -> src < src - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'ordInstance -inspect $ 'ordInstance `hasNoType` ''S.Step -inspect $ 'ordInstance `hasNoType` ''Fold.Step -inspect $ 'ordInstance `hasNoType` ''SPEC -#endif - -o_1_space_elimination_multi_stream_pure :: Int -> [Benchmark] -o_1_space_elimination_multi_stream_pure value = - [ bgroup "multi-stream-pure" - [ benchIO "==" $ eqInstance value - , benchIO "/=" $ eqInstanceNotEq value - , benchIO "<" $ ordInstance value - ] - ] - {-# INLINE isPrefixOf #-} isPrefixOf :: Int -> IO Bool isPrefixOf value = withStream value (\src -> S.isPrefixOf src src) @@ -929,13 +436,8 @@ o_1_space_elimination_multi_stream value = benchmarks :: Int -> [(SpaceComplexity, Benchmark)] benchmarks size = map (SpaceO_1,) (concat - [ o_1_space_elimination_foldable size - , o_1_space_elimination_folds size - , o_1_space_elimination_multi_stream_pure size + [ o_1_space_elimination_folds size , o_1_space_elimination_multi_stream size ]) - ++ map (HeapO_n,) ( - o_n_heap_elimination_buffered size - ++ o_n_heap_elimination_toList size) - ++ map (SpaceO_n,) ( - o_n_space_elimination_toList size) + ++ map (HeapO_n,) (o_n_heap_elimination_toList size) + ++ map (SpaceO_n,) (o_n_space_elimination_toList size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs index c9306b137b..bf760ae81a 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs @@ -23,6 +23,7 @@ module Stream.Generate (benchmarks) where #ifdef INSPECTION import GHC.Types (SPEC(..)) import Test.Inspection +import qualified Streamly.Internal.Data.Fold as Fold #endif import Control.DeepSeq (NFData(..)) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs b/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs index 245257c30a..225984680a 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs @@ -22,7 +22,9 @@ module Stream.Lift (benchmarks) where import Control.DeepSeq (NFData(..)) import Control.Monad.State.Strict (StateT, get, put) +import Data.Functor.Identity (Identity(..), runIdentity) import Stream.Common (sourceUnfoldr, sourceUnfoldrM) +import Stream.Type (withPureStream) import System.Random (randomRIO) import Streamly.Internal.Data.Stream (Stream) @@ -101,6 +103,19 @@ inspect $ 'withStateIO `hasNoType` ''Fold.Step inspect $ 'withStateIO `hasNoType` ''SPEC #endif +{-# INLINE generalizeInner #-} +generalizeInner :: Int -> IO Int +generalizeInner value = + withPureStream value $ + runIdentity . Stream.fold Fold.length . Stream.generalizeInner + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'generalizeInner +inspect $ 'generalizeInner `hasNoType` ''Step +inspect $ 'generalizeInner `hasNoType` ''Fold.Step +inspect $ 'generalizeInner `hasNoType` ''SPEC +#endif + {-# INLINE generalizeInnerIO #-} generalizeInnerIO :: Int -> IO Int generalizeInnerIO value = withRandomIntIO $ \n -> @@ -112,6 +127,7 @@ o_1_space_hoisting value = [ bgroup "hoisting" [ benchIO "evalState" $ evalStateTIO value , benchIO "withState" $ withStateIO value + , benchIO "length . generalizeInner" $ generalizeInner value , benchIO "generalizeInner" $ generalizeInnerIO value ] ] diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs index 3f4dc99de8..7cea6f2d58 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs @@ -790,6 +790,28 @@ inspect $ 'elemIndices4 `hasNoType` ''FL.Step inspect $ 'elemIndices4 `hasNoType` ''SPEC #endif +{-# INLINE findIndex #-} +findIndex :: Int -> IO (Maybe Int) +findIndex value = withStream value (Stream.head . Stream.findIndices (== (value + 1))) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'findIndex +inspect $ 'findIndex `hasNoType` ''Stream.Step +inspect $ 'findIndex `hasNoType` ''FL.Step +inspect $ 'findIndex `hasNoType` ''SPEC +#endif + +{-# INLINE elemIndex #-} +elemIndex :: Int -> IO (Maybe Int) +elemIndex value = withStream value (Stream.head . Stream.elemIndices (value + 1)) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'elemIndex +inspect $ 'elemIndex `hasNoType` ''Stream.Step +inspect $ 'elemIndex `hasNoType` ''FL.Step +inspect $ 'elemIndex `hasNoType` ''SPEC +#endif + {-# INLINE deleteBy #-} deleteBy :: MonadIO m => Int -> Int -> Stream m Int -> m () deleteBy value n = composeN n $ Stream.deleteBy (>=) (value + 1) @@ -934,6 +956,8 @@ o_1_space_filtering value = -- Searching (stateful map and filter) , benchIO "findIndices" $ findIndices1 value , benchIO "elemIndices" $ elemIndices1 value + , benchIO "findIndex" $ findIndex value + , benchIO "elemIndex" $ elemIndex value ] ] diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Type.hs b/benchmark/Streamly/Benchmark/Data/Stream/Type.hs index dcfe2ecb83..2a3597c695 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Type.hs @@ -29,6 +29,13 @@ module Stream.Type , checkStream , checkPair , result + , withPureStream + , withStream + , withDrain + , withDrainPure + , withRandomInt + , withRandomIntIO + , benchIO ) where #ifdef INSPECTION @@ -47,6 +54,7 @@ import Streamly.Internal.Data.Stream (Stream) import Streamly.Data.Unfold (Unfold) import System.Random (randomRIO) +import qualified Data.Foldable as F import qualified GHC.Exts as GHC import qualified Streamly.Internal.Data.Fold as FL @@ -79,6 +87,10 @@ withDrain f = withRandomIntIO $ \n -> drain (f n) withDrainPure :: (Int -> Stream Identity a) -> IO () withDrainPure f = withRandomIntIO $ \n -> return $! runIdentity $ drain (f n) +{-# INLINE withRandomInt #-} +withRandomInt :: (Int -> b) -> IO b +withRandomInt f = randomRIO (1, 1 :: Int) <&> f + {-# INLINE withStream #-} withStream :: Int -> (Stream IO Int -> IO b) -> IO b withStream value f = withRandomIntIO (f . sourceUnfoldrM value) @@ -170,6 +182,374 @@ instance NFData a => NFData (Stream Identity a) where {-# INLINE rnf #-} rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs +------------------------------------------------------------------------------- +-- Foldable Instance +------------------------------------------------------------------------------- + +{-# INLINE foldableFoldl' #-} +foldableFoldl' :: Int -> Int -> Int +foldableFoldl' value n = + F.foldl' (+) 0 (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableFoldl' +inspect $ 'foldableFoldl' `hasNoType` ''Stream.Step +#endif + +{-# INLINE foldableFoldrElem #-} +foldableFoldrElem :: Int -> Int -> Bool +foldableFoldrElem value n = + F.foldr (\x xs -> x == value || xs) + False + (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableFoldrElem +inspect $ 'foldableFoldrElem `hasNoType` ''Stream.Step +inspect $ 'foldableFoldrElem `hasNoType` ''Fold.Step +inspect $ 'foldableFoldrElem `hasNoType` ''SPEC +#endif + +{-# INLINE foldableSum #-} +foldableSum :: Int -> Int -> Int +foldableSum value n = + Prelude.sum (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableSum +inspect $ 'foldableSum `hasNoType` ''Stream.Step +inspect $ 'foldableSum `hasNoType` ''Fold.Step +inspect $ 'foldableSum `hasNoType` ''SPEC +#endif + +{-# INLINE foldableProduct #-} +foldableProduct :: Int -> Int -> Int +foldableProduct value n = + Prelude.product (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableProduct +inspect $ 'foldableProduct `hasNoType` ''Stream.Step +inspect $ 'foldableProduct `hasNoType` ''Fold.Step +inspect $ 'foldableProduct `hasNoType` ''SPEC +#endif + +{-# INLINE _foldableNull #-} +_foldableNull :: Int -> Int -> Bool +_foldableNull value n = + Prelude.null (sourceUnfoldr value n :: Stream Identity Int) + +{-# INLINE foldableElem #-} +foldableElem :: Int -> Int -> Bool +foldableElem value n = + value `Prelude.elem` (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableElem +inspect $ 'foldableElem `hasNoType` ''Stream.Step +inspect $ 'foldableElem `hasNoType` ''Fold.Step +inspect $ 'foldableElem `hasNoType` ''SPEC +#endif + +{-# INLINE foldableNotElem #-} +foldableNotElem :: Int -> Int -> Bool +foldableNotElem value n = + value `Prelude.notElem` (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableNotElem +inspect $ 'foldableNotElem `hasNoType` ''Stream.Step +inspect $ 'foldableNotElem `hasNoType` ''Fold.Step +inspect $ 'foldableNotElem `hasNoType` ''SPEC +#endif + +{-# INLINE foldableFind #-} +foldableFind :: Int -> Int -> Maybe Int +foldableFind value n = + F.find (== (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableFind +inspect $ 'foldableFind `hasNoType` ''Stream.Step +inspect $ 'foldableFind `hasNoType` ''Fold.Step +inspect $ 'foldableFind `hasNoType` ''SPEC +#endif + +{-# INLINE foldableAll #-} +foldableAll :: Int -> Int -> Bool +foldableAll value n = + Prelude.all (<= (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableAll +inspect $ 'foldableAll `hasNoType` ''Stream.Step +inspect $ 'foldableAll `hasNoType` ''Fold.Step +inspect $ 'foldableAll `hasNoType` ''SPEC +#endif + +{- HLINT ignore "Use any"-} +{-# INLINE foldableAny #-} +foldableAny :: Int -> Int -> Bool +foldableAny value n = + Prelude.any (> (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableAny +inspect $ 'foldableAny `hasNoType` ''Stream.Step +inspect $ 'foldableAny `hasNoType` ''Fold.Step +inspect $ 'foldableAny `hasNoType` ''SPEC +#endif + +{- HLINT ignore "Use all"-} +{-# INLINE foldableAnd #-} +foldableAnd :: Int -> Int -> Bool +foldableAnd value n = + Prelude.and $ fmap + (<= (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableAnd +inspect $ 'foldableAnd `hasNoType` ''Stream.Step +inspect $ 'foldableAnd `hasNoType` ''Fold.Step +inspect $ 'foldableAnd `hasNoType` ''SPEC +#endif + +{- HLINT ignore "Use any"-} +{-# INLINE foldableOr #-} +foldableOr :: Int -> Int -> Bool +foldableOr value n = + Prelude.or $ fmap + (> (value + 1)) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableOr +inspect $ 'foldableOr `hasNoType` ''Stream.Step +inspect $ 'foldableOr `hasNoType` ''Fold.Step +inspect $ 'foldableOr `hasNoType` ''SPEC +#endif + +{-# INLINE foldableLength #-} +foldableLength :: Int -> Int -> Int +foldableLength value n = + Prelude.length (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableLength +inspect $ 'foldableLength `hasNoType` ''Stream.Step +inspect $ 'foldableLength `hasNoType` ''Fold.Step +inspect $ 'foldableLength `hasNoType` ''SPEC +#endif + +{-# INLINE foldableMin #-} +foldableMin :: Int -> Int -> Int +foldableMin value n = + Prelude.minimum (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableMin +inspect $ 'foldableMin `hasNoType` ''Stream.Step +inspect $ 'foldableMin `hasNoType` ''Fold.Step +inspect $ 'foldableMin `hasNoType` ''SPEC +#endif + +{-# INLINE ordInstanceMin #-} +ordInstanceMin :: Int -> Int -> () +ordInstanceMin value n = + let src = sourceUnfoldr value n + in runIdentity $ drain $ min src src + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'ordInstanceMin +inspect $ 'ordInstanceMin `hasNoType` ''Stream.Step +inspect $ 'ordInstanceMin `hasNoType` ''Fold.Step +inspect $ 'ordInstanceMin `hasNoType` ''SPEC +#endif + +{-# INLINE foldableMax #-} +foldableMax :: Int -> Int -> Int +foldableMax value n = + Prelude.maximum (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableMax +inspect $ 'foldableMax `hasNoType` ''Stream.Step +inspect $ 'foldableMax `hasNoType` ''Fold.Step +inspect $ 'foldableMax `hasNoType` ''SPEC +#endif + +{-# INLINE foldableMinBy #-} +foldableMinBy :: Int -> Int -> Int +foldableMinBy value n = + F.minimumBy compare (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableMinBy +inspect $ 'foldableMinBy `hasNoType` ''Stream.Step +inspect $ 'foldableMinBy `hasNoType` ''Fold.Step +inspect $ 'foldableMinBy `hasNoType` ''SPEC +#endif + +{-# INLINE foldableListMinBy #-} +foldableListMinBy :: Int -> Int -> Int +foldableListMinBy value n = F.minimumBy compare [1..value+n] + +{-# INLINE foldableMaxBy #-} +foldableMaxBy :: Int -> Int -> Int +foldableMaxBy value n = + F.maximumBy compare (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableMaxBy +inspect $ 'foldableMaxBy `hasNoType` ''Stream.Step +inspect $ 'foldableMaxBy `hasNoType` ''Fold.Step +inspect $ 'foldableMaxBy `hasNoType` ''SPEC +#endif + +{-# INLINE foldableToList #-} +foldableToList :: Int -> Int -> [Int] +foldableToList value n = + F.toList (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableToList +inspect $ 'foldableToList `hasNoType` ''Stream.Step +inspect $ 'foldableToList `hasNoType` ''Fold.Step +inspect $ 'foldableToList `hasNoType` ''SPEC +#endif + +{-# INLINE foldableMapM_ #-} +foldableMapM_ :: Int -> Int -> IO () +foldableMapM_ value n = + F.mapM_ (\_ -> return ()) (sourceUnfoldr value n :: Stream Identity Int) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableMapM_ +inspect $ 'foldableMapM_ `hasNoType` ''Stream.Step +inspect $ 'foldableMapM_ `hasNoType` ''Fold.Step +inspect $ 'foldableMapM_ `hasNoType` ''SPEC +#endif + +{-# INLINE foldableSequence_ #-} +foldableSequence_ :: Int -> Int -> IO () +foldableSequence_ value n = + F.sequence_ (sourceUnfoldrAction value n :: Stream Identity (IO Int)) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldableSequence_ +inspect $ 'foldableSequence_ `hasNoType` ''Stream.Step +inspect $ 'foldableSequence_ `hasNoType` ''Fold.Step +inspect $ 'foldableSequence_ `hasNoType` ''SPEC +#endif + +{-# INLINE _foldableMsum #-} +_foldableMsum :: Int -> Int -> IO Int +_foldableMsum value n = + F.msum (sourceUnfoldrAction value n :: Stream Identity (IO Int)) + +o_1_space_elimination_foldable :: Int -> [Benchmark] +o_1_space_elimination_foldable value = + [ bgroup "foldable" + -- Foldable instance + [ benchIO "foldl'" $ withRandomInt (foldableFoldl' value) + , benchIO "foldrElem" $ withRandomInt (foldableFoldrElem value) + -- , benchIO "null" $ withRandomInt (_foldableNull value) + , benchIO "elem" $ withRandomInt (foldableElem value) + , benchIO "length" $ withRandomInt (foldableLength value) + , benchIO "sum" $ withRandomInt (foldableSum value) + , benchIO "product" $ withRandomInt (foldableProduct value) + , benchIO "minimum" $ withRandomInt (foldableMin value) + , benchIO "min (ord)" $ withRandomInt (ordInstanceMin value) + , benchIO "maximum" $ withRandomInt (foldableMax value) + , benchIO "minimumBy" $ withRandomInt (foldableMinBy value) + , benchIO "maximumBy" $ withRandomInt (foldableMaxBy value) + , benchIO "minimumByList" $ withRandomInt (foldableListMinBy value) + , benchIO "length . toList" $ + withRandomInt (Prelude.length . foldableToList value) + , benchIO "notElem" $ withRandomInt (foldableNotElem value) + , benchIO "find" $ withRandomInt (foldableFind value) + , benchIO "all" $ withRandomInt (foldableAll value) + , benchIO "any" $ withRandomInt (foldableAny value) + , benchIO "and" $ withRandomInt (foldableAnd value) + , benchIO "or" $ withRandomInt (foldableOr value) + + -- Applicative and Traversable operations + -- TBD: traverse_ + , benchIO "mapM_" $ withRandomIntIO (foldableMapM_ value) + -- TBD: for_ + -- TBD: forM_ + , benchIO "sequence_" $ withRandomIntIO (foldableSequence_ value) + -- TBD: sequenceA_ + -- TBD: asum + -- XXX needs to be fixed, results are in ns + -- , benchIOSink1 "msum" (foldableMsum value) + ] + ] + +------------------------------------------------------------------------------- +-- Show instance +------------------------------------------------------------------------------- + +{-# INLINE showInstance #-} +showInstance :: Int -> IO String +showInstance value = withPureStream value show + +{-# INLINE showInstanceList #-} +showInstanceList :: [Int] -> String +showInstanceList = show + +o_n_heap_elimination_show :: Int -> [Benchmark] +o_n_heap_elimination_show value = + [ bgroup "buffered" + -- Buffers the output of show/read. + -- XXX can the outputs be streaming? Can we have special read/show + -- style type classes, readM/showM supporting streaming effects? + [ bench "showsPrec Haskell lists" $ nf showInstanceList (mkList value) + -- XXX This is not o-1-space for GHC-8.10 + , benchIO "showsPrec pure streams" $ showInstance value + ] + ] + +------------------------------------------------------------------------------- +-- Eq and Ord instances +------------------------------------------------------------------------------- + +{-# INLINE eqInstance #-} +eqInstance :: Int -> IO Bool +eqInstance value = withPureStream value $ \src -> src == src + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'eqInstance +inspect $ 'eqInstance `hasNoType` ''Stream.Step +inspect $ 'eqInstance `hasNoType` ''Fold.Step +inspect $ 'eqInstance `hasNoType` ''SPEC +#endif + +{-# INLINE eqInstanceNotEq #-} +eqInstanceNotEq :: Int -> IO Bool +eqInstanceNotEq value = withPureStream value $ \src -> src /= src + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'eqInstanceNotEq +inspect $ 'eqInstanceNotEq `hasNoType` ''Stream.Step +inspect $ 'eqInstanceNotEq `hasNoType` ''Fold.Step +inspect $ 'eqInstanceNotEq `hasNoType` ''SPEC +#endif + +{-# INLINE ordInstance #-} +ordInstance :: Int -> IO Bool +ordInstance value = withPureStream value $ \src -> src < src + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'ordInstance +inspect $ 'ordInstance `hasNoType` ''Stream.Step +inspect $ 'ordInstance `hasNoType` ''Fold.Step +inspect $ 'ordInstance `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Generation +------------------------------------------------------------------------------- + o_1_space_generation :: Int -> [Benchmark] o_1_space_generation value = [ bgroup "generation" @@ -314,6 +694,32 @@ inspect $ hasNoTypeClasses 'foldlM'ReduceIdentity inspect $ 'foldlM'ReduceIdentity `hasNoType` ''S.Step #endif +{-# INLINE toNull #-} +toNull :: Int -> IO () +toNull value = withStream value S.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'toNull +inspect $ 'toNull `hasNoType` ''Stream.Step +inspect $ 'toNull `hasNoType` ''Fold.Step +inspect $ 'toNull `hasNoType` ''SPEC +#endif + +{-# INLINE drainPure #-} +drainPure :: Int -> IO () +drainPure value = withPureStream value $ runIdentity . drain + +{-# INLINE drainN #-} +drainN :: Int -> IO () +drainN value = withStream value (S.fold (Fold.drainN value)) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'drainN +inspect $ 'drainN `hasNoType` ''S.Step +inspect $ 'drainN `hasNoType` ''Fold.Step +inspect $ 'drainN `hasNoType` ''SPEC +#endif + o_1_space_elimination_folds :: Int -> [Benchmark] o_1_space_elimination_folds value = [ bgroup "elimination" @@ -341,9 +747,21 @@ o_1_space_elimination_folds value = ] ] + -- this is too fast, causes all benchmarks reported in ns + -- , benchIO "null" $ ... + -- deconstruction , benchIO "uncons" $ uncons value , benchIO "foldBreak" $ foldBreak value + + -- draining + , benchIO "toNull" $ toNull value + , benchIO "drainN" $ drainN value + , benchIO "drain (pure)" $ drainPure value + + -- length is used to check for foldr/build fusion + , benchIO "length . IsList.toList" $ + withPureStream value (Prelude.length . GHC.toList) ] ] @@ -442,7 +860,10 @@ inspect $ 'cmpByPure `hasNoType` ''Fold.Step o_1_space_elimination_multi_stream_pure :: Int -> [Benchmark] o_1_space_elimination_multi_stream_pure value = [ bgroup "multi-stream-pure" - [ benchIO "eqBy" $ eqByPure value + [ benchIO "==" $ eqInstance value + , benchIO "/=" $ eqInstanceNotEq value + , benchIO "<" $ ordInstance value + , benchIO "eqBy" $ eqByPure value , benchIO "cmpBy" $ cmpByPure value ] ] @@ -1431,8 +1852,10 @@ benchmarks size = map (SpaceO_1,) (o_1_space_generation size) ++ map (HeapO_n,) (o_n_heap_generation size) -- Elimination + ++ map (SpaceO_1,) (o_1_space_elimination_foldable size) ++ map (SpaceO_1,) (o_1_space_elimination_folds size) ++ map (HeapO_n,) (o_n_heap_elimination_foldl size) + ++ map (HeapO_n,) (o_n_heap_elimination_show size) ++ map (SpaceO_n,) (o_n_space_elimination_foldr size) ++ map (SpaceO_n,) (o_n_space_elimination_toList size) ++ map (SpaceO_1,) (o_1_space_elimination_multi_stream_pure size) From 4dfd0a4c1436ead645eb1e30d92457d19b80881e Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 15:59:54 +0530 Subject: [PATCH 03/15] Rename Stream benchmark modules mirroring source modules --- benchmark/Streamly/Benchmark/Data/Stream.hs | 8 ++++---- .../Benchmark/Data/Stream/{Expand.hs => Nesting.hs} | 2 +- .../Benchmark/Data/Stream/{Split.hs => ParseSplit.hs} | 2 +- .../Data/Stream/{SplitChunks.hs => ParseSplitChunks.hs} | 2 +- .../Benchmark/Data/Stream/{Reduce.hs => Transform2.hs} | 2 +- benchmark/streamly-benchmarks.cabal | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) rename benchmark/Streamly/Benchmark/Data/Stream/{Expand.hs => Nesting.hs} (99%) rename benchmark/Streamly/Benchmark/Data/Stream/{Split.hs => ParseSplit.hs} (99%) rename benchmark/Streamly/Benchmark/Data/Stream/{SplitChunks.hs => ParseSplitChunks.hs} (98%) rename benchmark/Streamly/Benchmark/Data/Stream/{Reduce.hs => Transform2.hs} (99%) diff --git a/benchmark/Streamly/Benchmark/Data/Stream.hs b/benchmark/Streamly/Benchmark/Data/Stream.hs index f2140b7b18..74ab6f524b 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream.hs @@ -21,12 +21,12 @@ import Streamly.Benchmark.Common.Handle (mkHandleBenchEnv) import qualified Stream.Eliminate as Elimination import qualified Stream.Exceptions as Exceptions -import qualified Stream.Expand as NestedStream +import qualified Stream.Nesting as NestedStream import qualified Stream.Generate as Generation import qualified Stream.Lift as Lift -import qualified Stream.Reduce as NestedFold -import qualified Stream.Split as Split -import qualified Stream.SplitChunks as SplitChunks +import qualified Stream.Transform2 as NestedFold +import qualified Stream.ParseSplit as Split +import qualified Stream.ParseSplitChunks as SplitChunks import qualified Stream.Transform as Transformation import qualified Stream.Type as Type diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Expand.hs b/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs similarity index 99% rename from benchmark/Streamly/Benchmark/Data/Stream/Expand.hs rename to benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs index 4a0e81ee56..34f56fccd9 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Expand.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs @@ -18,7 +18,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.Expand (benchmarks) where +module Stream.Nesting (benchmarks) where #ifdef INSPECTION import GHC.Types (SPEC(..)) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Split.hs b/benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs similarity index 99% rename from benchmark/Streamly/Benchmark/Data/Stream/Split.hs rename to benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs index c6a4f23442..5fcb8a3b0b 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Split.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs @@ -19,7 +19,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.Split (benchmarks) where +module Stream.ParseSplit (benchmarks) where import Data.Char (ord) import Data.Word (Word8) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/SplitChunks.hs b/benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs similarity index 98% rename from benchmark/Streamly/Benchmark/Data/Stream/SplitChunks.hs rename to benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs index 940562255c..8acadc5343 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/SplitChunks.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs @@ -19,7 +19,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.SplitChunks (benchmarks) where +module Stream.ParseSplitChunks (benchmarks) where import System.IO (Handle) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs similarity index 99% rename from benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs rename to benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs index 76e15f43e2..56ffc7549f 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs @@ -20,7 +20,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.Reduce (benchmarks) where +module Stream.Transform2 (benchmarks) where #ifdef INSPECTION import GHC.Types (SPEC(..)) diff --git a/benchmark/streamly-benchmarks.cabal b/benchmark/streamly-benchmarks.cabal index c786dbed9d..6239b40e2c 100644 --- a/benchmark/streamly-benchmarks.cabal +++ b/benchmark/streamly-benchmarks.cabal @@ -438,13 +438,13 @@ benchmark Data.Stream Stream.Common Stream.Eliminate Stream.Exceptions - Stream.Expand + Stream.Nesting Stream.Generate Stream.Lift - Stream.Reduce - Stream.Split + Stream.Transform2 + Stream.ParseSplit -- XXX uses lot of memory - Stream.SplitChunks + Stream.ParseSplitChunks Stream.Transform Stream.Type if flag(limit-build-mem) From 228d04d6403d81e9cf79a76eddb9790eb54aace5 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 16:43:42 +0530 Subject: [PATCH 04/15] Move misclassified benchmarks out of Stream.Transform2 --- benchmark/Streamly/Benchmark/Data/Stream.hs | 2 + .../Benchmark/Data/Stream/Eliminate.hs | 49 +++++- .../Streamly/Benchmark/Data/Stream/Parse.hs | 162 ++++++++++++++++++ .../Benchmark/Data/Stream/Transform2.hs | 136 +-------------- benchmark/streamly-benchmarks.cabal | 1 + 5 files changed, 213 insertions(+), 137 deletions(-) create mode 100644 benchmark/Streamly/Benchmark/Data/Stream/Parse.hs diff --git a/benchmark/Streamly/Benchmark/Data/Stream.hs b/benchmark/Streamly/Benchmark/Data/Stream.hs index 74ab6f524b..7c0431239c 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream.hs @@ -24,6 +24,7 @@ import qualified Stream.Exceptions as Exceptions import qualified Stream.Nesting as NestedStream import qualified Stream.Generate as Generation import qualified Stream.Lift as Lift +import qualified Stream.Parse as Parse import qualified Stream.Transform2 as NestedFold import qualified Stream.ParseSplit as Split import qualified Stream.ParseSplitChunks as SplitChunks @@ -64,6 +65,7 @@ main = do [ Generation.benchmarks size , Elimination.benchmarks size , Exceptions.benchmarks env size + , Parse.benchmarks size , Split.benchmarks env , SplitChunks.benchmarks env , Transformation.benchmarks size diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs b/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs index 2919f35078..096f142292 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs @@ -19,8 +19,9 @@ module Stream.Eliminate (benchmarks) where -import Control.Monad ((>=>)) +import Control.Monad ((>=>), when) import Data.Functor.Identity (Identity(..), runIdentity) +import Data.Maybe (isJust) import System.Random (randomRIO) import qualified Streamly.Internal.Data.Fold as Fold @@ -38,7 +39,7 @@ import qualified Streamly.Internal.Data.Stream as S import Test.Tasty.Bench import Streamly.Benchmark.Common import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!), - lookup, repeat, minimum, maximum, product, last, mapM_, init) + lookup, repeat, minimum, maximum, product, last, mapM_, init, tail) import qualified Prelude ------------------------------------------------------------------------------- @@ -426,6 +427,49 @@ o_1_space_elimination_multi_stream value = ] ] +------------------------------------------------------------------------------- +-- Iterating using tail +------------------------------------------------------------------------------- + +{-# INLINE tail #-} +tail :: Int -> IO () +tail value = withStream value go + where go s = S.tail s >>= Prelude.mapM_ go + +{-# INLINE nullHeadTail #-} +nullHeadTail :: Int -> IO () +nullHeadTail value = withStream value go + where + go s = do + r <- S.null s + when (not r) $ do + _ <- S.head s + S.tail s >>= Prelude.mapM_ go + +nullTail :: Int -> IO () +nullTail value = withStream value go + where + go s = do + r <- S.null s + when (not r) $ S.tail s >>= Prelude.mapM_ go + +headTail :: Int -> IO () +headTail value = withStream value go + where + go s = do + h <- S.head s + when (isJust h) $ S.tail s >>= Prelude.mapM_ go + +o_n_stack_iterated :: Int -> [Benchmark] +o_n_stack_iterated value = + [ bgroup "iterated" + [ benchIO "tail" $ tail value + , benchIO "nullTail" $ nullTail value + , benchIO "headTail" $ headTail value + , benchIO "nullHeadTail" $ nullHeadTail value + ] + ] + ------------------------------------------------------------------------------- -- Main ------------------------------------------------------------------------------- @@ -441,3 +485,4 @@ benchmarks size = ]) ++ map (HeapO_n,) (o_n_heap_elimination_toList size) ++ map (SpaceO_n,) (o_n_space_elimination_toList size) + ++ map (StackO_n,) (o_n_stack_iterated size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs new file mode 100644 index 0000000000..7e50e2bd91 --- /dev/null +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs @@ -0,0 +1,162 @@ +-- | +-- Module : Stream.Parse +-- Copyright : (c) 2018 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com + +{-# LANGUAGE CPP #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE ScopedTypeVariables #-} + +#ifdef __HADDOCK_VERSION__ +#undef INSPECTION +#endif + +#ifdef INSPECTION +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} +#endif + +module Stream.Parse (benchmarks) where + +#ifdef INSPECTION +import GHC.Types (SPEC(..)) +import Test.Inspection +#endif + +import Control.DeepSeq (NFData(..)) +import Data.Monoid (Sum(..)) +import Streamly.Internal.Data.Stream (Stream) +import System.Random (randomRIO) + +import qualified Stream.Common as Common +import qualified Streamly.Internal.Data.Fold as FL +import qualified Streamly.Internal.Data.Stream as S + +import Test.Tasty.Bench +import Streamly.Benchmark.Common +import Stream.Common hiding (benchIO) + +------------------------------------------------------------------------------- +-- Helpers +------------------------------------------------------------------------------- + +{-# INLINE benchIO #-} +benchIO :: NFData b => String -> IO b -> Benchmark +benchIO name = bench name . nfIO + +{-# INLINE withRandomIntIO #-} +withRandomIntIO :: (Int -> IO b) -> IO b +withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f + +{-# INLINE withStream #-} +withStream :: Int -> (Stream IO Int -> IO b) -> IO b +withStream value f = withRandomIntIO (f . sourceUnfoldrM value) + +------------------------------------------------------------------------------- +-- Grouping transformations +------------------------------------------------------------------------------- + +{-# INLINE groups #-} +groups :: Int -> IO () +groups value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groups +-- inspect $ 'groups `hasNoType` ''S.Step +inspect $ 'groups `hasNoType` ''FL.Step +inspect $ 'groups `hasNoType` ''SPEC +#endif + +{-# INLINE groupsWhileLT #-} +groupsWhileLT :: Int -> IO () +groupsWhileLT value = withStream value $ Common.drain . S.groupsWhile (<) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsWhileLT +-- inspect $ 'groupsWhileLT `hasNoType` ''S.Step +inspect $ 'groupsWhileLT `hasNoType` ''FL.Step +inspect $ 'groupsWhileLT `hasNoType` ''SPEC +#endif + +{-# INLINE groupsWhileEq #-} +groupsWhileEq :: Int -> IO () +groupsWhileEq value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsWhileEq +-- inspect $ 'groupsWhileEq `hasNoType` ''S.Step +inspect $ 'groupsWhileEq `hasNoType` ''FL.Step +inspect $ 'groupsWhileEq `hasNoType` ''SPEC +#endif + +{-# INLINE groupsByRollingLT #-} +groupsByRollingLT :: Int -> IO () +groupsByRollingLT value = withStream value $ Common.drain . S.groupsRollingBy (<) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsByRollingLT +inspect $ 'groupsByRollingLT `hasNoType` ''S.Step +-- inspect $ 'groupsByRollingLT `hasNoType` ''S.GroupByState +inspect $ 'groupsByRollingLT `hasNoType` ''FL.Step +inspect $ 'groupsByRollingLT `hasNoType` ''SPEC +#endif + +{-# INLINE groupsByRollingEq #-} +groupsByRollingEq :: Int -> IO () +groupsByRollingEq value = withStream value $ Common.drain . S.groupsRollingBy (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsByRollingEq +inspect $ 'groupsByRollingEq `hasNoType` ''S.Step +-- inspect $ 'groupsByRollingEq `hasNoType` ''S.GroupByState +inspect $ 'groupsByRollingEq `hasNoType` ''FL.Step +inspect $ 'groupsByRollingEq `hasNoType` ''SPEC +#endif + +{-# INLINE foldIterateM #-} +foldIterateM :: Int -> IO () +foldIterateM value = + withStream value $ + Common.drain + . fmap getSum + . S.foldIterateM + (return . FL.take 2 . FL.sconcat) (return (Sum 0)) + . fmap Sum + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldIterateM +inspect $ 'foldIterateM `hasNoType` ''S.Step +inspect $ 'foldIterateM `hasNoType` ''S.FIterState +inspect $ 'foldIterateM `hasNoType` ''FL.Step +inspect $ 'foldIterateM `hasNoType` ''SPEC +#endif + +o_1_space_grouping :: Int -> [Benchmark] +o_1_space_grouping value = + -- Buffering operations using heap proportional to group/window sizes. + [ bgroup "grouping" + [ + benchIO "groups" $ groups value + , benchIO "groupsWhileLT" $ groupsWhileLT value + , benchIO "groupsWhileEq" $ groupsWhileEq value + , benchIO "groupsByRollingLT" $ groupsByRollingLT value + , benchIO "groupsByRollingEq" $ groupsByRollingEq value + + -- XXX parseMany/parseIterate benchmarks are in the Parser/ParserD + -- modules we can bring those here. chunksOf benchmarks are in + -- Parser/ParserD/Array.Stream/FileSystem.Handle. + , benchIO "foldIterateM" $ foldIterateM value + ] + ] + +------------------------------------------------------------------------------- +-- Main +------------------------------------------------------------------------------- + +-- In addition to gauge options, the number of elements in the stream can be +-- passed using the --stream-size option. +-- +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = + map (SpaceO_1,) (o_1_space_grouping size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs index 56ffc7549f..bad8bd3cd7 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs @@ -28,10 +28,7 @@ import Test.Inspection #endif import Control.DeepSeq (NFData(..)) -import Control.Monad (when) import Control.Monad.IO.Class (MonadIO(..)) -import Data.Maybe (isJust) -import Data.Monoid (Sum(..)) import GHC.Generics (Generic) import Streamly.Internal.Data.Stream (Stream) import System.Random (randomRIO) @@ -76,103 +73,6 @@ withStream value f = withRandomIntIO (f . sourceUnfoldrM value) benchIO :: NFData b => String -> IO b -> Benchmark benchIO name = bench name . nfIO -------------------------------------------------------------------------------- --- Grouping transformations -------------------------------------------------------------------------------- - -{-# INLINE groups #-} -groups :: Int -> IO () -groups value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groups --- inspect $ 'groups `hasNoType` ''S.Step -inspect $ 'groups `hasNoType` ''FL.Step -inspect $ 'groups `hasNoType` ''SPEC -#endif - -{-# INLINE groupsWhileLT #-} -groupsWhileLT :: Int -> IO () -groupsWhileLT value = withStream value $ Common.drain . S.groupsWhile (<) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsWhileLT --- inspect $ 'groupsWhileLT `hasNoType` ''S.Step -inspect $ 'groupsWhileLT `hasNoType` ''FL.Step -inspect $ 'groupsWhileLT `hasNoType` ''SPEC -#endif - -{-# INLINE groupsWhileEq #-} -groupsWhileEq :: Int -> IO () -groupsWhileEq value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsWhileEq --- inspect $ 'groupsWhileEq `hasNoType` ''S.Step -inspect $ 'groupsWhileEq `hasNoType` ''FL.Step -inspect $ 'groupsWhileEq `hasNoType` ''SPEC -#endif - -{-# INLINE groupsByRollingLT #-} -groupsByRollingLT :: Int -> IO () -groupsByRollingLT value = withStream value $ Common.drain . S.groupsRollingBy (<) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsByRollingLT -inspect $ 'groupsByRollingLT `hasNoType` ''S.Step --- inspect $ 'groupsByRollingLT `hasNoType` ''S.GroupByState -inspect $ 'groupsByRollingLT `hasNoType` ''FL.Step -inspect $ 'groupsByRollingLT `hasNoType` ''SPEC -#endif - -{-# INLINE groupsByRollingEq #-} -groupsByRollingEq :: Int -> IO () -groupsByRollingEq value = withStream value $ Common.drain . S.groupsRollingBy (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsByRollingEq -inspect $ 'groupsByRollingEq `hasNoType` ''S.Step --- inspect $ 'groupsByRollingEq `hasNoType` ''S.GroupByState -inspect $ 'groupsByRollingEq `hasNoType` ''FL.Step -inspect $ 'groupsByRollingEq `hasNoType` ''SPEC -#endif - -{-# INLINE foldIterateM #-} -foldIterateM :: Int -> IO () -foldIterateM value = - withStream value $ - Common.drain - . fmap getSum - . S.foldIterateM - (return . FL.take 2 . FL.sconcat) (return (Sum 0)) - . fmap Sum - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldIterateM -inspect $ 'foldIterateM `hasNoType` ''S.Step -inspect $ 'foldIterateM `hasNoType` ''S.FIterState -inspect $ 'foldIterateM `hasNoType` ''FL.Step -inspect $ 'foldIterateM `hasNoType` ''SPEC -#endif - -o_1_space_grouping :: Int -> [Benchmark] -o_1_space_grouping value = - -- Buffering operations using heap proportional to group/window sizes. - [ bgroup "grouping" - [ - benchIO "groups" $ groups value - , benchIO "groupsWhileLT" $ groupsWhileLT value - , benchIO "groupsWhileEq" $ groupsWhileEq value - , benchIO "groupsByRollingLT" $ groupsByRollingLT value - , benchIO "groupsByRollingEq" $ groupsByRollingEq value - - -- XXX parseMany/parseIterate benchmarks are in the Parser/ParserD - -- modules we can bring those here. chunksOf benchmarks are in - -- Parser/ParserD/Array.Stream/FileSystem.Handle. - , benchIO "foldIterateM" $ foldIterateM value - ] - ] - ------------------------------------------------------------------------------- -- Size conserving transformations (reordering, buffering, etc.) ------------------------------------------------------------------------------- @@ -773,35 +673,6 @@ iterateDropWhileFalse value iterCount = withRandomIntIO $ Common.drain . iterateSource (S.dropWhile (> (value + 1))) (value `div` iterCount) iterCount -{-# INLINE tail #-} -tail :: Int -> IO () -tail value = withStream value go - where go s = S.tail s >>= mapM_ go - -{-# INLINE nullHeadTail #-} -nullHeadTail :: Int -> IO () -nullHeadTail value = withStream value go - where - go s = do - r <- S.null s - when (not r) $ do - _ <- S.head s - S.tail s >>= mapM_ go - -nullTail :: Int -> IO () -nullTail value = withStream value go - where - go s = do - r <- S.null s - when (not r) $ S.tail s >>= mapM_ go - -headTail :: Int -> IO () -headTail value = withStream value go - where - go s = do - h <- S.head s - when (isJust h) $ S.tail s >>= mapM_ go - o_n_stack_iterated :: Int -> [Benchmark] o_n_stack_iterated value = [ bgroup "iterated" @@ -813,10 +684,6 @@ o_n_stack_iterated value = , benchIO "dropOne (n/10 x 10)" $ iterateDropOne value 10 , benchIO "dropWhileTrue (n/10 x 10)" $ iterateDropWhileTrue value 10 , benchIO "dropWhileFalse (n/10 x 10)" $ iterateDropWhileFalse value 10 - , benchIO "tail" $ tail value - , benchIO "nullTail" $ nullTail value - , benchIO "headTail" $ headTail value - , benchIO "nullHeadTail" $ nullHeadTail value ] ] @@ -1067,8 +934,7 @@ o_1_space_scansX4 value = benchmarks :: Int -> [(SpaceComplexity, Benchmark)] benchmarks size = map (SpaceO_1,) (Prelude.concat - [ o_1_space_grouping size - , o_1_space_transformations_mixed size + [ o_1_space_transformations_mixed size , o_1_space_transformations_mixedX2 size , o_1_space_transformations_mixedX4 size diff --git a/benchmark/streamly-benchmarks.cabal b/benchmark/streamly-benchmarks.cabal index 6239b40e2c..9f1393b36f 100644 --- a/benchmark/streamly-benchmarks.cabal +++ b/benchmark/streamly-benchmarks.cabal @@ -441,6 +441,7 @@ benchmark Data.Stream Stream.Nesting Stream.Generate Stream.Lift + Stream.Parse Stream.Transform2 Stream.ParseSplit -- XXX uses lot of memory From 8de4ce4d6541df922104174bd639df2aab38f879 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 17:13:27 +0530 Subject: [PATCH 05/15] Organize the Parse and Transform submodules in Stream benchmarks --- benchmark/Streamly/Benchmark/Data/Stream.hs | 8 +- .../Streamly/Benchmark/Data/Stream/Parse.hs | 161 +-- .../Benchmark/Data/Stream/Parse/Group.hs | 159 +++ .../Stream/{ParseSplit.hs => Parse/Split.hs} | 2 +- .../SplitChunks.hs} | 2 +- .../Benchmark/Data/Stream/Transform.hs | 1212 +--------------- .../Benchmark/Data/Stream/Transform/Basic.hs | 1220 +++++++++++++++++ .../{Transform2.hs => Transform/Composed.hs} | 4 +- benchmark/streamly-benchmarks.cabal | 8 +- 9 files changed, 1404 insertions(+), 1372 deletions(-) create mode 100644 benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs rename benchmark/Streamly/Benchmark/Data/Stream/{ParseSplit.hs => Parse/Split.hs} (99%) rename benchmark/Streamly/Benchmark/Data/Stream/{ParseSplitChunks.hs => Parse/SplitChunks.hs} (98%) create mode 100644 benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs rename benchmark/Streamly/Benchmark/Data/Stream/{Transform2.hs => Transform/Composed.hs} (99%) diff --git a/benchmark/Streamly/Benchmark/Data/Stream.hs b/benchmark/Streamly/Benchmark/Data/Stream.hs index 7c0431239c..f0bb96b8e4 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream.hs @@ -25,9 +25,6 @@ import qualified Stream.Nesting as NestedStream import qualified Stream.Generate as Generation import qualified Stream.Lift as Lift import qualified Stream.Parse as Parse -import qualified Stream.Transform2 as NestedFold -import qualified Stream.ParseSplit as Split -import qualified Stream.ParseSplitChunks as SplitChunks import qualified Stream.Transform as Transformation import qualified Stream.Type as Type @@ -65,11 +62,8 @@ main = do [ Generation.benchmarks size , Elimination.benchmarks size , Exceptions.benchmarks env size - , Parse.benchmarks size - , Split.benchmarks env - , SplitChunks.benchmarks env + , Parse.benchmarks size env , Transformation.benchmarks size - , NestedFold.benchmarks size , Lift.benchmarks size , NestedStream.benchmarks size , Type.benchmarks size diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs index 7e50e2bd91..703712d356 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse.hs @@ -4,159 +4,18 @@ -- License : BSD-3-Clause -- Maintainer : streamly@composewell.com -{-# LANGUAGE CPP #-} -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE ScopedTypeVariables #-} - -#ifdef __HADDOCK_VERSION__ -#undef INSPECTION -#endif - -#ifdef INSPECTION -{-# LANGUAGE TemplateHaskell #-} -{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} -#endif - module Stream.Parse (benchmarks) where -#ifdef INSPECTION -import GHC.Types (SPEC(..)) -import Test.Inspection -#endif - -import Control.DeepSeq (NFData(..)) -import Data.Monoid (Sum(..)) -import Streamly.Internal.Data.Stream (Stream) -import System.Random (randomRIO) +import qualified Stream.Parse.Group as Group +import qualified Stream.Parse.Split as Split +import qualified Stream.Parse.SplitChunks as SplitChunks -import qualified Stream.Common as Common -import qualified Streamly.Internal.Data.Fold as FL -import qualified Streamly.Internal.Data.Stream as S - -import Test.Tasty.Bench +import Test.Tasty.Bench (Benchmark) import Streamly.Benchmark.Common -import Stream.Common hiding (benchIO) - -------------------------------------------------------------------------------- --- Helpers -------------------------------------------------------------------------------- - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = withRandomIntIO (f . sourceUnfoldrM value) - -------------------------------------------------------------------------------- --- Grouping transformations -------------------------------------------------------------------------------- - -{-# INLINE groups #-} -groups :: Int -> IO () -groups value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groups --- inspect $ 'groups `hasNoType` ''S.Step -inspect $ 'groups `hasNoType` ''FL.Step -inspect $ 'groups `hasNoType` ''SPEC -#endif - -{-# INLINE groupsWhileLT #-} -groupsWhileLT :: Int -> IO () -groupsWhileLT value = withStream value $ Common.drain . S.groupsWhile (<) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsWhileLT --- inspect $ 'groupsWhileLT `hasNoType` ''S.Step -inspect $ 'groupsWhileLT `hasNoType` ''FL.Step -inspect $ 'groupsWhileLT `hasNoType` ''SPEC -#endif - -{-# INLINE groupsWhileEq #-} -groupsWhileEq :: Int -> IO () -groupsWhileEq value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsWhileEq --- inspect $ 'groupsWhileEq `hasNoType` ''S.Step -inspect $ 'groupsWhileEq `hasNoType` ''FL.Step -inspect $ 'groupsWhileEq `hasNoType` ''SPEC -#endif - -{-# INLINE groupsByRollingLT #-} -groupsByRollingLT :: Int -> IO () -groupsByRollingLT value = withStream value $ Common.drain . S.groupsRollingBy (<) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsByRollingLT -inspect $ 'groupsByRollingLT `hasNoType` ''S.Step --- inspect $ 'groupsByRollingLT `hasNoType` ''S.GroupByState -inspect $ 'groupsByRollingLT `hasNoType` ''FL.Step -inspect $ 'groupsByRollingLT `hasNoType` ''SPEC -#endif - -{-# INLINE groupsByRollingEq #-} -groupsByRollingEq :: Int -> IO () -groupsByRollingEq value = withStream value $ Common.drain . S.groupsRollingBy (==) FL.drain - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'groupsByRollingEq -inspect $ 'groupsByRollingEq `hasNoType` ''S.Step --- inspect $ 'groupsByRollingEq `hasNoType` ''S.GroupByState -inspect $ 'groupsByRollingEq `hasNoType` ''FL.Step -inspect $ 'groupsByRollingEq `hasNoType` ''SPEC -#endif - -{-# INLINE foldIterateM #-} -foldIterateM :: Int -> IO () -foldIterateM value = - withStream value $ - Common.drain - . fmap getSum - . S.foldIterateM - (return . FL.take 2 . FL.sconcat) (return (Sum 0)) - . fmap Sum - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'foldIterateM -inspect $ 'foldIterateM `hasNoType` ''S.Step -inspect $ 'foldIterateM `hasNoType` ''S.FIterState -inspect $ 'foldIterateM `hasNoType` ''FL.Step -inspect $ 'foldIterateM `hasNoType` ''SPEC -#endif - -o_1_space_grouping :: Int -> [Benchmark] -o_1_space_grouping value = - -- Buffering operations using heap proportional to group/window sizes. - [ bgroup "grouping" - [ - benchIO "groups" $ groups value - , benchIO "groupsWhileLT" $ groupsWhileLT value - , benchIO "groupsWhileEq" $ groupsWhileEq value - , benchIO "groupsByRollingLT" $ groupsByRollingLT value - , benchIO "groupsByRollingEq" $ groupsByRollingEq value - - -- XXX parseMany/parseIterate benchmarks are in the Parser/ParserD - -- modules we can bring those here. chunksOf benchmarks are in - -- Parser/ParserD/Array.Stream/FileSystem.Handle. - , benchIO "foldIterateM" $ foldIterateM value - ] - ] - -------------------------------------------------------------------------------- --- Main -------------------------------------------------------------------------------- +import Streamly.Benchmark.Common.Handle (BenchEnv) --- In addition to gauge options, the number of elements in the stream can be --- passed using the --stream-size option. --- -benchmarks :: Int -> [(SpaceComplexity, Benchmark)] -benchmarks size = - map (SpaceO_1,) (o_1_space_grouping size) +benchmarks :: Int -> BenchEnv -> [(SpaceComplexity, Benchmark)] +benchmarks size env = + Group.benchmarks size + ++ Split.benchmarks env + ++ SplitChunks.benchmarks env diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs new file mode 100644 index 0000000000..7563366d10 --- /dev/null +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs @@ -0,0 +1,159 @@ +-- | +-- Module : Stream.Parse +-- Copyright : (c) 2018 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com + +{-# LANGUAGE CPP #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE ScopedTypeVariables #-} + +#ifdef __HADDOCK_VERSION__ +#undef INSPECTION +#endif + +#ifdef INSPECTION +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} +#endif + +module Stream.Parse.Group (benchmarks) where + +#ifdef INSPECTION +import GHC.Types (SPEC(..)) +import Test.Inspection +#endif + +import Control.DeepSeq (NFData(..)) +import Data.Monoid (Sum(..)) +import Streamly.Internal.Data.Stream (Stream) +import System.Random (randomRIO) + +import qualified Stream.Common as Common +import qualified Streamly.Internal.Data.Fold as FL +import qualified Streamly.Internal.Data.Stream as S + +import Test.Tasty.Bench +import Streamly.Benchmark.Common +import Stream.Common hiding (benchIO) + +------------------------------------------------------------------------------- +-- Helpers +------------------------------------------------------------------------------- + +{-# INLINE benchIO #-} +benchIO :: NFData b => String -> IO b -> Benchmark +benchIO name = bench name . nfIO + +{-# INLINE withRandomIntIO #-} +withRandomIntIO :: (Int -> IO b) -> IO b +withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f + +{-# INLINE withStream #-} +withStream :: Int -> (Stream IO Int -> IO b) -> IO b +withStream value f = withRandomIntIO (f . sourceUnfoldrM value) + +------------------------------------------------------------------------------- +-- Grouping transformations +------------------------------------------------------------------------------- + +{-# INLINE groups #-} +groups :: Int -> IO () +groups value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groups +-- inspect $ 'groups `hasNoType` ''S.Step +inspect $ 'groups `hasNoType` ''FL.Step +inspect $ 'groups `hasNoType` ''SPEC +#endif + +{-# INLINE groupsWhileLT #-} +groupsWhileLT :: Int -> IO () +groupsWhileLT value = withStream value $ Common.drain . S.groupsWhile (<) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsWhileLT +-- inspect $ 'groupsWhileLT `hasNoType` ''S.Step +inspect $ 'groupsWhileLT `hasNoType` ''FL.Step +inspect $ 'groupsWhileLT `hasNoType` ''SPEC +#endif + +{-# INLINE groupsWhileEq #-} +groupsWhileEq :: Int -> IO () +groupsWhileEq value = withStream value $ Common.drain . S.groupsWhile (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsWhileEq +-- inspect $ 'groupsWhileEq `hasNoType` ''S.Step +inspect $ 'groupsWhileEq `hasNoType` ''FL.Step +inspect $ 'groupsWhileEq `hasNoType` ''SPEC +#endif + +{-# INLINE groupsByRollingLT #-} +groupsByRollingLT :: Int -> IO () +groupsByRollingLT value = withStream value $ Common.drain . S.groupsRollingBy (<) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsByRollingLT +inspect $ 'groupsByRollingLT `hasNoType` ''S.Step +-- inspect $ 'groupsByRollingLT `hasNoType` ''S.GroupByState +inspect $ 'groupsByRollingLT `hasNoType` ''FL.Step +inspect $ 'groupsByRollingLT `hasNoType` ''SPEC +#endif + +{-# INLINE groupsByRollingEq #-} +groupsByRollingEq :: Int -> IO () +groupsByRollingEq value = withStream value $ Common.drain . S.groupsRollingBy (==) FL.drain + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'groupsByRollingEq +inspect $ 'groupsByRollingEq `hasNoType` ''S.Step +-- inspect $ 'groupsByRollingEq `hasNoType` ''S.GroupByState +inspect $ 'groupsByRollingEq `hasNoType` ''FL.Step +inspect $ 'groupsByRollingEq `hasNoType` ''SPEC +#endif + +{-# INLINE foldIterateM #-} +foldIterateM :: Int -> IO () +foldIterateM value = + withStream value $ + Common.drain + . fmap getSum + . S.foldIterateM + (return . FL.take 2 . FL.sconcat) (return (Sum 0)) + . fmap Sum + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'foldIterateM +inspect $ 'foldIterateM `hasNoType` ''S.Step +inspect $ 'foldIterateM `hasNoType` ''S.FIterState +inspect $ 'foldIterateM `hasNoType` ''FL.Step +inspect $ 'foldIterateM `hasNoType` ''SPEC +#endif + +o_1_space_grouping :: Int -> [Benchmark] +o_1_space_grouping value = + -- Buffering operations using heap proportional to group/window sizes. + [ bgroup "grouping" + [ + benchIO "groups" $ groups value + , benchIO "groupsWhileLT" $ groupsWhileLT value + , benchIO "groupsWhileEq" $ groupsWhileEq value + , benchIO "groupsByRollingLT" $ groupsByRollingLT value + , benchIO "groupsByRollingEq" $ groupsByRollingEq value + + -- XXX parseMany/parseIterate benchmarks are in the Parser/ParserD + -- modules we can bring those here. chunksOf benchmarks are in + -- Parser/ParserD/Array.Stream/FileSystem.Handle. + , benchIO "foldIterateM" $ foldIterateM value + ] + ] + +------------------------------------------------------------------------------- +-- Main +------------------------------------------------------------------------------- + +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = + map (SpaceO_1,) (o_1_space_grouping size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs similarity index 99% rename from benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs rename to benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs index 5fcb8a3b0b..250a783b68 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/ParseSplit.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs @@ -19,7 +19,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.ParseSplit (benchmarks) where +module Stream.Parse.Split (benchmarks) where import Data.Char (ord) import Data.Word (Word8) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse/SplitChunks.hs similarity index 98% rename from benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs rename to benchmark/Streamly/Benchmark/Data/Stream/Parse/SplitChunks.hs index 8acadc5343..adffa1b8fe 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/ParseSplitChunks.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse/SplitChunks.hs @@ -19,7 +19,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.ParseSplitChunks (benchmarks) where +module Stream.Parse.SplitChunks (benchmarks) where import System.IO (Handle) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs index 7cea6f2d58..23141a040d 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform.hs @@ -4,1217 +4,13 @@ -- License : BSD-3-Clause -- Maintainer : streamly@composewell.com -{-# LANGUAGE CPP #-} -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE RankNTypes #-} - -{-# OPTIONS_GHC -Wno-orphans #-} - -#ifdef __HADDOCK_VERSION__ -#undef INSPECTION -#endif - -#ifdef INSPECTION -{-# LANGUAGE TemplateHaskell #-} -{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} -#endif - module Stream.Transform (benchmarks) where -#ifdef INSPECTION -import GHC.Types (SPEC(..)) -import Test.Inspection -import qualified Streamly.Internal.Data.Producer as Producer -#endif +import qualified Stream.Transform.Basic as Basic +import qualified Stream.Transform.Composed as Composed -import Control.DeepSeq (NFData(..)) -import Control.Monad.IO.Class (MonadIO(..)) -import Streamly.Internal.Data.Stream (Stream) -import System.Random (randomRIO) - -import qualified Stream.Common as Common -import qualified Streamly.Internal.Data.Fold as FL -import qualified Streamly.Internal.Data.Scanl as Scanl -import qualified Streamly.Internal.Data.Stream as Stream -import qualified Streamly.Internal.Data.Unfold as Unfold - -import Test.Tasty.Bench -import Stream.Common hiding (scanl', benchIO) +import Test.Tasty.Bench (Benchmark) import Streamly.Benchmark.Common -import Prelude hiding (sequence, mapM) - -------------------------------------------------------------------------------- --- Helpers -------------------------------------------------------------------------------- - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = withRandomIntIO (f . sourceUnfoldrM value) - -------------------------------------------------------------------------------- --- Pipelines (stream-to-stream transformations) -------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- --- one-to-one transformations -------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- --- maps and scans -------------------------------------------------------------------------------- - -{-# INLINE scanl' #-} -scanl' :: MonadIO m => Int -> Stream m Int -> m () -scanl' n = composeN n $ Stream.scanl' (+) 0 - -{-# INLINE scanl'1 #-} -scanl'1 :: Int -> IO () -scanl'1 value = withStream value (scanl' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl'1 -inspect $ 'scanl'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanl'4 #-} -scanl'4 :: Int -> IO () -scanl'4 value = withStream value (scanl' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl'4 -inspect $ 'scanl'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanlM' #-} -scanlM' :: MonadIO m => Int -> Stream m Int -> m () -scanlM' n = composeN n $ Stream.scanlM' (\b a -> return $ b + a) (return 0) - -{-# INLINE scanlM'1 #-} -scanlM'1 :: Int -> IO () -scanlM'1 value = withStream value (scanlM' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanlM'1 -inspect $ 'scanlM'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanlM'4 #-} -scanlM'4 :: Int -> IO () -scanlM'4 value = withStream value (scanlM' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanlM'4 -inspect $ 'scanlM'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanl1' #-} -scanl1' :: MonadIO m => Int -> Stream m Int -> m () -scanl1' n = composeN n $ Stream.scanl1' (+) - -{-# INLINE scanl1'1 #-} -scanl1'1 :: Int -> IO () -scanl1'1 value = withStream value (scanl1' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl1'1 -inspect $ 'scanl1'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanl1'4 #-} -scanl1'4 :: Int -> IO () -scanl1'4 value = withStream value (scanl1' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl1'4 -inspect $ 'scanl1'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanl1M' #-} -scanl1M' :: MonadIO m => Int -> Stream m Int -> m () -scanl1M' n = composeN n $ Stream.scanl1M' (\b a -> return $ b + a) - -{-# INLINE scanl1M'1 #-} -scanl1M'1 :: Int -> IO () -scanl1M'1 value = withStream value (scanl1M' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl1M'1 -inspect $ 'scanl1M'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scanl1M'4 #-} -scanl1M'4 :: Int -> IO () -scanl1M'4 value = withStream value (scanl1M' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scanl1M'4 -inspect $ 'scanl1M'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE scan #-} -scan :: MonadIO m => Int -> Stream m Int -> m () -scan n = composeN n $ Stream.scanl Scanl.sum - -{-# INLINE scan1 #-} -scan1 :: Int -> IO () -scan1 value = withStream value (scan 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scan1 -inspect $ 'scan1 `hasNoType` ''Stream.Step -inspect $ 'scan1 `hasNoType` ''Stream.ScanState -inspect $ 'scan1 `hasNoType` ''FL.Step -inspect $ 'scan1 `hasNoType` ''SPEC -#endif - -{-# INLINE scan4 #-} -scan4 :: Int -> IO () -scan4 value = withStream value (scan 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'scan4 -inspect $ 'scan4 `hasNoType` ''Stream.Step -inspect $ 'scan4 `hasNoType` ''Stream.ScanState -inspect $ 'scan4 `hasNoType` ''FL.Step -inspect $ 'scan4 `hasNoType` ''SPEC -#endif - -{-# INLINE postscan #-} -postscan :: MonadIO m => Int -> Stream m Int -> m () -postscan n = composeN n $ Stream.postscanl Scanl.sum - -{-# INLINE postscan1 #-} -postscan1 :: Int -> IO () -postscan1 value = withStream value (postscan 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscan1 -inspect $ 'postscan1 `hasNoType` ''Stream.Step -inspect $ 'postscan1 `hasNoType` ''Stream.ScanState -inspect $ 'postscan1 `hasNoType` ''FL.Step -inspect $ 'postscan1 `hasNoType` ''SPEC -#endif - -{-# INLINE postscan4 #-} -postscan4 :: Int -> IO () -postscan4 value = withStream value (postscan 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscan4 -inspect $ 'postscan4 `hasNoType` ''Stream.Step -inspect $ 'postscan4 `hasNoType` ''Stream.ScanState -inspect $ 'postscan4 `hasNoType` ''FL.Step -inspect $ 'postscan4 `hasNoType` ''SPEC -#endif - -{-# INLINE postscanl' #-} -postscanl' :: MonadIO m => Int -> Stream m Int -> m () -postscanl' n = composeN n $ Stream.postscanl' (+) 0 - -{-# INLINE postscanl'1 #-} -postscanl'1 :: Int -> IO () -postscanl'1 value = withStream value (postscanl' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscanl'1 -inspect $ 'postscanl'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE postscanl'4 #-} -postscanl'4 :: Int -> IO () -postscanl'4 value = withStream value (postscanl' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscanl'4 --- inspect $ 'postscanl'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE postscanlM' #-} -postscanlM' :: MonadIO m => Int -> Stream m Int -> m () -postscanlM' n = composeN n $ Stream.postscanlM' (\b a -> return $ b + a) (return 0) - -{-# INLINE postscanlM'1 #-} -postscanlM'1 :: Int -> IO () -postscanlM'1 value = withStream value (postscanlM' 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscanlM'1 -inspect $ 'postscanlM'1 `hasNoType` ''Stream.Step -#endif - -{-# INLINE postscanlM'4 #-} -postscanlM'4 :: Int -> IO () -postscanlM'4 value = withStream value (postscanlM' 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'postscanlM'4 -inspect $ 'postscanlM'4 `hasNoType` ''Stream.Step -#endif - -{-# INLINE sequence #-} -sequence :: MonadAsync m => Stream m (m Int) -> m () -sequence = Common.drain . Stream.sequence - -{-# INLINE sequence1 #-} -sequence1 :: Int -> IO () -sequence1 value = withRandomIntIO $ sequence . sourceUnfoldrAction value - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'sequence1 -inspect $ 'sequence1 `hasNoType` ''Stream.Step -inspect $ 'sequence1 `hasNoType` ''FL.Step -inspect $ 'sequence1 `hasNoType` ''SPEC -#endif - -{-# INLINE tap #-} -tap :: MonadIO m => Int -> Stream m Int -> m () -tap n = composeN n $ Stream.tap FL.sum - -{-# INLINE tap1 #-} -tap1 :: Int -> IO () -tap1 value = withStream value (tap 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'tap1 -inspect $ 'tap1 `hasNoType` ''Stream.Step -inspect $ 'tap1 `hasNoType` ''Stream.TapState -inspect $ 'tap1 `hasNoType` ''FL.Step -inspect $ 'tap1 `hasNoType` ''SPEC -#endif - -{-# INLINE _timestamped #-} -_timestamped :: MonadIO m => Stream m Int -> m () -_timestamped = Stream.drain . Stream.timestamped -{- -{-# INLINE foldrT #-} -foldrT :: MonadIO m => Int -> Stream m Int -> m () -foldrT n = composeN n (unCrossStream . Stream.foldrT cns (CrossStream Stream.nil)) - - where cns x (CrossStream xs) = CrossStream (Stream.cons x xs) - -{-# INLINE foldrTMap #-} -foldrTMap :: MonadIO m => Int -> Stream m Int -> m () -foldrTMap n = composeN n $ Stream.foldrT (\x xs -> x + 1 `Stream.cons` xs) Stream.nil --} - -{-# INLINE trace #-} -trace :: MonadAsync m => Int -> Stream m Int -> m () -trace n = composeN n $ Stream.trace return - -{-# INLINE trace4 #-} -trace4 :: Int -> IO () -trace4 value = withStream value (trace 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'trace4 -inspect $ 'trace4 `hasNoType` ''Stream.Step -inspect $ 'trace4 `hasNoType` ''FL.Step -inspect $ 'trace4 `hasNoType` ''SPEC -#endif - -o_1_space_mapping :: Int -> [Benchmark] -o_1_space_mapping value = - [ bgroup - "mapping" - [ - -- , benchIOSink value "foldrT" (foldrT 1) - -- , benchIOSink value "foldrTMap" (foldrTMap 1) - - -- Mapping - benchIO "sequence" $ sequence1 value - , benchIO "tap" $ tap1 value - -- XXX tasty-bench hangs benchmarking this - -- , benchIOSink value "timestamped" _timestamped - -- Scanning - , benchIO "scanl'" $ scanl'1 value - , benchIO "scanl1'" $ scanl1'1 value - , benchIO "scanlM'" $ scanlM'1 value - , benchIO "scanl1M'" $ scanl1M'1 value - , benchIO "postscanl'" $ postscanl'1 value - , benchIO "postscanlM'" $ postscanlM'1 value - , benchIO "scan" $ scan1 value - , benchIO "postscan" $ postscan1 value - ] - ] - -o_1_space_mappingX4 :: Int -> [Benchmark] -o_1_space_mappingX4 value = - [ bgroup "mappingX4" - [ benchIO "trace" $ trace4 value - - , benchIO "scanl'" $ scanl'4 value - , benchIO "scanl1'" $ scanl1'4 value - , benchIO "scanlM'" $ scanlM'4 value - , benchIO "scanl1M'" $ scanl1M'4 value - , benchIO "postscanl'" $ postscanl'4 value - , benchIO "postscanlM'" $ postscanlM'4 value - , benchIO "scan" $ scan4 value - , benchIO "postscan" $ postscan4 value - ] - ] - -{-# INLINE sieveScan #-} -sieveScan :: Monad m => Stream m Int -> Stream m Int -sieveScan = - Stream.mapMaybe snd - . Stream.scanl (Scanl.scanlM' (\(primes, _) n -> do - return $ - let ps = takeWhile (\p -> p * p <= n) primes - in if all (\p -> n `mod` p /= 0) ps - then (primes ++ [n], Just n) - else (primes, Nothing)) (return ([2], Just 2))) - -{-# INLINE naivePrimeSieve #-} -naivePrimeSieve :: Int -> IO Int -naivePrimeSieve value = - withRandomIntIO $ \n -> - Stream.fold FL.sum $ sieveScan $ Stream.enumerateFromTo 2 (value + n) - -o_n_space_mapping :: Int -> [Benchmark] -o_n_space_mapping value = - [ bgroup "mapping" - [ benchIO "naive prime sieve" $ naivePrimeSieve value - ] - ] - -------------------------------------------------------------------------------- --- Iteration/looping utilities -------------------------------------------------------------------------------- - -{-# INLINE iterateN #-} -iterateN :: (Int -> a -> a) -> a -> Int -> a -iterateN g initial count = f count initial - - where - - f (0 :: Int) x = x - f i x = f (i - 1) (g i x) - --- Iterate a transformation over a singleton stream -{-# INLINE iterateSingleton #-} -iterateSingleton :: Applicative m => - (Int -> Stream m Int -> Stream m Int) - -> Int - -> Int - -> Stream m Int -iterateSingleton g count n = iterateN g (Stream.fromPure n) count - -{- --- XXX need to check why this is slower than the explicit recursion above, even --- if the above code is written in a foldr like head recursive way. We also --- need to try this with foldlM' once #150 is fixed. --- However, it is perhaps best to keep the iteration benchmarks independent of --- foldrM and any related fusion issues. -{-# INLINE _iterateSingleton #-} -_iterateSingleton :: - Monad m - => (Int -> Stream m Int -> Stream m Int) - -> Int - -> Int - -> Stream m Int -_iterateSingleton g value n = S.foldrM g (return n) $ sourceIntFromTo value n --} - -{-# INLINE iteratePlusBaseline #-} -iteratePlusBaseline :: Int -> IO Int -iteratePlusBaseline value = - withRandomIntIO $ \i0 -> - iterateN (\i acc -> acc >>= \n -> return $ i + n) (return i0) value - -{-# INLINE iterateSubMap #-} -iterateSubMap :: Int -> IO () -iterateSubMap value = withRandomIntIO $ drain . iterateSingleton (<$) value - -{-# INLINE iterateFmap #-} -iterateFmap :: Int -> IO () -iterateFmap value = withRandomIntIO $ drain . iterateSingleton (fmap . (+)) value - -o_n_space_iterated :: Int -> [Benchmark] -o_n_space_iterated value = - [ bgroup "iterated" - [ benchIO "(+) (n times) (baseline)" $ iteratePlusBaseline value - , benchIO "(<$) (n times)" $ iterateSubMap value - , benchIO "fmap (n times)" $ iterateFmap value - {- - , benchIOSrc fromSerial "_(<$) (n times)" $ - _iterateSingleton (<$) value - , benchIOSrc fromSerial "_fmap (n times)" $ - _iterateSingleton (fmap . (+)) value - -} - ] - ] - -------------------------------------------------------------------------------- --- Size reducing transformations (filtering) -------------------------------------------------------------------------------- - -{-# INLINE filterEven #-} -filterEven :: MonadIO m => Int -> Stream m Int -> m () -filterEven n = composeN n $ Stream.filter even - -{-# INLINE filterEven1 #-} -filterEven1 :: Int -> IO () -filterEven1 value = withStream value (filterEven 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterEven1 -inspect $ 'filterEven1 `hasNoType` ''Stream.Step -inspect $ 'filterEven1 `hasNoType` ''FL.Step -inspect $ 'filterEven1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterEven4 #-} -filterEven4 :: Int -> IO () -filterEven4 value = withStream value (filterEven 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterEven4 -inspect $ 'filterEven4 `hasNoType` ''Stream.Step -inspect $ 'filterEven4 `hasNoType` ''FL.Step -inspect $ 'filterEven4 `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllOut #-} -filterAllOut :: MonadIO m => Int -> Int -> Stream m Int -> m () -filterAllOut value n = composeN n $ Stream.filter (> (value + 1)) - -{-# INLINE filterAllOut1 #-} -filterAllOut1 :: Int -> IO () -filterAllOut1 value = withStream value (filterAllOut value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterAllOut1 -inspect $ 'filterAllOut1 `hasNoType` ''Stream.Step -inspect $ 'filterAllOut1 `hasNoType` ''FL.Step -inspect $ 'filterAllOut1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllOut4 #-} -filterAllOut4 :: Int -> IO () -filterAllOut4 value = withStream value (filterAllOut value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterAllOut4 -inspect $ 'filterAllOut4 `hasNoType` ''Stream.Step -inspect $ 'filterAllOut4 `hasNoType` ''FL.Step -inspect $ 'filterAllOut4 `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllIn #-} -filterAllIn :: MonadIO m => Int -> Int -> Stream m Int -> m () -filterAllIn value n = composeN n $ Stream.filter (<= (value + 1)) - -{-# INLINE filterAllIn1 #-} -filterAllIn1 :: Int -> IO () -filterAllIn1 value = withStream value (filterAllIn value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterAllIn1 -inspect $ 'filterAllIn1 `hasNoType` ''Stream.Step -inspect $ 'filterAllIn1 `hasNoType` ''FL.Step -inspect $ 'filterAllIn1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllIn4 #-} -filterAllIn4 :: Int -> IO () -filterAllIn4 value = withStream value (filterAllIn value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterAllIn4 -inspect $ 'filterAllIn4 `hasNoType` ''Stream.Step -inspect $ 'filterAllIn4 `hasNoType` ''FL.Step -inspect $ 'filterAllIn4 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMEven #-} -filterMEven :: MonadIO m => Int -> Stream m Int -> m () -filterMEven n = composeN n $ Stream.filterM (return . even) - -{-# INLINE filterMEven1 #-} -filterMEven1 :: Int -> IO () -filterMEven1 value = withStream value (filterMEven 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMEven1 -inspect $ 'filterMEven1 `hasNoType` ''Stream.Step -inspect $ 'filterMEven1 `hasNoType` ''FL.Step -inspect $ 'filterMEven1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMEven4 #-} -filterMEven4 :: Int -> IO () -filterMEven4 value = withStream value (filterMEven 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMEven4 -inspect $ 'filterMEven4 `hasNoType` ''Stream.Step -inspect $ 'filterMEven4 `hasNoType` ''FL.Step -inspect $ 'filterMEven4 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMAllOut #-} -filterMAllOut :: MonadIO m => Int -> Int -> Stream m Int -> m () -filterMAllOut value n = composeN n $ Stream.filterM (\x -> return $ x > (value + 1)) - -{-# INLINE filterMAllOut1 #-} -filterMAllOut1 :: Int -> IO () -filterMAllOut1 value = withStream value (filterMAllOut value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMAllOut1 -inspect $ 'filterMAllOut1 `hasNoType` ''Stream.Step -inspect $ 'filterMAllOut1 `hasNoType` ''FL.Step -inspect $ 'filterMAllOut1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMAllOut4 #-} -filterMAllOut4 :: Int -> IO () -filterMAllOut4 value = withStream value (filterMAllOut value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMAllOut4 -inspect $ 'filterMAllOut4 `hasNoType` ''Stream.Step -inspect $ 'filterMAllOut4 `hasNoType` ''FL.Step -inspect $ 'filterMAllOut4 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMAllIn #-} -filterMAllIn :: MonadIO m => Int -> Int -> Stream m Int -> m () -filterMAllIn value n = composeN n $ Stream.filterM (\x -> return $ x <= (value + 1)) - -{-# INLINE filterMAllIn1 #-} -filterMAllIn1 :: Int -> IO () -filterMAllIn1 value = withStream value (filterMAllIn value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMAllIn1 -inspect $ 'filterMAllIn1 `hasNoType` ''Stream.Step -inspect $ 'filterMAllIn1 `hasNoType` ''FL.Step -inspect $ 'filterMAllIn1 `hasNoType` ''SPEC -#endif - -{-# INLINE filterMAllIn4 #-} -filterMAllIn4 :: Int -> IO () -filterMAllIn4 value = withStream value (filterMAllIn value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'filterMAllIn4 -inspect $ 'filterMAllIn4 `hasNoType` ''Stream.Step -inspect $ 'filterMAllIn4 `hasNoType` ''FL.Step -inspect $ 'filterMAllIn4 `hasNoType` ''SPEC -#endif - -{-# INLINE dropOne #-} -dropOne :: MonadIO m => Int -> Stream m Int -> m () -dropOne n = composeN n $ Stream.drop 1 - -{-# INLINE dropOne1 #-} -dropOne1 :: Int -> IO () -dropOne1 value = withStream value (dropOne 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropOne1 -inspect $ 'dropOne1 `hasNoType` ''Stream.Step -inspect $ 'dropOne1 `hasNoType` ''FL.Step -inspect $ 'dropOne1 `hasNoType` ''SPEC -#endif - -{-# INLINE dropOne4 #-} -dropOne4 :: Int -> IO () -dropOne4 value = withStream value (dropOne 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropOne4 -inspect $ 'dropOne4 `hasNoType` ''Stream.Step -inspect $ 'dropOne4 `hasNoType` ''FL.Step -inspect $ 'dropOne4 `hasNoType` ''SPEC -#endif - -{-# INLINE dropAll #-} -dropAll :: MonadIO m => Int -> Int -> Stream m Int -> m () -dropAll value n = composeN n $ Stream.drop (value + 1) - -{-# INLINE dropAll1 #-} -dropAll1 :: Int -> IO () -dropAll1 value = withStream value (dropAll value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropAll1 -inspect $ 'dropAll1 `hasNoType` ''Stream.Step -inspect $ 'dropAll1 `hasNoType` ''FL.Step -inspect $ 'dropAll1 `hasNoType` ''SPEC -#endif - -{-# INLINE dropAll4 #-} -dropAll4 :: Int -> IO () -dropAll4 value = withStream value (dropAll value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropAll4 -inspect $ 'dropAll4 `hasNoType` ''Stream.Step -inspect $ 'dropAll4 `hasNoType` ''FL.Step -inspect $ 'dropAll4 `hasNoType` ''SPEC -#endif - -{-# INLINE dropWhileTrue #-} -dropWhileTrue :: MonadIO m => Int -> Int -> Stream m Int -> m () -dropWhileTrue value n = composeN n $ Stream.dropWhile (<= (value + 1)) - -{-# INLINE dropWhileTrue1 #-} -dropWhileTrue1 :: Int -> IO () -dropWhileTrue1 value = withStream value (dropWhileTrue value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropWhileTrue1 -inspect $ 'dropWhileTrue1 `hasNoType` ''Stream.Step -inspect $ 'dropWhileTrue1 `hasNoType` ''Stream.DropWhileState -inspect $ 'dropWhileTrue1 `hasNoType` ''FL.Step -inspect $ 'dropWhileTrue1 `hasNoType` ''SPEC -#endif - -{-# INLINE dropWhileTrue4 #-} -dropWhileTrue4 :: Int -> IO () -dropWhileTrue4 value = withStream value (dropWhileTrue value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropWhileTrue4 -inspect $ 'dropWhileTrue4 `hasNoType` ''Stream.Step -inspect $ 'dropWhileTrue4 `hasNoType` ''Stream.DropWhileState -inspect $ 'dropWhileTrue4 `hasNoType` ''FL.Step -inspect $ 'dropWhileTrue4 `hasNoType` ''SPEC -#endif - -{-# INLINE dropWhileMTrue #-} -dropWhileMTrue :: MonadIO m => Int -> Int -> Stream m Int -> m () -dropWhileMTrue value n = composeN n $ Stream.dropWhileM (return . (<= (value + 1))) - -{-# INLINE dropWhileMTrue4 #-} -dropWhileMTrue4 :: Int -> IO () -dropWhileMTrue4 value = withStream value (dropWhileMTrue value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropWhileMTrue4 -inspect $ 'dropWhileMTrue4 `hasNoType` ''Stream.Step -inspect $ 'dropWhileMTrue4 `hasNoType` ''Stream.DropWhileState -inspect $ 'dropWhileMTrue4 `hasNoType` ''FL.Step -inspect $ 'dropWhileMTrue4 `hasNoType` ''SPEC -#endif - -{-# INLINE dropWhileFalse #-} -dropWhileFalse :: MonadIO m => Int -> Int -> Stream m Int -> m () -dropWhileFalse value n = composeN n $ Stream.dropWhile (> (value + 1)) - -{-# INLINE dropWhileFalse1 #-} -dropWhileFalse1 :: Int -> IO () -dropWhileFalse1 value = withStream value (dropWhileFalse value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropWhileFalse1 -inspect $ 'dropWhileFalse1 `hasNoType` ''Stream.Step -inspect $ 'dropWhileFalse1 `hasNoType` ''Stream.DropWhileState -inspect $ 'dropWhileFalse1 `hasNoType` ''FL.Step -inspect $ 'dropWhileFalse1 `hasNoType` ''SPEC -#endif - -{-# INLINE dropWhileFalse4 #-} -dropWhileFalse4 :: Int -> IO () -dropWhileFalse4 value = withStream value (dropWhileFalse value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'dropWhileFalse4 -inspect $ 'dropWhileFalse4 `hasNoType` ''Stream.Step -inspect $ 'dropWhileFalse4 `hasNoType` ''Stream.DropWhileState -inspect $ 'dropWhileFalse4 `hasNoType` ''FL.Step -inspect $ 'dropWhileFalse4 `hasNoType` ''SPEC -#endif - -{-# INLINE findIndices #-} -findIndices :: MonadIO m => Int -> Int -> Stream m Int -> m () -findIndices value n = composeN n $ Stream.findIndices (== (value + 1)) - -{-# INLINE findIndices1 #-} -findIndices1 :: Int -> IO () -findIndices1 value = withStream value (findIndices value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'findIndices1 -inspect $ 'findIndices1 `hasNoType` ''Stream.Step -inspect $ 'findIndices1 `hasNoType` ''FL.Step -inspect $ 'findIndices1 `hasNoType` ''SPEC -#endif - -{-# INLINE findIndices4 #-} -findIndices4 :: Int -> IO () -findIndices4 value = withStream value (findIndices value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'findIndices4 -inspect $ 'findIndices4 `hasNoType` ''Stream.Step -inspect $ 'findIndices4 `hasNoType` ''FL.Step -inspect $ 'findIndices4 `hasNoType` ''SPEC -#endif - -{-# INLINE elemIndices #-} -elemIndices :: MonadIO m => Int -> Int -> Stream m Int -> m () -elemIndices value n = composeN n $ Stream.elemIndices (value + 1) - -{-# INLINE elemIndices1 #-} -elemIndices1 :: Int -> IO () -elemIndices1 value = withStream value (elemIndices value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'elemIndices1 -inspect $ 'elemIndices1 `hasNoType` ''Stream.Step -inspect $ 'elemIndices1 `hasNoType` ''FL.Step -inspect $ 'elemIndices1 `hasNoType` ''SPEC -#endif - -{-# INLINE elemIndices4 #-} -elemIndices4 :: Int -> IO () -elemIndices4 value = withStream value (elemIndices value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'elemIndices4 -inspect $ 'elemIndices4 `hasNoType` ''Stream.Step -inspect $ 'elemIndices4 `hasNoType` ''FL.Step -inspect $ 'elemIndices4 `hasNoType` ''SPEC -#endif - -{-# INLINE findIndex #-} -findIndex :: Int -> IO (Maybe Int) -findIndex value = withStream value (Stream.head . Stream.findIndices (== (value + 1))) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'findIndex -inspect $ 'findIndex `hasNoType` ''Stream.Step -inspect $ 'findIndex `hasNoType` ''FL.Step -inspect $ 'findIndex `hasNoType` ''SPEC -#endif - -{-# INLINE elemIndex #-} -elemIndex :: Int -> IO (Maybe Int) -elemIndex value = withStream value (Stream.head . Stream.elemIndices (value + 1)) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'elemIndex -inspect $ 'elemIndex `hasNoType` ''Stream.Step -inspect $ 'elemIndex `hasNoType` ''FL.Step -inspect $ 'elemIndex `hasNoType` ''SPEC -#endif - -{-# INLINE deleteBy #-} -deleteBy :: MonadIO m => Int -> Int -> Stream m Int -> m () -deleteBy value n = composeN n $ Stream.deleteBy (>=) (value + 1) - -{-# INLINE deleteBy1 #-} -deleteBy1 :: Int -> IO () -deleteBy1 value = withStream value (deleteBy value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'deleteBy1 -inspect $ 'deleteBy1 `hasNoType` ''Stream.Step -inspect $ 'deleteBy1 `hasNoType` ''FL.Step -inspect $ 'deleteBy1 `hasNoType` ''SPEC -#endif - -{-# INLINE deleteBy4 #-} -deleteBy4 :: Int -> IO () -deleteBy4 value = withStream value (deleteBy value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'deleteBy4 -inspect $ 'deleteBy4 `hasNoType` ''Stream.Step -inspect $ 'deleteBy4 `hasNoType` ''FL.Step -inspect $ 'deleteBy4 `hasNoType` ''SPEC -#endif - --- uniq . uniq == uniq, composeN 2 ~ composeN 1 -{-# INLINE uniq #-} -uniq :: MonadIO m => Int -> Stream m Int -> m () -uniq n = composeN n Stream.uniq - -{-# INLINE uniq1 #-} -uniq1 :: Int -> IO () -uniq1 value = withStream value (uniq 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'uniq1 -inspect $ 'uniq1 `hasNoType` ''Stream.Step -inspect $ 'uniq1 `hasNoType` ''FL.Step -inspect $ 'uniq1 `hasNoType` ''SPEC -#endif - -{-# INLINE uniq4 #-} -uniq4 :: Int -> IO () -uniq4 value = withStream value (uniq 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'uniq4 -inspect $ 'uniq4 `hasNoType` ''Stream.Step -inspect $ 'uniq4 `hasNoType` ''FL.Step -inspect $ 'uniq4 `hasNoType` ''SPEC -#endif - -{-# INLINE mapMaybe #-} -mapMaybe :: MonadIO m => Int -> Stream m Int -> m () -mapMaybe n = - composeN n $ - Stream.mapMaybe - (\x -> - if odd x - then Nothing - else Just x) - -{-# INLINE mapMaybe1 #-} -mapMaybe1 :: Int -> IO () -mapMaybe1 value = withStream value (mapMaybe 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'mapMaybe1 -inspect $ 'mapMaybe1 `hasNoType` ''Stream.Step -inspect $ 'mapMaybe1 `hasNoType` ''FL.Step -inspect $ 'mapMaybe1 `hasNoType` ''SPEC -#endif - -{-# INLINE mapMaybe4 #-} -mapMaybe4 :: Int -> IO () -mapMaybe4 value = withStream value (mapMaybe 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'mapMaybe4 -inspect $ 'mapMaybe4 `hasNoType` ''Stream.Step -inspect $ 'mapMaybe4 `hasNoType` ''FL.Step -inspect $ 'mapMaybe4 `hasNoType` ''SPEC -#endif - -{-# INLINE mapMaybeM #-} -mapMaybeM :: MonadAsync m => Int -> Stream m Int -> m () -mapMaybeM n = - composeN n $ - Stream.mapMaybeM - (\x -> - if odd x - then return Nothing - else return $ Just x) - -{-# INLINE mapMaybeM1 #-} -mapMaybeM1 :: Int -> IO () -mapMaybeM1 value = withStream value (mapMaybeM 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'mapMaybeM1 -inspect $ 'mapMaybeM1 `hasNoType` ''Stream.Step -inspect $ 'mapMaybeM1 `hasNoType` ''FL.Step -inspect $ 'mapMaybeM1 `hasNoType` ''SPEC -#endif - -{-# INLINE mapMaybeM4 #-} -mapMaybeM4 :: Int -> IO () -mapMaybeM4 value = withStream value (mapMaybeM 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'mapMaybeM4 -inspect $ 'mapMaybeM4 `hasNoType` ''Stream.Step -inspect $ 'mapMaybeM4 `hasNoType` ''FL.Step -inspect $ 'mapMaybeM4 `hasNoType` ''SPEC -#endif - -o_1_space_filtering :: Int -> [Benchmark] -o_1_space_filtering value = - [ bgroup "filtering" - [ benchIO "filter-even" $ filterEven1 value - , benchIO "filter-all-out" $ filterAllOut1 value - , benchIO "filter-all-in" $ filterAllIn1 value - - , benchIO "filterM-even" $ filterMEven1 value - , benchIO "filterM-all-out" $ filterMAllOut1 value - , benchIO "filterM-all-in" $ filterMAllIn1 value - - , benchIO "drop-one" $ dropOne1 value - , benchIO "drop-all" $ dropAll1 value - , benchIO "dropWhile-true" $ dropWhileTrue1 value - -- , benchIO "dropWhileM-true" ... - , benchIO "dropWhile-false" $ dropWhileFalse1 value - , benchIO "deleteBy" $ deleteBy1 value - - , benchIO "uniq" $ uniq1 value - - -- Map and filter - , benchIO "mapMaybe" $ mapMaybe1 value - , benchIO "mapMaybeM" $ mapMaybeM1 value - - -- Searching (stateful map and filter) - , benchIO "findIndices" $ findIndices1 value - , benchIO "elemIndices" $ elemIndices1 value - , benchIO "findIndex" $ findIndex value - , benchIO "elemIndex" $ elemIndex value - ] - ] - -o_1_space_filteringX4 :: Int -> [Benchmark] -o_1_space_filteringX4 value = - [ bgroup "filteringX4" - [ benchIO "filter-even" $ filterEven4 value - , benchIO "filter-all-out" $ filterAllOut4 value - , benchIO "filter-all-in" $ filterAllIn4 value - - , benchIO "filterM-even" $ filterMEven4 value - , benchIO "filterM-all-out" $ filterMAllOut4 value - , benchIO "filterM-all-in" $ filterMAllIn4 value - - , benchIO "drop-one" $ dropOne4 value - , benchIO "drop-all" $ dropAll4 value - , benchIO "dropWhile-true" $ dropWhileTrue4 value - , benchIO "dropWhileM-true" $ dropWhileMTrue4 value - -- XXX requires @-fspec-constr-recursive=12@. - , benchIO "dropWhile-false" $ dropWhileFalse4 value - , benchIO "deleteBy" $ deleteBy4 value - - , benchIO "uniq" $ uniq4 value - - -- map and filter - , benchIO "mapMaybe" $ mapMaybe4 value - , benchIO "mapMaybeM" $ mapMaybeM4 value - - -- searching - , benchIO "findIndices" $ findIndices4 value - , benchIO "elemIndices" $ elemIndices4 value - ] - ] - -------------------------------------------------------------------------------- --- Size increasing transformations (insertions) -------------------------------------------------------------------------------- - -{-# INLINE intersperse #-} -intersperse :: MonadAsync m => Int -> Int -> Stream m Int -> m () -intersperse value n = composeN n $ Stream.intersperse (value + 1) - -{-# INLINE intersperse1 #-} -intersperse1 :: Int -> IO () -intersperse1 value = withStream value (intersperse value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'intersperse1 -inspect $ 'intersperse1 `hasNoType` ''Stream.Step -inspect $ 'intersperse1 `hasNoType` ''Stream.LoopState -inspect $ 'intersperse1 `hasNoType` ''FL.Step -inspect $ 'intersperse1 `hasNoType` ''SPEC -#endif - -{-# INLINE intersperse4 #-} -intersperse4 :: Int -> IO () -intersperse4 value = withStream value (intersperse value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'intersperse4 -inspect $ 'intersperse4 `hasNoType` ''Stream.Step --- inspect $ 'intersperse4 `hasNoType` ''Stream.LoopState -inspect $ 'intersperse4 `hasNoType` ''FL.Step --- inspect $ 'intersperse4 `hasNoType` ''SPEC -#endif - -{-# INLINE intersperseM #-} -intersperseM :: MonadAsync m => Int -> Int -> Stream m Int -> m () -intersperseM value n = composeN n $ Stream.intersperseM (return $ value + 1) - -{-# INLINE intersperseM1 #-} -intersperseM1 :: Int -> IO () -intersperseM1 value = withStream value (intersperseM value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'intersperseM1 -inspect $ 'intersperseM1 `hasNoType` ''Stream.Step -inspect $ 'intersperseM1 `hasNoType` ''Stream.LoopState -inspect $ 'intersperseM1 `hasNoType` ''FL.Step -inspect $ 'intersperseM1 `hasNoType` ''SPEC -#endif - -{-# INLINE insertBy #-} -insertBy :: MonadIO m => Int -> Int -> Stream m Int -> m () -insertBy value n = composeN n $ Stream.insertBy compare (value + 1) - -{-# INLINE insertBy1 #-} -insertBy1 :: Int -> IO () -insertBy1 value = withStream value (insertBy value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'insertBy1 -inspect $ 'insertBy1 `hasNoType` ''Stream.Step -inspect $ 'insertBy1 `hasNoType` ''FL.Step -inspect $ 'insertBy1 `hasNoType` ''SPEC -#endif - -{-# INLINE insertBy4 #-} -insertBy4 :: Int -> IO () -insertBy4 value = withStream value (insertBy value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'insertBy4 -inspect $ 'insertBy4 `hasNoType` ''Stream.Step -inspect $ 'insertBy4 `hasNoType` ''FL.Step -inspect $ 'insertBy4 `hasNoType` ''SPEC -#endif - -{-# INLINE interposeSuffix #-} -interposeSuffix :: Monad m => Int -> Int -> Stream m Int -> m () -interposeSuffix value n = - composeN n $ Stream.unfoldEachSepBy (value + 1) Unfold.identity - -{-# INLINE interposeSuffix1 #-} -interposeSuffix1 :: Int -> IO () -interposeSuffix1 value = withStream value (interposeSuffix value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'interposeSuffix1 -inspect $ 'interposeSuffix1 `hasNoType` ''Stream.Step -inspect $ 'interposeSuffix1 `hasNoType` ''Stream.InterposeState -inspect $ 'interposeSuffix1 `hasNoType` ''FL.Step -inspect $ 'interposeSuffix1 `hasNoType` ''SPEC -#endif - -{-# INLINE intercalateSuffix #-} -intercalateSuffix :: Monad m => Int -> Int -> Stream m Int -> m () -intercalateSuffix value n = - composeN n $ Stream.unfoldEachSepBySeq (value + 1) Unfold.identity - -{-# INLINE intercalateSuffix1 #-} -intercalateSuffix1 :: Int -> IO () -intercalateSuffix1 value = withStream value (intercalateSuffix value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'intercalateSuffix1 -inspect $ 'intercalateSuffix1 `hasNoType` ''Stream.Step -inspect $ 'intercalateSuffix1 `hasNoType` ''Stream.LoopState -inspect $ 'intercalateSuffix1 `hasNoType` ''Producer.ConcatState -inspect $ 'intercalateSuffix1 `hasNoType` ''FL.Step -inspect $ 'intercalateSuffix1 `hasNoType` ''SPEC -#endif - -o_1_space_inserting :: Int -> [Benchmark] -o_1_space_inserting value = - [ bgroup "inserting" - [ benchIO "intersperse" $ intersperse1 value - , benchIO "intersperseM" $ intersperseM1 value - , benchIO "insertBy" $ insertBy1 value - , benchIO "interposeSuffix" $ interposeSuffix1 value - , benchIO "intercalateSuffix" $ intercalateSuffix1 value - ] - ] - -o_1_space_insertingX4 :: Int -> [Benchmark] -o_1_space_insertingX4 value = - [ bgroup "insertingX4" - [ - -- XXX requires @-fspec-constr-recursive=16@. - benchIO "intersperse" $ intersperse4 value - , benchIO "insertBy" $ insertBy4 value - ] - ] - -------------------------------------------------------------------------------- --- Indexing -------------------------------------------------------------------------------- - -{-# INLINE indexed #-} -indexed :: MonadIO m => Int -> Stream m Int -> m () -indexed n = composeN n (fmap snd . Stream.indexed) - -{-# INLINE indexed1 #-} -indexed1 :: Int -> IO () -indexed1 value = withStream value (indexed 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'indexed1 -inspect $ 'indexed1 `hasNoType` ''Stream.Step -inspect $ 'indexed1 `hasNoType` ''FL.Step -inspect $ 'indexed1 `hasNoType` ''SPEC -#endif - -{-# INLINE indexed4 #-} -indexed4 :: Int -> IO () -indexed4 value = withStream value (indexed 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'indexed4 -inspect $ 'indexed4 `hasNoType` ''Stream.Step -inspect $ 'indexed4 `hasNoType` ''FL.Step -inspect $ 'indexed4 `hasNoType` ''SPEC -#endif - -{-# INLINE indexedR #-} -indexedR :: MonadIO m => Int -> Int -> Stream m Int -> m () -indexedR value n = composeN n (fmap snd . Stream.indexedR value) - -{-# INLINE indexedR1 #-} -indexedR1 :: Int -> IO () -indexedR1 value = withStream value (indexedR value 1) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'indexedR1 -inspect $ 'indexedR1 `hasNoType` ''Stream.Step -inspect $ 'indexedR1 `hasNoType` ''FL.Step -inspect $ 'indexedR1 `hasNoType` ''SPEC -#endif - -{-# INLINE indexedR4 #-} -indexedR4 :: Int -> IO () -indexedR4 value = withStream value (indexedR value 4) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'indexedR4 -inspect $ 'indexedR4 `hasNoType` ''Stream.Step -inspect $ 'indexedR4 `hasNoType` ''FL.Step -inspect $ 'indexedR4 `hasNoType` ''SPEC -#endif - -o_1_space_indexing :: Int -> [Benchmark] -o_1_space_indexing value = - [ bgroup "indexing" - [ benchIO "indexed" $ indexed1 value - , benchIO "indexedR" $ indexedR1 value - ] - ] - -o_1_space_indexingX4 :: Int -> [Benchmark] -o_1_space_indexingX4 value = - [ bgroup "indexingx4" - [ benchIO "indexed" $ indexed4 value - , benchIO "indexedR" $ indexedR4 value - ] - ] - -------------------------------------------------------------------------------- --- Main -------------------------------------------------------------------------------- --- In addition to gauge options, the number of elements in the stream can be --- passed using the --stream-size option. --- benchmarks :: Int -> [(SpaceComplexity, Benchmark)] -benchmarks size = - map (SpaceO_1,) (Prelude.concat - [ o_1_space_mapping size - , o_1_space_mappingX4 size - , o_1_space_filtering size - , o_1_space_filteringX4 size - , o_1_space_inserting size - , o_1_space_insertingX4 size - , o_1_space_indexing size - , o_1_space_indexingX4 size - ]) - ++ map (SpaceO_n,) (Prelude.concat - [ - o_n_space_mapping size - , o_n_space_iterated size - ]) +benchmarks size = Basic.benchmarks size ++ Composed.benchmarks size diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs new file mode 100644 index 0000000000..6e9c40de35 --- /dev/null +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs @@ -0,0 +1,1220 @@ +-- | +-- Module : Stream.Transform +-- Copyright : (c) 2018 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com + +{-# LANGUAGE CPP #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE RankNTypes #-} + +{-# OPTIONS_GHC -Wno-orphans #-} + +#ifdef __HADDOCK_VERSION__ +#undef INSPECTION +#endif + +#ifdef INSPECTION +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} +#endif + +module Stream.Transform.Basic (benchmarks) where + +#ifdef INSPECTION +import GHC.Types (SPEC(..)) +import Test.Inspection +import qualified Streamly.Internal.Data.Producer as Producer +#endif + +import Control.DeepSeq (NFData(..)) +import Control.Monad.IO.Class (MonadIO(..)) +import Streamly.Internal.Data.Stream (Stream) +import System.Random (randomRIO) + +import qualified Stream.Common as Common +import qualified Streamly.Internal.Data.Fold as FL +import qualified Streamly.Internal.Data.Scanl as Scanl +import qualified Streamly.Internal.Data.Stream as Stream +import qualified Streamly.Internal.Data.Unfold as Unfold + +import Test.Tasty.Bench +import Stream.Common hiding (scanl', benchIO) +import Streamly.Benchmark.Common +import Prelude hiding (sequence, mapM) + +------------------------------------------------------------------------------- +-- Helpers +------------------------------------------------------------------------------- + +{-# INLINE benchIO #-} +benchIO :: NFData b => String -> IO b -> Benchmark +benchIO name = bench name . nfIO + +{-# INLINE withRandomIntIO #-} +withRandomIntIO :: (Int -> IO b) -> IO b +withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f + +{-# INLINE withStream #-} +withStream :: Int -> (Stream IO Int -> IO b) -> IO b +withStream value f = withRandomIntIO (f . sourceUnfoldrM value) + +------------------------------------------------------------------------------- +-- Pipelines (stream-to-stream transformations) +------------------------------------------------------------------------------- + +------------------------------------------------------------------------------- +-- one-to-one transformations +------------------------------------------------------------------------------- + +------------------------------------------------------------------------------- +-- maps and scans +------------------------------------------------------------------------------- + +{-# INLINE scanl' #-} +scanl' :: MonadIO m => Int -> Stream m Int -> m () +scanl' n = composeN n $ Stream.scanl' (+) 0 + +{-# INLINE scanl'1 #-} +scanl'1 :: Int -> IO () +scanl'1 value = withStream value (scanl' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl'1 +inspect $ 'scanl'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanl'4 #-} +scanl'4 :: Int -> IO () +scanl'4 value = withStream value (scanl' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl'4 +inspect $ 'scanl'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanlM' #-} +scanlM' :: MonadIO m => Int -> Stream m Int -> m () +scanlM' n = composeN n $ Stream.scanlM' (\b a -> return $ b + a) (return 0) + +{-# INLINE scanlM'1 #-} +scanlM'1 :: Int -> IO () +scanlM'1 value = withStream value (scanlM' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanlM'1 +inspect $ 'scanlM'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanlM'4 #-} +scanlM'4 :: Int -> IO () +scanlM'4 value = withStream value (scanlM' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanlM'4 +inspect $ 'scanlM'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanl1' #-} +scanl1' :: MonadIO m => Int -> Stream m Int -> m () +scanl1' n = composeN n $ Stream.scanl1' (+) + +{-# INLINE scanl1'1 #-} +scanl1'1 :: Int -> IO () +scanl1'1 value = withStream value (scanl1' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl1'1 +inspect $ 'scanl1'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanl1'4 #-} +scanl1'4 :: Int -> IO () +scanl1'4 value = withStream value (scanl1' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl1'4 +inspect $ 'scanl1'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanl1M' #-} +scanl1M' :: MonadIO m => Int -> Stream m Int -> m () +scanl1M' n = composeN n $ Stream.scanl1M' (\b a -> return $ b + a) + +{-# INLINE scanl1M'1 #-} +scanl1M'1 :: Int -> IO () +scanl1M'1 value = withStream value (scanl1M' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl1M'1 +inspect $ 'scanl1M'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scanl1M'4 #-} +scanl1M'4 :: Int -> IO () +scanl1M'4 value = withStream value (scanl1M' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scanl1M'4 +inspect $ 'scanl1M'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE scan #-} +scan :: MonadIO m => Int -> Stream m Int -> m () +scan n = composeN n $ Stream.scanl Scanl.sum + +{-# INLINE scan1 #-} +scan1 :: Int -> IO () +scan1 value = withStream value (scan 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scan1 +inspect $ 'scan1 `hasNoType` ''Stream.Step +inspect $ 'scan1 `hasNoType` ''Stream.ScanState +inspect $ 'scan1 `hasNoType` ''FL.Step +inspect $ 'scan1 `hasNoType` ''SPEC +#endif + +{-# INLINE scan4 #-} +scan4 :: Int -> IO () +scan4 value = withStream value (scan 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'scan4 +inspect $ 'scan4 `hasNoType` ''Stream.Step +inspect $ 'scan4 `hasNoType` ''Stream.ScanState +inspect $ 'scan4 `hasNoType` ''FL.Step +inspect $ 'scan4 `hasNoType` ''SPEC +#endif + +{-# INLINE postscan #-} +postscan :: MonadIO m => Int -> Stream m Int -> m () +postscan n = composeN n $ Stream.postscanl Scanl.sum + +{-# INLINE postscan1 #-} +postscan1 :: Int -> IO () +postscan1 value = withStream value (postscan 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscan1 +inspect $ 'postscan1 `hasNoType` ''Stream.Step +inspect $ 'postscan1 `hasNoType` ''Stream.ScanState +inspect $ 'postscan1 `hasNoType` ''FL.Step +inspect $ 'postscan1 `hasNoType` ''SPEC +#endif + +{-# INLINE postscan4 #-} +postscan4 :: Int -> IO () +postscan4 value = withStream value (postscan 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscan4 +inspect $ 'postscan4 `hasNoType` ''Stream.Step +inspect $ 'postscan4 `hasNoType` ''Stream.ScanState +inspect $ 'postscan4 `hasNoType` ''FL.Step +inspect $ 'postscan4 `hasNoType` ''SPEC +#endif + +{-# INLINE postscanl' #-} +postscanl' :: MonadIO m => Int -> Stream m Int -> m () +postscanl' n = composeN n $ Stream.postscanl' (+) 0 + +{-# INLINE postscanl'1 #-} +postscanl'1 :: Int -> IO () +postscanl'1 value = withStream value (postscanl' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscanl'1 +inspect $ 'postscanl'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE postscanl'4 #-} +postscanl'4 :: Int -> IO () +postscanl'4 value = withStream value (postscanl' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscanl'4 +-- inspect $ 'postscanl'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE postscanlM' #-} +postscanlM' :: MonadIO m => Int -> Stream m Int -> m () +postscanlM' n = composeN n $ Stream.postscanlM' (\b a -> return $ b + a) (return 0) + +{-# INLINE postscanlM'1 #-} +postscanlM'1 :: Int -> IO () +postscanlM'1 value = withStream value (postscanlM' 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscanlM'1 +inspect $ 'postscanlM'1 `hasNoType` ''Stream.Step +#endif + +{-# INLINE postscanlM'4 #-} +postscanlM'4 :: Int -> IO () +postscanlM'4 value = withStream value (postscanlM' 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'postscanlM'4 +inspect $ 'postscanlM'4 `hasNoType` ''Stream.Step +#endif + +{-# INLINE sequence #-} +sequence :: MonadAsync m => Stream m (m Int) -> m () +sequence = Common.drain . Stream.sequence + +{-# INLINE sequence1 #-} +sequence1 :: Int -> IO () +sequence1 value = withRandomIntIO $ sequence . sourceUnfoldrAction value + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'sequence1 +inspect $ 'sequence1 `hasNoType` ''Stream.Step +inspect $ 'sequence1 `hasNoType` ''FL.Step +inspect $ 'sequence1 `hasNoType` ''SPEC +#endif + +{-# INLINE tap #-} +tap :: MonadIO m => Int -> Stream m Int -> m () +tap n = composeN n $ Stream.tap FL.sum + +{-# INLINE tap1 #-} +tap1 :: Int -> IO () +tap1 value = withStream value (tap 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'tap1 +inspect $ 'tap1 `hasNoType` ''Stream.Step +inspect $ 'tap1 `hasNoType` ''Stream.TapState +inspect $ 'tap1 `hasNoType` ''FL.Step +inspect $ 'tap1 `hasNoType` ''SPEC +#endif + +{-# INLINE _timestamped #-} +_timestamped :: MonadIO m => Stream m Int -> m () +_timestamped = Stream.drain . Stream.timestamped +{- +{-# INLINE foldrT #-} +foldrT :: MonadIO m => Int -> Stream m Int -> m () +foldrT n = composeN n (unCrossStream . Stream.foldrT cns (CrossStream Stream.nil)) + + where cns x (CrossStream xs) = CrossStream (Stream.cons x xs) + +{-# INLINE foldrTMap #-} +foldrTMap :: MonadIO m => Int -> Stream m Int -> m () +foldrTMap n = composeN n $ Stream.foldrT (\x xs -> x + 1 `Stream.cons` xs) Stream.nil +-} + +{-# INLINE trace #-} +trace :: MonadAsync m => Int -> Stream m Int -> m () +trace n = composeN n $ Stream.trace return + +{-# INLINE trace4 #-} +trace4 :: Int -> IO () +trace4 value = withStream value (trace 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'trace4 +inspect $ 'trace4 `hasNoType` ''Stream.Step +inspect $ 'trace4 `hasNoType` ''FL.Step +inspect $ 'trace4 `hasNoType` ''SPEC +#endif + +o_1_space_mapping :: Int -> [Benchmark] +o_1_space_mapping value = + [ bgroup + "mapping" + [ + -- , benchIOSink value "foldrT" (foldrT 1) + -- , benchIOSink value "foldrTMap" (foldrTMap 1) + + -- Mapping + benchIO "sequence" $ sequence1 value + , benchIO "tap" $ tap1 value + -- XXX tasty-bench hangs benchmarking this + -- , benchIOSink value "timestamped" _timestamped + -- Scanning + , benchIO "scanl'" $ scanl'1 value + , benchIO "scanl1'" $ scanl1'1 value + , benchIO "scanlM'" $ scanlM'1 value + , benchIO "scanl1M'" $ scanl1M'1 value + , benchIO "postscanl'" $ postscanl'1 value + , benchIO "postscanlM'" $ postscanlM'1 value + , benchIO "scan" $ scan1 value + , benchIO "postscan" $ postscan1 value + ] + ] + +o_1_space_mappingX4 :: Int -> [Benchmark] +o_1_space_mappingX4 value = + [ bgroup "mappingX4" + [ benchIO "trace" $ trace4 value + + , benchIO "scanl'" $ scanl'4 value + , benchIO "scanl1'" $ scanl1'4 value + , benchIO "scanlM'" $ scanlM'4 value + , benchIO "scanl1M'" $ scanl1M'4 value + , benchIO "postscanl'" $ postscanl'4 value + , benchIO "postscanlM'" $ postscanlM'4 value + , benchIO "scan" $ scan4 value + , benchIO "postscan" $ postscan4 value + ] + ] + +{-# INLINE sieveScan #-} +sieveScan :: Monad m => Stream m Int -> Stream m Int +sieveScan = + Stream.mapMaybe snd + . Stream.scanl (Scanl.scanlM' (\(primes, _) n -> do + return $ + let ps = takeWhile (\p -> p * p <= n) primes + in if all (\p -> n `mod` p /= 0) ps + then (primes ++ [n], Just n) + else (primes, Nothing)) (return ([2], Just 2))) + +{-# INLINE naivePrimeSieve #-} +naivePrimeSieve :: Int -> IO Int +naivePrimeSieve value = + withRandomIntIO $ \n -> + Stream.fold FL.sum $ sieveScan $ Stream.enumerateFromTo 2 (value + n) + +o_n_space_mapping :: Int -> [Benchmark] +o_n_space_mapping value = + [ bgroup "mapping" + [ benchIO "naive prime sieve" $ naivePrimeSieve value + ] + ] + +------------------------------------------------------------------------------- +-- Iteration/looping utilities +------------------------------------------------------------------------------- + +{-# INLINE iterateN #-} +iterateN :: (Int -> a -> a) -> a -> Int -> a +iterateN g initial count = f count initial + + where + + f (0 :: Int) x = x + f i x = f (i - 1) (g i x) + +-- Iterate a transformation over a singleton stream +{-# INLINE iterateSingleton #-} +iterateSingleton :: Applicative m => + (Int -> Stream m Int -> Stream m Int) + -> Int + -> Int + -> Stream m Int +iterateSingleton g count n = iterateN g (Stream.fromPure n) count + +{- +-- XXX need to check why this is slower than the explicit recursion above, even +-- if the above code is written in a foldr like head recursive way. We also +-- need to try this with foldlM' once #150 is fixed. +-- However, it is perhaps best to keep the iteration benchmarks independent of +-- foldrM and any related fusion issues. +{-# INLINE _iterateSingleton #-} +_iterateSingleton :: + Monad m + => (Int -> Stream m Int -> Stream m Int) + -> Int + -> Int + -> Stream m Int +_iterateSingleton g value n = S.foldrM g (return n) $ sourceIntFromTo value n +-} + +{-# INLINE iteratePlusBaseline #-} +iteratePlusBaseline :: Int -> IO Int +iteratePlusBaseline value = + withRandomIntIO $ \i0 -> + iterateN (\i acc -> acc >>= \n -> return $ i + n) (return i0) value + +{-# INLINE iterateSubMap #-} +iterateSubMap :: Int -> IO () +iterateSubMap value = withRandomIntIO $ drain . iterateSingleton (<$) value + +{-# INLINE iterateFmap #-} +iterateFmap :: Int -> IO () +iterateFmap value = withRandomIntIO $ drain . iterateSingleton (fmap . (+)) value + +o_n_space_iterated :: Int -> [Benchmark] +o_n_space_iterated value = + [ bgroup "iterated" + [ benchIO "(+) (n times) (baseline)" $ iteratePlusBaseline value + , benchIO "(<$) (n times)" $ iterateSubMap value + , benchIO "fmap (n times)" $ iterateFmap value + {- + , benchIOSrc fromSerial "_(<$) (n times)" $ + _iterateSingleton (<$) value + , benchIOSrc fromSerial "_fmap (n times)" $ + _iterateSingleton (fmap . (+)) value + -} + ] + ] + +------------------------------------------------------------------------------- +-- Size reducing transformations (filtering) +------------------------------------------------------------------------------- + +{-# INLINE filterEven #-} +filterEven :: MonadIO m => Int -> Stream m Int -> m () +filterEven n = composeN n $ Stream.filter even + +{-# INLINE filterEven1 #-} +filterEven1 :: Int -> IO () +filterEven1 value = withStream value (filterEven 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterEven1 +inspect $ 'filterEven1 `hasNoType` ''Stream.Step +inspect $ 'filterEven1 `hasNoType` ''FL.Step +inspect $ 'filterEven1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterEven4 #-} +filterEven4 :: Int -> IO () +filterEven4 value = withStream value (filterEven 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterEven4 +inspect $ 'filterEven4 `hasNoType` ''Stream.Step +inspect $ 'filterEven4 `hasNoType` ''FL.Step +inspect $ 'filterEven4 `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllOut #-} +filterAllOut :: MonadIO m => Int -> Int -> Stream m Int -> m () +filterAllOut value n = composeN n $ Stream.filter (> (value + 1)) + +{-# INLINE filterAllOut1 #-} +filterAllOut1 :: Int -> IO () +filterAllOut1 value = withStream value (filterAllOut value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterAllOut1 +inspect $ 'filterAllOut1 `hasNoType` ''Stream.Step +inspect $ 'filterAllOut1 `hasNoType` ''FL.Step +inspect $ 'filterAllOut1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllOut4 #-} +filterAllOut4 :: Int -> IO () +filterAllOut4 value = withStream value (filterAllOut value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterAllOut4 +inspect $ 'filterAllOut4 `hasNoType` ''Stream.Step +inspect $ 'filterAllOut4 `hasNoType` ''FL.Step +inspect $ 'filterAllOut4 `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllIn #-} +filterAllIn :: MonadIO m => Int -> Int -> Stream m Int -> m () +filterAllIn value n = composeN n $ Stream.filter (<= (value + 1)) + +{-# INLINE filterAllIn1 #-} +filterAllIn1 :: Int -> IO () +filterAllIn1 value = withStream value (filterAllIn value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterAllIn1 +inspect $ 'filterAllIn1 `hasNoType` ''Stream.Step +inspect $ 'filterAllIn1 `hasNoType` ''FL.Step +inspect $ 'filterAllIn1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllIn4 #-} +filterAllIn4 :: Int -> IO () +filterAllIn4 value = withStream value (filterAllIn value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterAllIn4 +inspect $ 'filterAllIn4 `hasNoType` ''Stream.Step +inspect $ 'filterAllIn4 `hasNoType` ''FL.Step +inspect $ 'filterAllIn4 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMEven #-} +filterMEven :: MonadIO m => Int -> Stream m Int -> m () +filterMEven n = composeN n $ Stream.filterM (return . even) + +{-# INLINE filterMEven1 #-} +filterMEven1 :: Int -> IO () +filterMEven1 value = withStream value (filterMEven 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMEven1 +inspect $ 'filterMEven1 `hasNoType` ''Stream.Step +inspect $ 'filterMEven1 `hasNoType` ''FL.Step +inspect $ 'filterMEven1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMEven4 #-} +filterMEven4 :: Int -> IO () +filterMEven4 value = withStream value (filterMEven 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMEven4 +inspect $ 'filterMEven4 `hasNoType` ''Stream.Step +inspect $ 'filterMEven4 `hasNoType` ''FL.Step +inspect $ 'filterMEven4 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMAllOut #-} +filterMAllOut :: MonadIO m => Int -> Int -> Stream m Int -> m () +filterMAllOut value n = composeN n $ Stream.filterM (\x -> return $ x > (value + 1)) + +{-# INLINE filterMAllOut1 #-} +filterMAllOut1 :: Int -> IO () +filterMAllOut1 value = withStream value (filterMAllOut value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMAllOut1 +inspect $ 'filterMAllOut1 `hasNoType` ''Stream.Step +inspect $ 'filterMAllOut1 `hasNoType` ''FL.Step +inspect $ 'filterMAllOut1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMAllOut4 #-} +filterMAllOut4 :: Int -> IO () +filterMAllOut4 value = withStream value (filterMAllOut value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMAllOut4 +inspect $ 'filterMAllOut4 `hasNoType` ''Stream.Step +inspect $ 'filterMAllOut4 `hasNoType` ''FL.Step +inspect $ 'filterMAllOut4 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMAllIn #-} +filterMAllIn :: MonadIO m => Int -> Int -> Stream m Int -> m () +filterMAllIn value n = composeN n $ Stream.filterM (\x -> return $ x <= (value + 1)) + +{-# INLINE filterMAllIn1 #-} +filterMAllIn1 :: Int -> IO () +filterMAllIn1 value = withStream value (filterMAllIn value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMAllIn1 +inspect $ 'filterMAllIn1 `hasNoType` ''Stream.Step +inspect $ 'filterMAllIn1 `hasNoType` ''FL.Step +inspect $ 'filterMAllIn1 `hasNoType` ''SPEC +#endif + +{-# INLINE filterMAllIn4 #-} +filterMAllIn4 :: Int -> IO () +filterMAllIn4 value = withStream value (filterMAllIn value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'filterMAllIn4 +inspect $ 'filterMAllIn4 `hasNoType` ''Stream.Step +inspect $ 'filterMAllIn4 `hasNoType` ''FL.Step +inspect $ 'filterMAllIn4 `hasNoType` ''SPEC +#endif + +{-# INLINE dropOne #-} +dropOne :: MonadIO m => Int -> Stream m Int -> m () +dropOne n = composeN n $ Stream.drop 1 + +{-# INLINE dropOne1 #-} +dropOne1 :: Int -> IO () +dropOne1 value = withStream value (dropOne 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropOne1 +inspect $ 'dropOne1 `hasNoType` ''Stream.Step +inspect $ 'dropOne1 `hasNoType` ''FL.Step +inspect $ 'dropOne1 `hasNoType` ''SPEC +#endif + +{-# INLINE dropOne4 #-} +dropOne4 :: Int -> IO () +dropOne4 value = withStream value (dropOne 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropOne4 +inspect $ 'dropOne4 `hasNoType` ''Stream.Step +inspect $ 'dropOne4 `hasNoType` ''FL.Step +inspect $ 'dropOne4 `hasNoType` ''SPEC +#endif + +{-# INLINE dropAll #-} +dropAll :: MonadIO m => Int -> Int -> Stream m Int -> m () +dropAll value n = composeN n $ Stream.drop (value + 1) + +{-# INLINE dropAll1 #-} +dropAll1 :: Int -> IO () +dropAll1 value = withStream value (dropAll value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropAll1 +inspect $ 'dropAll1 `hasNoType` ''Stream.Step +inspect $ 'dropAll1 `hasNoType` ''FL.Step +inspect $ 'dropAll1 `hasNoType` ''SPEC +#endif + +{-# INLINE dropAll4 #-} +dropAll4 :: Int -> IO () +dropAll4 value = withStream value (dropAll value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropAll4 +inspect $ 'dropAll4 `hasNoType` ''Stream.Step +inspect $ 'dropAll4 `hasNoType` ''FL.Step +inspect $ 'dropAll4 `hasNoType` ''SPEC +#endif + +{-# INLINE dropWhileTrue #-} +dropWhileTrue :: MonadIO m => Int -> Int -> Stream m Int -> m () +dropWhileTrue value n = composeN n $ Stream.dropWhile (<= (value + 1)) + +{-# INLINE dropWhileTrue1 #-} +dropWhileTrue1 :: Int -> IO () +dropWhileTrue1 value = withStream value (dropWhileTrue value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropWhileTrue1 +inspect $ 'dropWhileTrue1 `hasNoType` ''Stream.Step +inspect $ 'dropWhileTrue1 `hasNoType` ''Stream.DropWhileState +inspect $ 'dropWhileTrue1 `hasNoType` ''FL.Step +inspect $ 'dropWhileTrue1 `hasNoType` ''SPEC +#endif + +{-# INLINE dropWhileTrue4 #-} +dropWhileTrue4 :: Int -> IO () +dropWhileTrue4 value = withStream value (dropWhileTrue value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropWhileTrue4 +inspect $ 'dropWhileTrue4 `hasNoType` ''Stream.Step +inspect $ 'dropWhileTrue4 `hasNoType` ''Stream.DropWhileState +inspect $ 'dropWhileTrue4 `hasNoType` ''FL.Step +inspect $ 'dropWhileTrue4 `hasNoType` ''SPEC +#endif + +{-# INLINE dropWhileMTrue #-} +dropWhileMTrue :: MonadIO m => Int -> Int -> Stream m Int -> m () +dropWhileMTrue value n = composeN n $ Stream.dropWhileM (return . (<= (value + 1))) + +{-# INLINE dropWhileMTrue4 #-} +dropWhileMTrue4 :: Int -> IO () +dropWhileMTrue4 value = withStream value (dropWhileMTrue value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropWhileMTrue4 +inspect $ 'dropWhileMTrue4 `hasNoType` ''Stream.Step +inspect $ 'dropWhileMTrue4 `hasNoType` ''Stream.DropWhileState +inspect $ 'dropWhileMTrue4 `hasNoType` ''FL.Step +inspect $ 'dropWhileMTrue4 `hasNoType` ''SPEC +#endif + +{-# INLINE dropWhileFalse #-} +dropWhileFalse :: MonadIO m => Int -> Int -> Stream m Int -> m () +dropWhileFalse value n = composeN n $ Stream.dropWhile (> (value + 1)) + +{-# INLINE dropWhileFalse1 #-} +dropWhileFalse1 :: Int -> IO () +dropWhileFalse1 value = withStream value (dropWhileFalse value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropWhileFalse1 +inspect $ 'dropWhileFalse1 `hasNoType` ''Stream.Step +inspect $ 'dropWhileFalse1 `hasNoType` ''Stream.DropWhileState +inspect $ 'dropWhileFalse1 `hasNoType` ''FL.Step +inspect $ 'dropWhileFalse1 `hasNoType` ''SPEC +#endif + +{-# INLINE dropWhileFalse4 #-} +dropWhileFalse4 :: Int -> IO () +dropWhileFalse4 value = withStream value (dropWhileFalse value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'dropWhileFalse4 +inspect $ 'dropWhileFalse4 `hasNoType` ''Stream.Step +inspect $ 'dropWhileFalse4 `hasNoType` ''Stream.DropWhileState +inspect $ 'dropWhileFalse4 `hasNoType` ''FL.Step +inspect $ 'dropWhileFalse4 `hasNoType` ''SPEC +#endif + +{-# INLINE findIndices #-} +findIndices :: MonadIO m => Int -> Int -> Stream m Int -> m () +findIndices value n = composeN n $ Stream.findIndices (== (value + 1)) + +{-# INLINE findIndices1 #-} +findIndices1 :: Int -> IO () +findIndices1 value = withStream value (findIndices value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'findIndices1 +inspect $ 'findIndices1 `hasNoType` ''Stream.Step +inspect $ 'findIndices1 `hasNoType` ''FL.Step +inspect $ 'findIndices1 `hasNoType` ''SPEC +#endif + +{-# INLINE findIndices4 #-} +findIndices4 :: Int -> IO () +findIndices4 value = withStream value (findIndices value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'findIndices4 +inspect $ 'findIndices4 `hasNoType` ''Stream.Step +inspect $ 'findIndices4 `hasNoType` ''FL.Step +inspect $ 'findIndices4 `hasNoType` ''SPEC +#endif + +{-# INLINE elemIndices #-} +elemIndices :: MonadIO m => Int -> Int -> Stream m Int -> m () +elemIndices value n = composeN n $ Stream.elemIndices (value + 1) + +{-# INLINE elemIndices1 #-} +elemIndices1 :: Int -> IO () +elemIndices1 value = withStream value (elemIndices value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'elemIndices1 +inspect $ 'elemIndices1 `hasNoType` ''Stream.Step +inspect $ 'elemIndices1 `hasNoType` ''FL.Step +inspect $ 'elemIndices1 `hasNoType` ''SPEC +#endif + +{-# INLINE elemIndices4 #-} +elemIndices4 :: Int -> IO () +elemIndices4 value = withStream value (elemIndices value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'elemIndices4 +inspect $ 'elemIndices4 `hasNoType` ''Stream.Step +inspect $ 'elemIndices4 `hasNoType` ''FL.Step +inspect $ 'elemIndices4 `hasNoType` ''SPEC +#endif + +{-# INLINE findIndex #-} +findIndex :: Int -> IO (Maybe Int) +findIndex value = withStream value (Stream.head . Stream.findIndices (== (value + 1))) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'findIndex +inspect $ 'findIndex `hasNoType` ''Stream.Step +inspect $ 'findIndex `hasNoType` ''FL.Step +inspect $ 'findIndex `hasNoType` ''SPEC +#endif + +{-# INLINE elemIndex #-} +elemIndex :: Int -> IO (Maybe Int) +elemIndex value = withStream value (Stream.head . Stream.elemIndices (value + 1)) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'elemIndex +inspect $ 'elemIndex `hasNoType` ''Stream.Step +inspect $ 'elemIndex `hasNoType` ''FL.Step +inspect $ 'elemIndex `hasNoType` ''SPEC +#endif + +{-# INLINE deleteBy #-} +deleteBy :: MonadIO m => Int -> Int -> Stream m Int -> m () +deleteBy value n = composeN n $ Stream.deleteBy (>=) (value + 1) + +{-# INLINE deleteBy1 #-} +deleteBy1 :: Int -> IO () +deleteBy1 value = withStream value (deleteBy value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'deleteBy1 +inspect $ 'deleteBy1 `hasNoType` ''Stream.Step +inspect $ 'deleteBy1 `hasNoType` ''FL.Step +inspect $ 'deleteBy1 `hasNoType` ''SPEC +#endif + +{-# INLINE deleteBy4 #-} +deleteBy4 :: Int -> IO () +deleteBy4 value = withStream value (deleteBy value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'deleteBy4 +inspect $ 'deleteBy4 `hasNoType` ''Stream.Step +inspect $ 'deleteBy4 `hasNoType` ''FL.Step +inspect $ 'deleteBy4 `hasNoType` ''SPEC +#endif + +-- uniq . uniq == uniq, composeN 2 ~ composeN 1 +{-# INLINE uniq #-} +uniq :: MonadIO m => Int -> Stream m Int -> m () +uniq n = composeN n Stream.uniq + +{-# INLINE uniq1 #-} +uniq1 :: Int -> IO () +uniq1 value = withStream value (uniq 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'uniq1 +inspect $ 'uniq1 `hasNoType` ''Stream.Step +inspect $ 'uniq1 `hasNoType` ''FL.Step +inspect $ 'uniq1 `hasNoType` ''SPEC +#endif + +{-# INLINE uniq4 #-} +uniq4 :: Int -> IO () +uniq4 value = withStream value (uniq 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'uniq4 +inspect $ 'uniq4 `hasNoType` ''Stream.Step +inspect $ 'uniq4 `hasNoType` ''FL.Step +inspect $ 'uniq4 `hasNoType` ''SPEC +#endif + +{-# INLINE mapMaybe #-} +mapMaybe :: MonadIO m => Int -> Stream m Int -> m () +mapMaybe n = + composeN n $ + Stream.mapMaybe + (\x -> + if odd x + then Nothing + else Just x) + +{-# INLINE mapMaybe1 #-} +mapMaybe1 :: Int -> IO () +mapMaybe1 value = withStream value (mapMaybe 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'mapMaybe1 +inspect $ 'mapMaybe1 `hasNoType` ''Stream.Step +inspect $ 'mapMaybe1 `hasNoType` ''FL.Step +inspect $ 'mapMaybe1 `hasNoType` ''SPEC +#endif + +{-# INLINE mapMaybe4 #-} +mapMaybe4 :: Int -> IO () +mapMaybe4 value = withStream value (mapMaybe 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'mapMaybe4 +inspect $ 'mapMaybe4 `hasNoType` ''Stream.Step +inspect $ 'mapMaybe4 `hasNoType` ''FL.Step +inspect $ 'mapMaybe4 `hasNoType` ''SPEC +#endif + +{-# INLINE mapMaybeM #-} +mapMaybeM :: MonadAsync m => Int -> Stream m Int -> m () +mapMaybeM n = + composeN n $ + Stream.mapMaybeM + (\x -> + if odd x + then return Nothing + else return $ Just x) + +{-# INLINE mapMaybeM1 #-} +mapMaybeM1 :: Int -> IO () +mapMaybeM1 value = withStream value (mapMaybeM 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'mapMaybeM1 +inspect $ 'mapMaybeM1 `hasNoType` ''Stream.Step +inspect $ 'mapMaybeM1 `hasNoType` ''FL.Step +inspect $ 'mapMaybeM1 `hasNoType` ''SPEC +#endif + +{-# INLINE mapMaybeM4 #-} +mapMaybeM4 :: Int -> IO () +mapMaybeM4 value = withStream value (mapMaybeM 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'mapMaybeM4 +inspect $ 'mapMaybeM4 `hasNoType` ''Stream.Step +inspect $ 'mapMaybeM4 `hasNoType` ''FL.Step +inspect $ 'mapMaybeM4 `hasNoType` ''SPEC +#endif + +o_1_space_filtering :: Int -> [Benchmark] +o_1_space_filtering value = + [ bgroup "filtering" + [ benchIO "filter-even" $ filterEven1 value + , benchIO "filter-all-out" $ filterAllOut1 value + , benchIO "filter-all-in" $ filterAllIn1 value + + , benchIO "filterM-even" $ filterMEven1 value + , benchIO "filterM-all-out" $ filterMAllOut1 value + , benchIO "filterM-all-in" $ filterMAllIn1 value + + , benchIO "drop-one" $ dropOne1 value + , benchIO "drop-all" $ dropAll1 value + , benchIO "dropWhile-true" $ dropWhileTrue1 value + -- , benchIO "dropWhileM-true" ... + , benchIO "dropWhile-false" $ dropWhileFalse1 value + , benchIO "deleteBy" $ deleteBy1 value + + , benchIO "uniq" $ uniq1 value + + -- Map and filter + , benchIO "mapMaybe" $ mapMaybe1 value + , benchIO "mapMaybeM" $ mapMaybeM1 value + + -- Searching (stateful map and filter) + , benchIO "findIndices" $ findIndices1 value + , benchIO "elemIndices" $ elemIndices1 value + , benchIO "findIndex" $ findIndex value + , benchIO "elemIndex" $ elemIndex value + ] + ] + +o_1_space_filteringX4 :: Int -> [Benchmark] +o_1_space_filteringX4 value = + [ bgroup "filteringX4" + [ benchIO "filter-even" $ filterEven4 value + , benchIO "filter-all-out" $ filterAllOut4 value + , benchIO "filter-all-in" $ filterAllIn4 value + + , benchIO "filterM-even" $ filterMEven4 value + , benchIO "filterM-all-out" $ filterMAllOut4 value + , benchIO "filterM-all-in" $ filterMAllIn4 value + + , benchIO "drop-one" $ dropOne4 value + , benchIO "drop-all" $ dropAll4 value + , benchIO "dropWhile-true" $ dropWhileTrue4 value + , benchIO "dropWhileM-true" $ dropWhileMTrue4 value + -- XXX requires @-fspec-constr-recursive=12@. + , benchIO "dropWhile-false" $ dropWhileFalse4 value + , benchIO "deleteBy" $ deleteBy4 value + + , benchIO "uniq" $ uniq4 value + + -- map and filter + , benchIO "mapMaybe" $ mapMaybe4 value + , benchIO "mapMaybeM" $ mapMaybeM4 value + + -- searching + , benchIO "findIndices" $ findIndices4 value + , benchIO "elemIndices" $ elemIndices4 value + ] + ] + +------------------------------------------------------------------------------- +-- Size increasing transformations (insertions) +------------------------------------------------------------------------------- + +{-# INLINE intersperse #-} +intersperse :: MonadAsync m => Int -> Int -> Stream m Int -> m () +intersperse value n = composeN n $ Stream.intersperse (value + 1) + +{-# INLINE intersperse1 #-} +intersperse1 :: Int -> IO () +intersperse1 value = withStream value (intersperse value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'intersperse1 +inspect $ 'intersperse1 `hasNoType` ''Stream.Step +inspect $ 'intersperse1 `hasNoType` ''Stream.LoopState +inspect $ 'intersperse1 `hasNoType` ''FL.Step +inspect $ 'intersperse1 `hasNoType` ''SPEC +#endif + +{-# INLINE intersperse4 #-} +intersperse4 :: Int -> IO () +intersperse4 value = withStream value (intersperse value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'intersperse4 +inspect $ 'intersperse4 `hasNoType` ''Stream.Step +-- inspect $ 'intersperse4 `hasNoType` ''Stream.LoopState +inspect $ 'intersperse4 `hasNoType` ''FL.Step +-- inspect $ 'intersperse4 `hasNoType` ''SPEC +#endif + +{-# INLINE intersperseM #-} +intersperseM :: MonadAsync m => Int -> Int -> Stream m Int -> m () +intersperseM value n = composeN n $ Stream.intersperseM (return $ value + 1) + +{-# INLINE intersperseM1 #-} +intersperseM1 :: Int -> IO () +intersperseM1 value = withStream value (intersperseM value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'intersperseM1 +inspect $ 'intersperseM1 `hasNoType` ''Stream.Step +inspect $ 'intersperseM1 `hasNoType` ''Stream.LoopState +inspect $ 'intersperseM1 `hasNoType` ''FL.Step +inspect $ 'intersperseM1 `hasNoType` ''SPEC +#endif + +{-# INLINE insertBy #-} +insertBy :: MonadIO m => Int -> Int -> Stream m Int -> m () +insertBy value n = composeN n $ Stream.insertBy compare (value + 1) + +{-# INLINE insertBy1 #-} +insertBy1 :: Int -> IO () +insertBy1 value = withStream value (insertBy value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'insertBy1 +inspect $ 'insertBy1 `hasNoType` ''Stream.Step +inspect $ 'insertBy1 `hasNoType` ''FL.Step +inspect $ 'insertBy1 `hasNoType` ''SPEC +#endif + +{-# INLINE insertBy4 #-} +insertBy4 :: Int -> IO () +insertBy4 value = withStream value (insertBy value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'insertBy4 +inspect $ 'insertBy4 `hasNoType` ''Stream.Step +inspect $ 'insertBy4 `hasNoType` ''FL.Step +inspect $ 'insertBy4 `hasNoType` ''SPEC +#endif + +{-# INLINE interposeSuffix #-} +interposeSuffix :: Monad m => Int -> Int -> Stream m Int -> m () +interposeSuffix value n = + composeN n $ Stream.unfoldEachSepBy (value + 1) Unfold.identity + +{-# INLINE interposeSuffix1 #-} +interposeSuffix1 :: Int -> IO () +interposeSuffix1 value = withStream value (interposeSuffix value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'interposeSuffix1 +inspect $ 'interposeSuffix1 `hasNoType` ''Stream.Step +inspect $ 'interposeSuffix1 `hasNoType` ''Stream.InterposeState +inspect $ 'interposeSuffix1 `hasNoType` ''FL.Step +inspect $ 'interposeSuffix1 `hasNoType` ''SPEC +#endif + +{-# INLINE intercalateSuffix #-} +intercalateSuffix :: Monad m => Int -> Int -> Stream m Int -> m () +intercalateSuffix value n = + composeN n $ Stream.unfoldEachSepBySeq (value + 1) Unfold.identity + +{-# INLINE intercalateSuffix1 #-} +intercalateSuffix1 :: Int -> IO () +intercalateSuffix1 value = withStream value (intercalateSuffix value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'intercalateSuffix1 +inspect $ 'intercalateSuffix1 `hasNoType` ''Stream.Step +inspect $ 'intercalateSuffix1 `hasNoType` ''Stream.LoopState +inspect $ 'intercalateSuffix1 `hasNoType` ''Producer.ConcatState +inspect $ 'intercalateSuffix1 `hasNoType` ''FL.Step +inspect $ 'intercalateSuffix1 `hasNoType` ''SPEC +#endif + +o_1_space_inserting :: Int -> [Benchmark] +o_1_space_inserting value = + [ bgroup "inserting" + [ benchIO "intersperse" $ intersperse1 value + , benchIO "intersperseM" $ intersperseM1 value + , benchIO "insertBy" $ insertBy1 value + , benchIO "interposeSuffix" $ interposeSuffix1 value + , benchIO "intercalateSuffix" $ intercalateSuffix1 value + ] + ] + +o_1_space_insertingX4 :: Int -> [Benchmark] +o_1_space_insertingX4 value = + [ bgroup "insertingX4" + [ + -- XXX requires @-fspec-constr-recursive=16@. + benchIO "intersperse" $ intersperse4 value + , benchIO "insertBy" $ insertBy4 value + ] + ] + +------------------------------------------------------------------------------- +-- Indexing +------------------------------------------------------------------------------- + +{-# INLINE indexed #-} +indexed :: MonadIO m => Int -> Stream m Int -> m () +indexed n = composeN n (fmap snd . Stream.indexed) + +{-# INLINE indexed1 #-} +indexed1 :: Int -> IO () +indexed1 value = withStream value (indexed 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'indexed1 +inspect $ 'indexed1 `hasNoType` ''Stream.Step +inspect $ 'indexed1 `hasNoType` ''FL.Step +inspect $ 'indexed1 `hasNoType` ''SPEC +#endif + +{-# INLINE indexed4 #-} +indexed4 :: Int -> IO () +indexed4 value = withStream value (indexed 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'indexed4 +inspect $ 'indexed4 `hasNoType` ''Stream.Step +inspect $ 'indexed4 `hasNoType` ''FL.Step +inspect $ 'indexed4 `hasNoType` ''SPEC +#endif + +{-# INLINE indexedR #-} +indexedR :: MonadIO m => Int -> Int -> Stream m Int -> m () +indexedR value n = composeN n (fmap snd . Stream.indexedR value) + +{-# INLINE indexedR1 #-} +indexedR1 :: Int -> IO () +indexedR1 value = withStream value (indexedR value 1) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'indexedR1 +inspect $ 'indexedR1 `hasNoType` ''Stream.Step +inspect $ 'indexedR1 `hasNoType` ''FL.Step +inspect $ 'indexedR1 `hasNoType` ''SPEC +#endif + +{-# INLINE indexedR4 #-} +indexedR4 :: Int -> IO () +indexedR4 value = withStream value (indexedR value 4) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'indexedR4 +inspect $ 'indexedR4 `hasNoType` ''Stream.Step +inspect $ 'indexedR4 `hasNoType` ''FL.Step +inspect $ 'indexedR4 `hasNoType` ''SPEC +#endif + +o_1_space_indexing :: Int -> [Benchmark] +o_1_space_indexing value = + [ bgroup "indexing" + [ benchIO "indexed" $ indexed1 value + , benchIO "indexedR" $ indexedR1 value + ] + ] + +o_1_space_indexingX4 :: Int -> [Benchmark] +o_1_space_indexingX4 value = + [ bgroup "indexingx4" + [ benchIO "indexed" $ indexed4 value + , benchIO "indexedR" $ indexedR4 value + ] + ] + +------------------------------------------------------------------------------- +-- Main +------------------------------------------------------------------------------- + +-- In addition to gauge options, the number of elements in the stream can be +-- passed using the --stream-size option. +-- +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = + map (SpaceO_1,) (Prelude.concat + [ o_1_space_mapping size + , o_1_space_mappingX4 size + , o_1_space_filtering size + , o_1_space_filteringX4 size + , o_1_space_inserting size + , o_1_space_insertingX4 size + , o_1_space_indexing size + , o_1_space_indexingX4 size + ]) + ++ map (SpaceO_n,) (Prelude.concat + [ + o_n_space_mapping size + , o_n_space_iterated size + ]) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs similarity index 99% rename from benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs rename to benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs index bad8bd3cd7..dffc939692 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform2.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs @@ -20,7 +20,7 @@ {-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} #endif -module Stream.Transform2 (benchmarks) where +module Stream.Transform.Composed (benchmarks) where #ifdef INSPECTION import GHC.Types (SPEC(..)) @@ -34,7 +34,9 @@ import Streamly.Internal.Data.Stream (Stream) import System.Random (randomRIO) import qualified Stream.Common as Common +#ifdef INSPECTION import qualified Streamly.Internal.Data.Fold as FL +#endif import qualified Streamly.Internal.Data.Pipe as Pipe import qualified Streamly.Internal.Data.Scan as Scan import qualified Streamly.Internal.Data.Stream as S diff --git a/benchmark/streamly-benchmarks.cabal b/benchmark/streamly-benchmarks.cabal index 9f1393b36f..234a11b911 100644 --- a/benchmark/streamly-benchmarks.cabal +++ b/benchmark/streamly-benchmarks.cabal @@ -442,11 +442,13 @@ benchmark Data.Stream Stream.Generate Stream.Lift Stream.Parse - Stream.Transform2 - Stream.ParseSplit + Stream.Parse.Group + Stream.Parse.Split -- XXX uses lot of memory - Stream.ParseSplitChunks + Stream.Parse.SplitChunks Stream.Transform + Stream.Transform.Basic + Stream.Transform.Composed Stream.Type if flag(limit-build-mem) if flag(dev) From 6378c6119c11052ff615b4e7f79cb46875bddf20 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 17:31:11 +0530 Subject: [PATCH 06/15] Move misclassified across Transform.Basic and Transform.Composed --- .../Benchmark/Data/Stream/Transform/Basic.hs | 67 ++++++++++-------- .../Data/Stream/Transform/Composed.hs | 69 +++++++++---------- 2 files changed, 68 insertions(+), 68 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs index 6e9c40de35..8a86db3ac8 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs @@ -43,7 +43,7 @@ import qualified Streamly.Internal.Data.Unfold as Unfold import Test.Tasty.Bench import Stream.Common hiding (scanl', benchIO) import Streamly.Benchmark.Common -import Prelude hiding (sequence, mapM) +import Prelude hiding (sequence, mapM, reverse) ------------------------------------------------------------------------------- -- Helpers @@ -363,30 +363,6 @@ o_1_space_mappingX4 value = ] ] -{-# INLINE sieveScan #-} -sieveScan :: Monad m => Stream m Int -> Stream m Int -sieveScan = - Stream.mapMaybe snd - . Stream.scanl (Scanl.scanlM' (\(primes, _) n -> do - return $ - let ps = takeWhile (\p -> p * p <= n) primes - in if all (\p -> n `mod` p /= 0) ps - then (primes ++ [n], Just n) - else (primes, Nothing)) (return ([2], Just 2))) - -{-# INLINE naivePrimeSieve #-} -naivePrimeSieve :: Int -> IO Int -naivePrimeSieve value = - withRandomIntIO $ \n -> - Stream.fold FL.sum $ sieveScan $ Stream.enumerateFromTo 2 (value + n) - -o_n_space_mapping :: Int -> [Benchmark] -o_n_space_mapping value = - [ bgroup "mapping" - [ benchIO "naive prime sieve" $ naivePrimeSieve value - ] - ] - ------------------------------------------------------------------------------- -- Iteration/looping utilities ------------------------------------------------------------------------------- @@ -1194,6 +1170,40 @@ o_1_space_indexingX4 value = ] ] +------------------------------------------------------------------------------- +-- Size conserving transformations (reordering, buffering, etc.) +------------------------------------------------------------------------------- + +{-# INLINE reverse #-} +reverse :: Int -> IO () +reverse value = withStream value (composeN 1 Stream.reverse) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'reverse +inspect $ 'reverse `hasNoType` ''Stream.Step +inspect $ 'reverse `hasNoType` ''FL.Step +-- inspect $ 'reverse `hasNoType` ''SPEC +#endif + +{-# INLINE reverse' #-} +reverse' :: Int -> IO () +reverse' value = withStream value (composeN 1 Stream.reverseUnbox) + +#ifdef INSPECTION +inspect $ hasNoTypeClasses 'reverse' +-- inspect $ 'reverse' `hasNoType` ''Stream.Step +#endif + +o_n_heap_buffering :: Int -> [Benchmark] +o_n_heap_buffering value = + [ bgroup "buffered" + [ + -- Reversing a stream + benchIO "reverse" $ reverse value + , benchIO "reverse'" $ reverse' value + ] + ] + ------------------------------------------------------------------------------- -- Main ------------------------------------------------------------------------------- @@ -1213,8 +1223,5 @@ benchmarks size = , o_1_space_indexing size , o_1_space_indexingX4 size ]) - ++ map (SpaceO_n,) (Prelude.concat - [ - o_n_space_mapping size - , o_n_space_iterated size - ]) + ++ map (SpaceO_n,) (o_n_space_iterated size) + ++ map (HeapO_n,) (o_n_heap_buffering size) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs index dffc939692..2ad08602f4 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs @@ -34,10 +34,9 @@ import Streamly.Internal.Data.Stream (Stream) import System.Random (randomRIO) import qualified Stream.Common as Common -#ifdef INSPECTION import qualified Streamly.Internal.Data.Fold as FL -#endif import qualified Streamly.Internal.Data.Pipe as Pipe +import qualified Streamly.Internal.Data.Scanl as Scanl import qualified Streamly.Internal.Data.Scan as Scan import qualified Streamly.Internal.Data.Stream as S import qualified Streamly.Internal.Data.Stream as Stream @@ -45,7 +44,7 @@ import qualified Streamly.Internal.Data.Stream as Stream import Test.Tasty.Bench import Streamly.Benchmark.Common import Stream.Common hiding (benchIO) -import Prelude hiding (reverse, tail) +import Prelude hiding (tail) -- Apply transformation g count times on a stream of length len {-# INLINE iterateSource #-} @@ -75,40 +74,6 @@ withStream value f = withRandomIntIO (f . sourceUnfoldrM value) benchIO :: NFData b => String -> IO b -> Benchmark benchIO name = bench name . nfIO -------------------------------------------------------------------------------- --- Size conserving transformations (reordering, buffering, etc.) -------------------------------------------------------------------------------- - -{-# INLINE reverse #-} -reverse :: Int -> IO () -reverse value = withStream value (composeN 1 S.reverse) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'reverse -inspect $ 'reverse `hasNoType` ''S.Step -inspect $ 'reverse `hasNoType` ''FL.Step --- inspect $ 'reverse `hasNoType` ''SPEC -#endif - -{-# INLINE reverse' #-} -reverse' :: Int -> IO () -reverse' value = withStream value (composeN 1 S.reverseUnbox) - -#ifdef INSPECTION -inspect $ hasNoTypeClasses 'reverse' --- inspect $ 'reverse' `hasNoType` ''S.Step -#endif - -o_n_heap_buffering :: Int -> [Benchmark] -o_n_heap_buffering value = - [ bgroup "buffered" - [ - -- Reversing a stream - benchIO "reverse" $ reverse value - , benchIO "reverse'" $ reverse' value - ] - ] - ------------------------------------------------------------------------------- -- Mixed Transformation ------------------------------------------------------------------------------- @@ -926,6 +891,34 @@ o_1_space_scansX4 value = ] ] +------------------------------------------------------------------------------- +-- Composed transformations (scan + mapMaybe) +------------------------------------------------------------------------------- + +{-# INLINE sieveScan #-} +sieveScan :: Monad m => Stream m Int -> Stream m Int +sieveScan = + Stream.mapMaybe snd + . Stream.scanl (Scanl.scanlM' (\(primes, _) n -> do + return $ + let ps = takeWhile (\p -> p * p <= n) primes + in if all (\p -> n `mod` p /= 0) ps + then (primes ++ [n], Just n) + else (primes, Nothing)) (return ([2], Just 2))) + +{-# INLINE naivePrimeSieve #-} +naivePrimeSieve :: Int -> IO Int +naivePrimeSieve value = + withRandomIntIO $ \n -> + Stream.fold FL.sum $ sieveScan $ Stream.enumerateFromTo 2 (value + n) + +o_n_space_mapping :: Int -> [Benchmark] +o_n_space_mapping value = + [ bgroup "mapping" + [ benchIO "naive prime sieve" $ naivePrimeSieve value + ] + ] + ------------------------------------------------------------------------------- -- Main ------------------------------------------------------------------------------- @@ -949,4 +942,4 @@ benchmarks size = , o_1_space_scansX4 size ]) ++ map (StackO_n,) (o_n_stack_iterated size) - ++ map (HeapO_n,) (o_n_heap_buffering size) + ++ map (SpaceO_n,) (o_n_space_mapping size) From d9d96805aaf44950d28dc0fcb4785c43a6ef94f3 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 17:45:11 +0530 Subject: [PATCH 07/15] Reuse functions from Stream/Type.hs in all benchmarks --- .../Benchmark/Data/Stream/Generate.hs | 15 +------------ .../Streamly/Benchmark/Data/Stream/Lift.hs | 12 +---------- .../Streamly/Benchmark/Data/Stream/Nesting.hs | 12 +---------- .../Benchmark/Data/Stream/Parse/Group.hs | 21 +------------------ .../Benchmark/Data/Stream/Transform/Basic.hs | 19 +---------------- .../Data/Stream/Transform/Composed.hs | 14 +------------ 6 files changed, 6 insertions(+), 87 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs index bf760ae81a..906388943a 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Generate.hs @@ -26,32 +26,19 @@ import Test.Inspection import qualified Streamly.Internal.Data.Fold as Fold #endif -import Control.DeepSeq (NFData(..)) import Control.Monad.IO.Class (MonadIO) import Streamly.Internal.Data.Stream (Stream) import Streamly.Internal.Data.Time.Units (AbsTime) -import System.Random (randomRIO) import qualified Streamly.Internal.Data.Stream as Stream import Test.Tasty.Bench import Stream.Common hiding (benchIO) +import Stream.Type (benchIO, withDrain) import Streamly.Benchmark.Common import Prelude hiding (repeat, replicate, iterate) -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withDrain #-} -withDrain :: (Int -> Stream IO a) -> IO () -withDrain f = withRandomIntIO $ \n -> drain (f n) - -- XXX should we use rnf to evaluate the result? {-# INLINE fromListM #-} fromListM :: Monad m => [m a] -> Stream m a diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs b/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs index 225984680a..24ed58209c 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Lift.hs @@ -20,12 +20,10 @@ module Stream.Lift (benchmarks) where -import Control.DeepSeq (NFData(..)) import Control.Monad.State.Strict (StateT, get, put) import Data.Functor.Identity (Identity(..), runIdentity) import Stream.Common (sourceUnfoldr, sourceUnfoldrM) -import Stream.Type (withPureStream) -import System.Random (randomRIO) +import Stream.Type (benchIO, withPureStream, withRandomIntIO) import Streamly.Internal.Data.Stream (Stream) import qualified Stream.Common as Common @@ -43,14 +41,6 @@ import GHC.Types (SPEC(..)) import Test.Inspection #endif -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - ------------------------------------------------------------------------------- -- Monad transformation (hoisting etc.) ------------------------------------------------------------------------------- diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs b/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs index 34f56fccd9..b6f6f17512 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Nesting.hs @@ -36,23 +36,13 @@ import qualified Streamly.Internal.Data.Unfold as Unfold import qualified Streamly.Internal.Data.Stream as Stream import qualified Streamly.Internal.Data.StreamK as StreamK -import Control.DeepSeq (NFData(..)) -import System.Random (randomRIO) - import Test.Tasty.Bench import Stream.Common hiding (benchIO) +import Stream.Type (benchIO, withRandomIntIO) import Streamly.Benchmark.Common import qualified Stream.Type as Type import Prelude hiding (concatMap, zipWith) -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - ------------------------------------------------------------------------------- -- Multi-Stream ------------------------------------------------------------------------------- diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs index 7563366d10..897a590743 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Group.hs @@ -24,10 +24,7 @@ import GHC.Types (SPEC(..)) import Test.Inspection #endif -import Control.DeepSeq (NFData(..)) import Data.Monoid (Sum(..)) -import Streamly.Internal.Data.Stream (Stream) -import System.Random (randomRIO) import qualified Stream.Common as Common import qualified Streamly.Internal.Data.Fold as FL @@ -35,23 +32,7 @@ import qualified Streamly.Internal.Data.Stream as S import Test.Tasty.Bench import Streamly.Benchmark.Common -import Stream.Common hiding (benchIO) - -------------------------------------------------------------------------------- --- Helpers -------------------------------------------------------------------------------- - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = withRandomIntIO (f . sourceUnfoldrM value) +import Stream.Type (benchIO, withStream) ------------------------------------------------------------------------------- -- Grouping transformations diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs index 8a86db3ac8..48de62216b 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Basic.hs @@ -29,10 +29,8 @@ import Test.Inspection import qualified Streamly.Internal.Data.Producer as Producer #endif -import Control.DeepSeq (NFData(..)) import Control.Monad.IO.Class (MonadIO(..)) import Streamly.Internal.Data.Stream (Stream) -import System.Random (randomRIO) import qualified Stream.Common as Common import qualified Streamly.Internal.Data.Fold as FL @@ -42,25 +40,10 @@ import qualified Streamly.Internal.Data.Unfold as Unfold import Test.Tasty.Bench import Stream.Common hiding (scanl', benchIO) +import Stream.Type (benchIO, withRandomIntIO, withStream) import Streamly.Benchmark.Common import Prelude hiding (sequence, mapM, reverse) -------------------------------------------------------------------------------- --- Helpers -------------------------------------------------------------------------------- - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = withRandomIntIO (f . sourceUnfoldrM value) - ------------------------------------------------------------------------------- -- Pipelines (stream-to-stream transformations) ------------------------------------------------------------------------------- diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs index 2ad08602f4..4a1c23ce3c 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Transform/Composed.hs @@ -31,7 +31,6 @@ import Control.DeepSeq (NFData(..)) import Control.Monad.IO.Class (MonadIO(..)) import GHC.Generics (Generic) import Streamly.Internal.Data.Stream (Stream) -import System.Random (randomRIO) import qualified Stream.Common as Common import qualified Streamly.Internal.Data.Fold as FL @@ -44,6 +43,7 @@ import qualified Streamly.Internal.Data.Stream as Stream import Test.Tasty.Bench import Streamly.Benchmark.Common import Stream.Common hiding (benchIO) +import Stream.Type (benchIO, withRandomIntIO, withStream) import Prelude hiding (tail) -- Apply transformation g count times on a stream of length len @@ -62,18 +62,6 @@ iterateSource g count len n = f count (sourceUnfoldrM len n) f (0 :: Int) stream = stream f i stream = f (i - 1) (g stream) -{-# INLINE withRandomIntIO #-} -withRandomIntIO :: (Int -> IO b) -> IO b -withRandomIntIO f = randomRIO (1, 1 :: Int) >>= f - -{-# INLINE withStream #-} -withStream :: Int -> (Stream IO Int -> IO b) -> IO b -withStream value f = withRandomIntIO (f . sourceUnfoldrM value) - -{-# INLINE benchIO #-} -benchIO :: NFData b => String -> IO b -> Benchmark -benchIO name = bench name . nfIO - ------------------------------------------------------------------------------- -- Mixed Transformation ------------------------------------------------------------------------------- From 1577391767c9a61568b401e0132ad9998e06a1f0 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 18:08:09 +0530 Subject: [PATCH 08/15] Breakup unfold benchmarks into modules mirroring the source --- benchmark/Streamly/Benchmark/Data/Unfold.hs | 1026 +--------------- .../Benchmark/Data/Unfold/Enumeration.hs | 154 +++ .../Streamly/Benchmark/Data/Unfold/Type.hs | 1075 +++++++++++++++++ benchmark/streamly-benchmarks.cabal | 7 +- 4 files changed, 1253 insertions(+), 1009 deletions(-) create mode 100644 benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs create mode 100644 benchmark/Streamly/Benchmark/Data/Unfold/Type.hs diff --git a/benchmark/Streamly/Benchmark/Data/Unfold.hs b/benchmark/Streamly/Benchmark/Data/Unfold.hs index df3a920f28..320730953a 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold.hs @@ -25,9 +25,8 @@ module Main (main) where import Control.DeepSeq (NFData(..)) -import Control.Exception (SomeException, ErrorCall, try) +import Control.Exception (SomeException) import Data.Char (ord) -import qualified Data.Tuple as Tuple import Data.Word (Word8) import Streamly.Internal.Data.Unfold (Unfold) import System.IO (Handle, hClose) @@ -41,6 +40,9 @@ import qualified Streamly.Internal.Data.Unfold as UF import qualified Streamly.Internal.Data.Stream as S import qualified Streamly.Internal.Data.StreamK as K +import qualified Unfold.Enumeration as Enumeration +import qualified Unfold.Type as Type + import Test.Tasty.Bench hiding (env) import Prelude hiding (take, filter, zipWith, map, mapM, takeWhile, scanl, repeat, dropWhile) import Streamly.Benchmark.Common @@ -86,94 +88,10 @@ drainTransformationDefault :: drainTransformationDefault to = drainTransformation (UF.supplySecond to UF.enumerateFromToIntegral) -{-# INLINE drainProduct #-} -drainProduct :: - Monad m - => Unfold m a b - -> Unfold m c d - -> (Unfold m a b -> Unfold m c d -> Unfold m e f) - -> e - -> m () -drainProduct unf1 unf2 f seed = drainGeneration (f unf1 unf2) seed - -{-# INLINE drainProductDefault #-} -drainProductDefault :: - Monad m - => Int - -> (Unfold m Int Int -> Unfold m Int Int -> Unfold m e f) - -> e - -> m () -drainProductDefault to = drainProduct src src - - where - - src = UF.supplySecond to UF.enumerateFromToIntegral - ------------------------------------------------------------------------------- -- Operations on input ------------------------------------------------------------------------------- -{-# INLINE lmap #-} -lmap :: Int -> Int -> IO () -lmap size start = - drainTransformationDefault (size + start) (UF.lmap (+ 1)) start - -#ifdef INSPECTION -inspect $ 'lmap `hasNoType` ''S.Step -inspect $ 'lmap `hasNoType` ''FL.Step -inspect $ 'lmap `hasNoType` ''SPEC -#endif - -{-# INLINE lmapM #-} -lmapM :: Int -> Int -> IO () -lmapM size start = - drainTransformationDefault (size + start) (UF.lmapM (return . (+) 1)) start - -#ifdef INSPECTION -inspect $ 'lmapM `hasNoType` ''S.Step -inspect $ 'lmapM `hasNoType` ''FL.Step -inspect $ 'lmapM `hasNoType` ''SPEC -#endif - -{-# INLINE both #-} -both :: Int -> Int -> IO () -both size start = - drainTransformationDefault (size + start) (UF.supply start) () - -#ifdef INSPECTION -inspect $ 'both `hasNoType` ''S.Step -inspect $ 'both `hasNoType` ''FL.Step -inspect $ 'both `hasNoType` ''SPEC -#endif - -{-# INLINE first #-} -first :: Int -> Int -> IO () -first size start = - drainTransformation - (UF.take size UF.enumerateFromThenIntegral) - (UF.supplyFirst start) - 1 - -#ifdef INSPECTION -inspect $ 'first `hasNoType` ''S.Step -inspect $ 'first `hasNoType` ''FL.Step -inspect $ 'first `hasNoType` ''SPEC -#endif - -{-# INLINE second #-} -second :: Int -> Int -> IO () -second size start = - drainTransformation - (UF.take size UF.enumerateFromThenIntegral) - (UF.supplySecond 1) - start - -#ifdef INSPECTION -inspect $ 'second `hasNoType` ''S.Step -inspect $ 'second `hasNoType` ''FL.Step -inspect $ 'second `hasNoType` ''SPEC -#endif - {-# INLINE discardFirst #-} discardFirst :: Int -> Int -> IO () discardFirst size start = @@ -196,44 +114,6 @@ inspect $ 'discardSecond `hasNoType` ''FL.Step inspect $ 'discardSecond `hasNoType` ''SPEC #endif -{-# INLINE consInput #-} -consInput :: Int -> Int -> IO () -consInput size start = - drainTransformationDefault (size + start) UF.consInput start - -#ifdef INSPECTION -inspect $ 'consInput `hasNoType` ''S.Step -inspect $ 'consInput `hasNoType` ''FL.Step -inspect $ 'consInput `hasNoType` ''SPEC -inspect $ 'consInput `hasNoType` ''UF.ConsInputState -#endif - -{-# INLINE consInputWith #-} -consInputWith :: Int -> Int -> IO () -consInputWith size start = - drainTransformationDefault (size + start) (UF.consInputWith (+1)) start - -#ifdef INSPECTION -inspect $ 'consInputWith `hasNoType` ''S.Step -inspect $ 'consInputWith `hasNoType` ''FL.Step -inspect $ 'consInputWith `hasNoType` ''SPEC -inspect $ 'consInputWith `hasNoType` ''UF.ConsInputState -#endif - -{-# INLINE swap #-} -swap :: Int -> Int -> IO () -swap size start = - drainTransformation - (UF.take size UF.enumerateFromThenIntegral) - (UF.lmap Tuple.swap) - (1, start) - -#ifdef INSPECTION -inspect $ 'swap `hasNoType` ''S.Step -inspect $ 'swap `hasNoType` ''FL.Step -inspect $ 'swap `hasNoType` ''SPEC -#endif - ------------------------------------------------------------------------------- -- Stream generation ------------------------------------------------------------------------------- @@ -311,148 +191,13 @@ inspect $ 'consM `hasNoType` ''FL.Step inspect $ 'consM `hasNoType` ''SPEC #endif --- 'functionM', 'function', 'identity' and 'fromEffect' generate a single --- element per seed, so to process ~value elements we unfold them over an outer --- source of value seeds. -{-# INLINE functionM #-} -functionM :: Int -> Int -> IO () -functionM value start = - drainGeneration - (UF.unfoldEach (UF.functionM return) (source (start + value))) start - -#ifdef INSPECTION -inspect $ 'functionM `hasNoType` ''S.Step -inspect $ 'functionM `hasNoType` ''FL.Step -inspect $ 'functionM `hasNoType` ''SPEC -inspect $ 'functionM `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE function #-} -function :: Int -> Int -> IO () -function value start = - drainGeneration - (UF.unfoldEach (UF.function id) (source (start + value))) start - -#ifdef INSPECTION -inspect $ 'function `hasNoType` ''S.Step -inspect $ 'function `hasNoType` ''FL.Step -inspect $ 'function `hasNoType` ''SPEC -inspect $ 'function `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE identity #-} -identity :: Int -> Int -> IO () -identity value start = - drainGeneration (UF.unfoldEach UF.identity (source (start + value))) start - -#ifdef INSPECTION -inspect $ 'identity `hasNoType` ''S.Step -inspect $ 'identity `hasNoType` ''FL.Step -inspect $ 'identity `hasNoType` ''SPEC -inspect $ 'identity `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE fromEffect #-} -fromEffect :: Int -> Int -> IO () -fromEffect value start = - drainGeneration - (UF.unfoldEach (UF.fromEffect (return start)) (source (start + value))) - start - -#ifdef INSPECTION -inspect $ 'fromEffect `hasNoType` ''S.Step -inspect $ 'fromEffect `hasNoType` ''FL.Step -inspect $ 'fromEffect `hasNoType` ''SPEC -inspect $ 'fromEffect `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE fromPure #-} -fromPure :: Int -> Int -> IO () -fromPure value start = - drainGeneration - (UF.unfoldEach (UF.fromPure start) (source (start + value))) - start - -#ifdef INSPECTION -inspect $ 'fromPure `hasNoType` ''S.Step -inspect $ 'fromPure `hasNoType` ''FL.Step -inspect $ 'fromPure `hasNoType` ''SPEC -inspect $ 'fromPure `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE functionMaybeM #-} -functionMaybeM :: Int -> Int -> IO () -functionMaybeM value start = - drainGeneration - (UF.unfoldEach (UF.functionMaybeM (return . Just)) (source (start + value))) - start - -#ifdef INSPECTION -inspect $ 'functionMaybeM `hasNoType` ''S.Step -inspect $ 'functionMaybeM `hasNoType` ''FL.Step -inspect $ 'functionMaybeM `hasNoType` ''SPEC -inspect $ 'functionMaybeM `hasNoType` ''Producer.ConcatState -#endif - --- 'fromTuple' generates two elements per seed, so unfold it over value/2 tuples --- to emit and drain ~value elements. -{-# INLINE fromTuple #-} -fromTuple :: Int -> Int -> IO () -fromTuple value start = - let outer = UF.map (\i -> (i, i)) (source (start + value `div` 2)) - in drainGeneration (UF.unfoldEach UF.fromTuple outer) start - -#ifdef INSPECTION -inspect $ 'fromTuple `hasNoType` ''S.Step -inspect $ 'fromTuple `hasNoType` ''FL.Step -inspect $ 'fromTuple `hasNoType` ''SPEC -inspect $ 'fromTuple `hasNoType` ''Producer.ConcatState -inspect $ 'fromTuple `hasNoType` ''Producer.TupleState -#endif - -{-# INLINE sourceUnfoldrM #-} -sourceUnfoldrM :: Monad m => Int -> Int -> Unfold m Int Int -sourceUnfoldrM size start = UF.unfoldrM step - - where - - step i = - return - $ if i < start + size - then Just (i, i + 1) - else Nothing - -{-# INLINE unfoldrM #-} -unfoldrM :: Int -> Int -> IO () -unfoldrM size start = drainGeneration (sourceUnfoldrM size start) start - -#ifdef INSPECTION -inspect $ 'unfoldrM `hasNoType` ''S.Step -inspect $ 'unfoldrM `hasNoType` ''FL.Step -inspect $ 'unfoldrM `hasNoType` ''SPEC -#endif - -{-# INLINE unfoldr #-} -unfoldr :: Int -> Int -> IO () -unfoldr size start = drainGeneration (UF.unfoldr step) start - where - step i = if i < start + size then Just (i, i + 1) else Nothing - -#ifdef INSPECTION -inspect $ 'unfoldr `hasNoType` ''S.Step -inspect $ 'unfoldr `hasNoType` ''FL.Step -inspect $ 'unfoldr `hasNoType` ''SPEC -#endif - -{-# INLINE fromList #-} -fromList :: Int -> Int -> IO () -fromList size start = drainGeneration UF.fromList [start .. start + size] +{-# INLINE _fromSVar #-} +_fromSVar :: Int -> Int -> m () +_fromSVar = undefined -#ifdef INSPECTION -inspect $ 'fromList `hasNoType` ''S.Step -inspect $ 'fromList `hasNoType` ''FL.Step -inspect $ 'fromList `hasNoType` ''SPEC -#endif +{-# INLINE _fromProducer #-} +_fromProducer :: Int -> Int -> m () +_fromProducer = undefined {-# INLINE fromListM #-} fromListM :: Int -> Int -> IO () @@ -465,14 +210,6 @@ inspect $ 'fromListM `hasNoType` ''FL.Step inspect $ 'fromListM `hasNoType` ''SPEC #endif -{-# INLINE _fromSVar #-} -_fromSVar :: Int -> Int -> m () -_fromSVar = undefined - -{-# INLINE _fromProducer #-} -_fromProducer :: Int -> Int -> m () -_fromProducer = undefined - {-# INLINE replicateM #-} replicateM :: Int -> Int -> IO () replicateM size start = drainGeneration UF.replicateM (size, return start) @@ -525,81 +262,6 @@ inspect $ 'fromIndicesM `hasNoType` ''FL.Step inspect $ 'fromIndicesM `hasNoType` ''SPEC #endif -{-# INLINE enumerateFromThenIntegral #-} -enumerateFromThenIntegral :: Int -> Int -> IO () -enumerateFromThenIntegral size start = - drainGeneration (UF.take size UF.enumerateFromThenIntegral) (start, 1) - -#ifdef INSPECTION -inspect $ 'enumerateFromThenIntegral `hasNoType` ''S.Step -inspect $ 'enumerateFromThenIntegral `hasNoType` ''FL.Step -inspect $ 'enumerateFromThenIntegral `hasNoType` ''SPEC -#endif - -{-# INLINE enumerateFromToIntegral #-} -enumerateFromToIntegral :: Int -> Int -> IO () -enumerateFromToIntegral size start = - drainGeneration - ( UF.supplySecond - (size + start) - UF.enumerateFromToIntegral - ) start - -#ifdef INSPECTION -inspect $ 'enumerateFromToIntegral `hasNoType` ''S.Step -inspect $ 'enumerateFromToIntegral `hasNoType` ''FL.Step -inspect $ 'enumerateFromToIntegral `hasNoType` ''SPEC -#endif - -{-# INLINE enumerateFromIntegral #-} -enumerateFromIntegral :: Int -> Int -> IO () -enumerateFromIntegral size start = - drainGeneration (UF.take size UF.enumerateFromIntegral) start - -#ifdef INSPECTION -inspect $ 'enumerateFromIntegral `hasNoType` ''S.Step -inspect $ 'enumerateFromIntegral `hasNoType` ''FL.Step -inspect $ 'enumerateFromIntegral `hasNoType` ''SPEC -#endif - -{-# INLINE enumerateFromStepNum #-} -enumerateFromStepNum :: Int -> Int -> IO () -enumerateFromStepNum size start = - drainGeneration (UF.take size (UF.enumerateFromThenNum)) (start, 1) - -#ifdef INSPECTION -inspect $ 'enumerateFromStepNum `hasNoType` ''S.Step -inspect $ 'enumerateFromStepNum `hasNoType` ''FL.Step -inspect $ 'enumerateFromStepNum `hasNoType` ''SPEC -#endif - -{-# INLINE enumerateFromNum #-} -enumerateFromNum :: Int -> Int -> IO () -enumerateFromNum size start = drainGeneration (UF.take size UF.enumerateFromNum) start - -#ifdef INSPECTION -inspect $ 'enumerateFromNum `hasNoType` ''S.Step -inspect $ 'enumerateFromNum `hasNoType` ''FL.Step -inspect $ 'enumerateFromNum `hasNoType` ''SPEC -#endif - -{-# INLINE enumerateFromToFractional #-} -enumerateFromToFractional :: Int -> Int -> IO () -enumerateFromToFractional size start = - let intToDouble x = (fromInteger (fromIntegral x)) :: Double - in drainGeneration - ( UF.supplySecond - (intToDouble $ start + size) - UF.enumerateFromToFractional - ) - (intToDouble start) - -#ifdef INSPECTION -inspect $ 'enumerateFromToFractional `hasNoType` ''S.Step -inspect $ 'enumerateFromToFractional `hasNoType` ''FL.Step -inspect $ 'enumerateFromToFractional `hasNoType` ''SPEC -#endif - ------------------------------------------------------------------------------- -- Stream transformation ------------------------------------------------------------------------------- @@ -615,41 +277,6 @@ inspect $ 'postscan `hasNoType` ''FL.Step inspect $ 'postscan `hasNoType` ''SPEC #endif -{-# INLINE map #-} -map :: Int -> Int -> IO () -map size start = drainTransformationDefault (size + start) (UF.map (+1)) start - -#ifdef INSPECTION -inspect $ 'map `hasNoType` ''S.Step -inspect $ 'map `hasNoType` ''FL.Step -inspect $ 'map `hasNoType` ''SPEC -#endif - -{-# INLINE mapM #-} -mapM :: Int -> Int -> IO () -mapM size start = - drainTransformationDefault (size + start) (UF.mapM (return . (+) 1)) start - -#ifdef INSPECTION -inspect $ 'mapM `hasNoType` ''S.Step -inspect $ 'mapM `hasNoType` ''FL.Step -inspect $ 'mapM `hasNoType` ''SPEC -#endif - -{-# INLINE mapM2 #-} -mapM2 :: Int -> Int -> IO () -mapM2 size start = - drainTransformationDefault - size - (UF.mapM (\(a, b) -> return $ a + b) . UF.carryInput) - start - -#ifdef INSPECTION -inspect $ 'mapM2 `hasNoType` ''S.Step -inspect $ 'mapM2 `hasNoType` ''FL.Step -inspect $ 'mapM2 `hasNoType` ''SPEC -#endif - {-# INLINE scanl #-} scanl :: Int -> Int -> IO () scanl size start = @@ -676,34 +303,6 @@ inspect $ 'scanlMany `hasNoType` ''SPEC -- Stream filtering ------------------------------------------------------------------------------- -{-# INLINE takeWhileM #-} -takeWhileM :: Int -> Int -> IO () -takeWhileM size start = - drainTransformationDefault - size - (UF.takeWhileM (\b -> return (b <= size + start))) - start - -#ifdef INSPECTION -inspect $ 'takeWhileM `hasNoType` ''S.Step -inspect $ 'takeWhileM `hasNoType` ''FL.Step -inspect $ 'takeWhileM `hasNoType` ''SPEC -#endif - -{-# INLINE takeWhile #-} -takeWhile :: Int -> Int -> IO () -takeWhile size start = - drainTransformationDefault - size - (UF.takeWhile (\b -> b <= size + start)) - start - -#ifdef INSPECTION -inspect $ 'takeWhile `hasNoType` ''S.Step -inspect $ 'takeWhile `hasNoType` ''FL.Step -inspect $ 'takeWhile `hasNoType` ''SPEC -#endif - {-# INLINE take #-} take :: Int -> Int -> IO () take size start = drainTransformationDefault (size + start) (UF.take size) start @@ -874,54 +473,6 @@ inspect $ 'catMaybes `hasNoType` ''SPEC -- Stream combination ------------------------------------------------------------------------------- -{-# INLINE zipWith #-} -zipWith :: Int -> Int -> IO () -zipWith size start = - drainProductDefault (size + start) (UF.zipWith (+)) start - -#ifdef INSPECTION -inspect $ 'zipWith `hasNoType` ''S.Step -inspect $ 'zipWith `hasNoType` ''FL.Step -inspect $ 'zipWith `hasNoType` ''SPEC -#endif - -{-# INLINE zipWithM #-} -zipWithM :: Int -> Int -> IO () -zipWithM size start = - drainProductDefault - (size + start) - (UF.zipWithM (\a b -> return $ a + b)) - start - -#ifdef INSPECTION -inspect $ 'zipWithM `hasNoType` ''S.Step -inspect $ 'zipWithM `hasNoType` ''FL.Step -inspect $ 'zipWithM `hasNoType` ''SPEC -#endif - -{-# INLINE teeZipWith #-} -teeZipWith :: Int -> Int -> IO () -teeZipWith size start = - drainProductDefault (size + start) (UF.zipWith (+)) start - -#ifdef INSPECTION -inspect $ 'teeZipWith `hasNoType` ''S.Step -inspect $ 'teeZipWith `hasNoType` ''FL.Step -inspect $ 'teeZipWith `hasNoType` ''SPEC -#endif - -{-# INLINE interleave #-} -interleave :: Int -> Int -> IO () -interleave size start = - drainProductDefault (size + start) UF.interleave (start, start) - -#ifdef INSPECTION -inspect $ 'interleave `hasNoType` ''S.Step -inspect $ 'interleave `hasNoType` ''FL.Step -inspect $ 'interleave `hasNoType` ''SPEC -inspect $ 'interleave `hasNoType` ''Producer.InterleaveState -#endif - {-# INLINE eitherLeft #-} eitherLeft :: Int -> Int -> IO () eitherLeft size start = @@ -935,31 +486,6 @@ inspect $ 'eitherLeft `hasNoType` ''FL.Step inspect $ 'eitherLeft `hasNoType` ''SPEC #endif -{-# INLINE zipArrowWithM #-} -zipArrowWithM :: Int -> Int -> IO () -zipArrowWithM size start = - drainProductDefault - (size + start) - (UF.zipArrowWithM (\a b -> return (a + b))) - (start, start) - -#ifdef INSPECTION -inspect $ 'zipArrowWithM `hasNoType` ''S.Step -inspect $ 'zipArrowWithM `hasNoType` ''FL.Step -inspect $ 'zipArrowWithM `hasNoType` ''SPEC -#endif - -{-# INLINE zipArrowWith #-} -zipArrowWith :: Int -> Int -> IO () -zipArrowWith size start = - drainProductDefault (size + start) (UF.zipArrowWith (+)) (start, start) - -#ifdef INSPECTION -inspect $ 'zipArrowWith `hasNoType` ''S.Step -inspect $ 'zipArrowWith `hasNoType` ''FL.Step -inspect $ 'zipArrowWith `hasNoType` ''SPEC -#endif - {-# INLINE zipRepeat #-} zipRepeat :: Int -> Int -> IO () zipRepeat size start = @@ -978,147 +504,6 @@ inspect $ 'zipRepeat `hasNoType` ''SPEC nthRoot :: Double -> Int -> Int nthRoot n value = round (fromIntegral value**(1/n)) -{-# INLINE toNullAp #-} -toNullAp :: Int -> Int -> IO () -toNullAp value start = - let end = start + nthRoot 2 value - s = source end - -- in UF.fold ((+) <$> s <*> s) FL.drain start - in UF.fold FL.drain (((+) `fmap` s) `UF.crossApply` s) start - -#ifdef INSPECTION -inspect $ 'toNullAp `hasNoType` ''S.Step -inspect $ 'toNullAp `hasNoType` ''FL.Step -inspect $ 'toNullAp `hasNoType` ''SPEC -inspect $ 'toNullAp `hasNoType` ''Producer.CrossApplyState -#endif - -{-# INLINE crossApplyFst #-} -crossApplyFst :: Int -> Int -> IO () -crossApplyFst value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (s `UF.crossApplyFst` s) start - -#ifdef INSPECTION -inspect $ 'crossApplyFst `hasNoType` ''S.Step -inspect $ 'crossApplyFst `hasNoType` ''FL.Step -inspect $ 'crossApplyFst `hasNoType` ''SPEC -inspect $ 'crossApplyFst `hasNoType` ''Producer.CrossApplyFstState -#endif - -{-# INLINE crossApplySnd #-} -crossApplySnd :: Int -> Int -> IO () -crossApplySnd value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (s `UF.crossApplySnd` s) start - -#ifdef INSPECTION -inspect $ 'crossApplySnd `hasNoType` ''S.Step -inspect $ 'crossApplySnd `hasNoType` ''FL.Step -inspect $ 'crossApplySnd `hasNoType` ''SPEC -inspect $ 'crossApplySnd `hasNoType` ''Producer.CrossApplyState -#endif - -{-# INLINE cross #-} -cross :: Int -> Int -> IO () -cross value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (s `UF.cross` s) start - -#ifdef INSPECTION -inspect $ 'cross `hasNoType` ''S.Step -inspect $ 'cross `hasNoType` ''FL.Step -inspect $ 'cross `hasNoType` ''SPEC -inspect $ 'cross `hasNoType` ''Producer.CrossState -#endif - -{-# INLINE fairCross #-} -fairCross :: Int -> Int -> IO () -fairCross value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (s `UF.fairCross` s) start - -#ifdef INSPECTION -inspect $ 'fairCross `hasNoType` ''S.Step -inspect $ 'fairCross `hasNoType` ''FL.Step -inspect $ 'fairCross `hasNoType` ''SPEC -inspect $ 'fairCross `hasNoType` ''Producer.FairCrossState -#endif - -{-# INLINE crossApply #-} -crossApply :: Int -> Int -> IO () -crossApply value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (UF.crossApply (UF.map (+) s) s) start - -#ifdef INSPECTION -inspect $ 'crossApply `hasNoType` ''S.Step -inspect $ 'crossApply `hasNoType` ''FL.Step -inspect $ 'crossApply `hasNoType` ''SPEC -inspect $ 'crossApply `hasNoType` ''Producer.CrossApplyState -#endif - -{-# INLINE crossWithM #-} -crossWithM :: Int -> Int -> IO () -crossWithM value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (UF.crossWithM (\a b -> return (a + b)) s s) start - -#ifdef INSPECTION -inspect $ 'crossWithM `hasNoType` ''S.Step -inspect $ 'crossWithM `hasNoType` ''FL.Step -inspect $ 'crossWithM `hasNoType` ''SPEC -inspect $ 'crossWithM `hasNoType` ''Producer.CrossState -#endif - -{-# INLINE crossWith #-} -crossWith :: Int -> Int -> IO () -crossWith value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (UF.crossWith (+) s s) start - -#ifdef INSPECTION -inspect $ 'crossWith `hasNoType` ''S.Step -inspect $ 'crossWith `hasNoType` ''FL.Step -inspect $ 'crossWith `hasNoType` ''SPEC -inspect $ 'crossWith `hasNoType` ''Producer.CrossState -#endif - -{-# INLINE fairCrossWithM #-} -fairCrossWithM :: Int -> Int -> IO () -fairCrossWithM value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (UF.fairCrossWithM (\a b -> return (a + b)) s s) start - -#ifdef INSPECTION -inspect $ 'fairCrossWithM `hasNoType` ''S.Step -inspect $ 'fairCrossWithM `hasNoType` ''FL.Step -inspect $ 'fairCrossWithM `hasNoType` ''SPEC -inspect $ 'fairCrossWithM `hasNoType` ''Producer.FairCrossState -#endif - -{-# INLINE fairCrossWith #-} -fairCrossWith :: Int -> Int -> IO () -fairCrossWith value start = - let end = start + nthRoot 2 value - s = source end - in UF.fold FL.drain (UF.fairCrossWith (+) s s) start - -#ifdef INSPECTION -inspect $ 'fairCrossWith `hasNoType` ''S.Step -inspect $ 'fairCrossWith `hasNoType` ''FL.Step -inspect $ 'fairCrossWith `hasNoType` ''SPEC -inspect $ 'fairCrossWith `hasNoType` ''Producer.FairCrossState -#endif - {-# INLINE innerJoin #-} innerJoin :: Int -> Int -> IO () innerJoin value start = @@ -1133,290 +518,6 @@ inspect $ 'innerJoin `hasNoType` ''SPEC inspect $ 'innerJoin `hasNoType` ''Producer.CrossState #endif -------------------------------------------------------------------------------- --- Monad -------------------------------------------------------------------------------- - --- XXX to keep the benchmarks same as Stream we should use sourceUnfoldrM in --- all of these, and other benchmarks too. -{-# INLINE concatMapM #-} -concatMapM :: Int -> Int -> Int -> IO () -concatMapM inner outer start = - drainGeneration (UF.concatMapM unfoldInGen unfoldOut) start - - where - - unfoldInGen i = return (UF.supplySecond (i + inner) UF.enumerateFromToIntegral) - unfoldOut = UF.supplySecond (start + outer) UF.enumerateFromToIntegral - --- The 'bind'-based benchmarks use the Unfold monad ('UF.bind'), which is a --- concatMap and does not fuse, so the 'Step' constructors remain. -#ifdef INSPECTION --- inspect $ 'concatMapM `hasNoType` ''S.Step -inspect $ 'concatMapM `hasNoType` ''FL.Step -inspect $ 'concatMapM `hasNoType` ''SPEC -#endif - -{-# INLINE toNull #-} -toNull :: Int -> Int -> IO () -toNull value start = - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - return (x + y) - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - UF.fromPure (x + y) - in UF.fold FL.drain u start - -#ifdef INSPECTION --- inspect $ 'toNull `hasNoType` ''S.Step -inspect $ 'toNull `hasNoType` ''FL.Step -inspect $ 'toNull `hasNoType` ''SPEC -#endif - -{-# INLINE toNull3 #-} -toNull3 :: Int -> Int -> IO () -toNull3 value start = - let end = start + nthRoot 3 value - src = source end - {- - u = do - x <- src - y <- src - z <- src - return (x + y + z) - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - src `UF.bind` \z -> - UF.fromPure (x + y + z) - in UF.fold FL.drain u start - -#ifdef INSPECTION --- inspect $ 'toNull3 `hasNoType` ''S.Step -inspect $ 'toNull3 `hasNoType` ''FL.Step -inspect $ 'toNull3 `hasNoType` ''SPEC -#endif - -{-# INLINE toNullConcatMap #-} -toNullConcatMap :: Int -> Int -> IO () -toNullConcatMap value start = - let end = start + nthRoot 2 value - src = source end - u = UF.concatMap (\x -> - UF.concatMap (\y -> - UF.fromPure (x + y)) src) src - in UF.fold FL.drain u start - -{-# INLINE toNull3ConcatMap #-} -toNull3ConcatMap :: Int -> Int -> IO () -toNull3ConcatMap value start = - let end = start + nthRoot 3 value - src = source end - u = UF.concatMap (\x -> - UF.concatMap (\y -> - UF.concatMap (\z -> - UF.fromPure (x + y + z)) src) src) src - in UF.fold FL.drain u start - -{-# INLINE toList #-} -toList :: Int -> Int -> IO [Int] -toList value start = do - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - return (x + y) - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - UF.fromPure (x + y) - in UF.fold FL.toList u start - -#ifdef INSPECTION --- inspect $ 'toList `hasNoType` ''S.Step -inspect $ 'toList `hasNoType` ''FL.Step -inspect $ 'toList `hasNoType` ''SPEC -#endif - -{-# INLINE toListSome #-} -toListSome :: Int -> Int -> IO [Int] -toListSome value start = do - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - return (x + y) - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - UF.fromPure (x + y) - in UF.fold FL.toList (UF.take 1000 u) start - -#ifdef INSPECTION --- inspect $ 'toListSome `hasNoType` ''S.Step -inspect $ 'toListSome `hasNoType` ''FL.Step -inspect $ 'toListSome `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllOut #-} -filterAllOut :: Int -> Int -> IO () -filterAllOut value start = do - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - let s = x + y - in if s < 0 - then UF.fromPure s - else UF.nilM (return . const ()) - in UF.fold FL.drain u start - -#ifdef INSPECTION --- inspect $ 'filterAllOut `hasNoType` ''S.Step -inspect $ 'filterAllOut `hasNoType` ''FL.Step -inspect $ 'filterAllOut `hasNoType` ''SPEC -#endif - -{-# INLINE filterAllIn #-} -filterAllIn :: Int -> Int -> IO () -filterAllIn value start = do - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - let s = x + y - in if s > 0 - then UF.fromPure s - else UF.nilM (return . const ()) - in UF.fold FL.drain u start - -#ifdef INSPECTION --- inspect $ 'filterAllIn `hasNoType` ''S.Step -inspect $ 'filterAllIn `hasNoType` ''FL.Step -inspect $ 'filterAllIn `hasNoType` ''SPEC -#endif - -{-# INLINE filterSome #-} -filterSome :: Int -> Int -> IO () -filterSome value start = do - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - let s = x + y - in if s > 1100000 - then UF.fromPure s - else UF.nilM (return . const ()) - in UF.fold FL.drain u start - -#ifdef INSPECTION --- inspect $ 'filterSome `hasNoType` ''S.Step -inspect $ 'filterSome `hasNoType` ''FL.Step -inspect $ 'filterSome `hasNoType` ''SPEC -#endif - -{-# INLINE breakAfterSome #-} -breakAfterSome :: Int -> Int -> IO () -breakAfterSome value start = - let end = start + nthRoot 2 value - src = source end - {- - u = do - x <- src - y <- src - -} - u = src `UF.bind` \x -> - src `UF.bind` \y -> - let s = x + y - in if s > 1100000 - then error "break" - else UF.fromPure s - in do - (_ :: Either ErrorCall ()) <- try $ UF.fold FL.drain u start - return () - -#ifdef INSPECTION --- inspect $ 'breakAfterSome `hasNoType` ''S.Step -inspect $ 'breakAfterSome `hasNoType` ''FL.Step -inspect $ 'breakAfterSome `hasNoType` ''SPEC -#endif - -------------------------------------------------------------------------------- --- Benchmark ops -------------------------------------------------------------------------------- - -{-# INLINE unfoldEach #-} -unfoldEach :: Int -> Int -> Int -> IO () -unfoldEach inner outer start = do - UF.fold - FL.drain - (UF.unfoldEach (sourceUnfoldrM inner start) (sourceUnfoldrM outer start)) - start - -#ifdef INSPECTION -inspect $ 'unfoldEach `hasNoType` ''S.Step -inspect $ 'unfoldEach `hasNoType` ''FL.Step -inspect $ 'unfoldEach `hasNoType` ''SPEC -inspect $ 'unfoldEach `hasNoType` ''Producer.ConcatState -#endif - -{-# INLINE unfoldEachInterleave #-} -unfoldEachInterleave :: Int -> Int -> Int -> IO () -unfoldEachInterleave inner outer start = do - UF.fold - FL.drain - (UF.unfoldEachInterleave - (sourceUnfoldrM inner start) (sourceUnfoldrM outer start)) - start - --- 'unfoldEachInterleave' does not fuse: 'Step' and 'SPEC' are not eliminated. -#ifdef INSPECTION --- inspect $ 'unfoldEachInterleave `hasNoType` ''S.Step --- inspect $ 'unfoldEachInterleave `hasNoType` ''SPEC -inspect $ 'unfoldEachInterleave `hasNoType` ''FL.Step -#endif - -{-# INLINE concatMapPure #-} -concatMapPure :: Int -> Int -> Int -> IO () -concatMapPure inner outer start = - drainGeneration (UF.concatMap unfoldInGen unfoldOut) start - - where - - unfoldInGen i = UF.supplySecond (i + inner) UF.enumerateFromToIntegral - unfoldOut = UF.supplySecond (start + outer) UF.enumerateFromToIntegral - -#ifdef INSPECTION --- inspect $ 'concatMapPure `hasNoType` ''S.Step -inspect $ 'concatMapPure `hasNoType` ''FL.Step -inspect $ 'concatMapPure `hasNoType` ''SPEC -#endif - ------------------------------------------------------------------------------- -- Resource management ------------------------------------------------------------------------------- @@ -1515,16 +616,8 @@ o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation_input size = [ (SpaceO_1, bgroup "transformation/input" - [ benchIO "lmap" $ lmap size - , benchIO "lmapM" $ lmapM size - , benchIO "both" $ both size - , benchIO "first" $ first size - , benchIO "second" $ second size - , benchIO "discardFirst" $ discardFirst size + [ benchIO "discardFirst" $ discardFirst size , benchIO "discardSecond" $ discardSecond size - , benchIO "consInput" $ consInput size - , benchIO "consInputWith" $ consInputWith size - , benchIO "swap" $ swap size ]) ] @@ -1538,28 +631,12 @@ o_1_space_generation size = , benchIO "nilM" $ nilM size , benchIO "nil" $ nil size , benchIO "consM" $ consM size - , benchIO "functionM" $ functionM size - , benchIO "function" $ function size - , benchIO "identity" $ identity size - , benchIO "fromEffect" $ fromEffect size - , benchIO "fromPure" $ fromPure size - , benchIO "functionMaybeM" $ functionMaybeM size - , benchIO "fromTuple" $ fromTuple size - , benchIO "unfoldrM" $ unfoldrM size - , benchIO "unfoldr" $ unfoldr size - , benchIO "fromList" $ fromList size , benchIO "fromListM" $ fromListM size , benchIO "replicateM" $ replicateM size , benchIO "repeatM" $ repeatM size , benchIO "repeat" $ repeat size , benchIO "iterateM" $ iterateM size , benchIO "fromIndicesM" $ fromIndicesM size - , benchIO "enumerateFromThenIntegral" $ enumerateFromThenIntegral size - , benchIO "enumerateFromToIntegral" $ enumerateFromToIntegral size - , benchIO "enumerateFromIntegral" $ enumerateFromIntegral size - , benchIO "enumerateFromStepNum" $ enumerateFromStepNum size - , benchIO "enumerateFromNum" $ enumerateFromNum size - , benchIO "enumerateFromToFractional" $ enumerateFromToFractional size ]) ] @@ -1567,10 +644,7 @@ o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation size = [ (SpaceO_1, bgroup "transformation" - [ benchIO "map" $ map size - , benchIO "mapM" $ mapM size - , benchIO "mapM2" $ mapM2 size - , benchIO "postscan" $ postscan size + [ benchIO "postscan" $ postscan size , benchIO "scanl" $ scanl size , benchIO "scanlMany" $ scanlMany size ]) @@ -1580,9 +654,7 @@ o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_filtering size = [ (SpaceO_1, bgroup "filtering" - [ benchIO "takeWhileM" $ takeWhileM size - , benchIO "takeWhile" $ takeWhile size - , benchIO "take" $ take size + [ benchIO "take" $ take size , benchIO "filter" $ filter size , benchIO "filterM" $ filterM size , benchIO "dropOne" $ dropOne size @@ -1602,13 +674,7 @@ o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_zip size = [ (SpaceO_1, bgroup "zip" - [ benchIO "zipWithM" $ zipWithM size - , benchIO "zipWith" $ zipWith size - , benchIO "teeZipWith" $ teeZipWith size - , benchIO "interleave" $ interleave size - , benchIO "eitherLeft" $ eitherLeft size - , benchIO "zipArrowWithM" $ zipArrowWithM size - , benchIO "zipArrowWith" $ zipArrowWith size + [ benchIO "eitherLeft" $ eitherLeft size , benchIO "zipRepeat" $ zipRepeat size ]) ] @@ -1617,66 +683,12 @@ o_1_space_nested :: BenchEnv -> Int -> [(SpaceComplexity, Benchmark)] o_1_space_nested env size = [ (SpaceO_1, bgroup "nested" - [ benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size - , benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size - , benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size - , benchIO "cross outer=inner=(sqrt Max)" $ cross size - , benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size - , benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size - , benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size - , benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size - , benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size - , benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size - , benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size - - , benchIO "unfoldEach inner=outer=(sqrt Max)" $ unfoldEach sqrtVal sqrtVal - , benchIO "unfoldEach inner=1 outer=Max" $ unfoldEach 1 size - , benchIO "unfoldEach inner=Max outer=1" $ unfoldEach size 1 - , benchIO "unfoldEachInterleave inner=outer=(sqrt Max)" - $ unfoldEachInterleave sqrtVal sqrtVal - , benchIO "unfoldEachInterleave inner=1 outer=Max" - $ unfoldEachInterleave 1 size - , benchIO "unfoldEachInterleave inner=Max outer=1" - $ unfoldEachInterleave size 1 + [ benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size , mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env $ \inh _ -> foldManySepBy inh ]) ] - where - - sqrtVal = round $ sqrt (fromIntegral size :: Double) - -o_1_space_concat :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_concat size = - [ (SpaceO_1, bgroup - "concat" - [ benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal - , benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal - , benchIO "bind2" $ toNull size - , benchIO "bind3" $ toNull3 size - , benchIO "concatMap2" $ toNullConcatMap size - , benchIO "concatMap3" $ toNull3ConcatMap size - , benchIO "breakAfterSome2" $ breakAfterSome size - , benchIO "filterAllOut2" $ filterAllOut size - , benchIO "filterAllIn2" $ filterAllIn size - , benchIO "filterSome2" $ filterSome size - ]) - ] - - where - - sqrtVal = round $ sqrt (fromIntegral size :: Double) - -o_n_space_concat :: Int -> [(SpaceComplexity, Benchmark)] -o_n_space_concat size = - [ (SpaceO_n, bgroup - "concat" - [ benchIO "toList2" $ toList size - , benchIO "toListSome2" $ toListSome size - ]) - ] - o_1_space_resource_management :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_resource_management size = [ (SpaceO_1, bgroup @@ -1775,16 +787,16 @@ main = do allBenchmarks env size = let allBenches = Prelude.concat - [ o_1_space_transformation_input size + [ Type.benchmarks size + , Enumeration.benchmarks size + , o_1_space_transformation_input size , o_1_space_generation size , o_1_space_transformation size , o_1_space_filtering size , o_1_space_zip size , o_1_space_nested env size - , o_1_space_concat size , o_1_space_resource_management size , o_1_space_copy_read_exceptions env - , o_n_space_concat size ] get x = Prelude.map snd $ Prelude.filter ((==) x . fst) allBenches o_1_space = get SpaceO_1 @@ -1798,6 +810,6 @@ main = do -- Enable one benchmark below, and run the benchmark -- Check the .dump-simpl output let value = 100000 - lmapM value 0 + Type.lmapM value 0 return () #endif diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs new file mode 100644 index 0000000000..59a9805a1a --- /dev/null +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs @@ -0,0 +1,154 @@ +-- | +-- Module : Unfold.Enumeration +-- Copyright : (c) 2018 Composewell +-- License : MIT +-- Maintainer : streamly@composewell.com + +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE CPP #-} +{-# LANGUAGE ScopedTypeVariables #-} + +#undef FUSION_CHECK +#ifdef FUSION_CHECK +{-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all #-} +#endif + +#ifdef __HADDOCK_VERSION__ +#undef INSPECTION +#endif + +#ifdef INSPECTION +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} +#endif + +module Unfold.Enumeration (benchmarks) where + +import Control.DeepSeq (NFData(..)) +import Streamly.Internal.Data.Unfold (Unfold) +import System.Random (randomRIO) + +import qualified Streamly.Internal.Data.Fold as FL +import qualified Streamly.Internal.Data.Unfold as UF + + +import Test.Tasty.Bench hiding (env) +import Prelude hiding (take, filter, zipWith, map, mapM, takeWhile, scanl, repeat, dropWhile) +import Streamly.Benchmark.Common + +#ifdef INSPECTION +import GHC.Types (SPEC(..)) +import Test.Inspection +import qualified Streamly.Internal.Data.Stream as S +#endif + +{-# INLINE benchIO #-} +benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark +benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f + +{-# INLINE drainGeneration #-} +drainGeneration :: Monad m => Unfold m a b -> a -> m () +drainGeneration unf seed = UF.fold FL.drain unf seed + +------------------------------------------------------------------------------- +-- Stream generation +------------------------------------------------------------------------------- + +{-# INLINE enumerateFromThenIntegral #-} +enumerateFromThenIntegral :: Int -> Int -> IO () +enumerateFromThenIntegral size start = + drainGeneration (UF.take size UF.enumerateFromThenIntegral) (start, 1) + +#ifdef INSPECTION +inspect $ 'enumerateFromThenIntegral `hasNoType` ''S.Step +inspect $ 'enumerateFromThenIntegral `hasNoType` ''FL.Step +inspect $ 'enumerateFromThenIntegral `hasNoType` ''SPEC +#endif + +{-# INLINE enumerateFromToIntegral #-} +enumerateFromToIntegral :: Int -> Int -> IO () +enumerateFromToIntegral size start = + drainGeneration + ( UF.supplySecond + (size + start) + UF.enumerateFromToIntegral + ) start + +#ifdef INSPECTION +inspect $ 'enumerateFromToIntegral `hasNoType` ''S.Step +inspect $ 'enumerateFromToIntegral `hasNoType` ''FL.Step +inspect $ 'enumerateFromToIntegral `hasNoType` ''SPEC +#endif + +{-# INLINE enumerateFromIntegral #-} +enumerateFromIntegral :: Int -> Int -> IO () +enumerateFromIntegral size start = + drainGeneration (UF.take size UF.enumerateFromIntegral) start + +#ifdef INSPECTION +inspect $ 'enumerateFromIntegral `hasNoType` ''S.Step +inspect $ 'enumerateFromIntegral `hasNoType` ''FL.Step +inspect $ 'enumerateFromIntegral `hasNoType` ''SPEC +#endif + +{-# INLINE enumerateFromStepNum #-} +enumerateFromStepNum :: Int -> Int -> IO () +enumerateFromStepNum size start = + drainGeneration (UF.take size (UF.enumerateFromThenNum)) (start, 1) + +#ifdef INSPECTION +inspect $ 'enumerateFromStepNum `hasNoType` ''S.Step +inspect $ 'enumerateFromStepNum `hasNoType` ''FL.Step +inspect $ 'enumerateFromStepNum `hasNoType` ''SPEC +#endif + +{-# INLINE enumerateFromNum #-} +enumerateFromNum :: Int -> Int -> IO () +enumerateFromNum size start = drainGeneration (UF.take size UF.enumerateFromNum) start + +#ifdef INSPECTION +inspect $ 'enumerateFromNum `hasNoType` ''S.Step +inspect $ 'enumerateFromNum `hasNoType` ''FL.Step +inspect $ 'enumerateFromNum `hasNoType` ''SPEC +#endif + +{-# INLINE enumerateFromToFractional #-} +enumerateFromToFractional :: Int -> Int -> IO () +enumerateFromToFractional size start = + let intToDouble x = (fromInteger (fromIntegral x)) :: Double + in drainGeneration + ( UF.supplySecond + (intToDouble $ start + size) + UF.enumerateFromToFractional + ) + (intToDouble start) + +#ifdef INSPECTION +inspect $ 'enumerateFromToFractional `hasNoType` ''S.Step +inspect $ 'enumerateFromToFractional `hasNoType` ''FL.Step +inspect $ 'enumerateFromToFractional `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Benchmarks +------------------------------------------------------------------------------- + +o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_generation size = + [ (SpaceO_1, bgroup + "generation" + [ benchIO "enumerateFromThenIntegral" $ enumerateFromThenIntegral size + , benchIO "enumerateFromToIntegral" $ enumerateFromToIntegral size + , benchIO "enumerateFromIntegral" $ enumerateFromIntegral size + , benchIO "enumerateFromStepNum" $ enumerateFromStepNum size + , benchIO "enumerateFromNum" $ enumerateFromNum size + , benchIO "enumerateFromToFractional" $ enumerateFromToFractional size + ]) + ] + +------------------------------------------------------------------------------- +-- Driver +------------------------------------------------------------------------------- + +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = o_1_space_generation size diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs new file mode 100644 index 0000000000..f2f0c60093 --- /dev/null +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -0,0 +1,1075 @@ +-- | +-- Module : Unfold.Type +-- Copyright : (c) 2018 Composewell +-- License : MIT +-- Maintainer : streamly@composewell.com + +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE CPP #-} +{-# LANGUAGE ScopedTypeVariables #-} + +#undef FUSION_CHECK +#ifdef FUSION_CHECK +{-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all #-} +#endif + +#ifdef __HADDOCK_VERSION__ +#undef INSPECTION +#endif + +#ifdef INSPECTION +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-} +#endif + +module Unfold.Type (benchmarks) where + +import Control.DeepSeq (NFData(..)) +import Control.Exception (ErrorCall, try) +import qualified Data.Tuple as Tuple +import Streamly.Internal.Data.Unfold (Unfold) +import System.Random (randomRIO) + +import qualified Streamly.Internal.Data.Fold as FL +import qualified Streamly.Internal.Data.Unfold as UF + +import Test.Tasty.Bench hiding (env) +import Prelude hiding (take, filter, zipWith, map, mapM, takeWhile, scanl, repeat, dropWhile) +import Streamly.Benchmark.Common + +#ifdef INSPECTION +import GHC.Types (SPEC(..)) +import Test.Inspection +import qualified Streamly.Internal.Data.Stream as S + +import qualified Streamly.Internal.Data.Producer as Producer +#endif + +{-# INLINE benchIO #-} +benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark +benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f + +------------------------------------------------------------------------------- +-- Stream generation and elimination +------------------------------------------------------------------------------- + +-- generate numbers up to the argument value +{-# INLINE source #-} +source :: Monad m => Int -> Unfold m Int Int +source n = UF.supplySecond n UF.enumerateFromToIntegral + +------------------------------------------------------------------------------- +-- Benchmark helpers +------------------------------------------------------------------------------- + +{-# INLINE drainGeneration #-} +drainGeneration :: Monad m => Unfold m a b -> a -> m () +drainGeneration unf seed = UF.fold FL.drain unf seed + +{-# INLINE drainTransformation #-} +drainTransformation :: + Monad m => Unfold m a b -> (Unfold m a b -> Unfold m c d) -> c -> m () +drainTransformation unf f seed = drainGeneration (f unf) seed + +{-# INLINE drainTransformationDefault #-} +drainTransformationDefault :: + Monad m => Int -> (Unfold m Int Int -> Unfold m c d) -> c -> m () +drainTransformationDefault to = + drainTransformation (UF.supplySecond to UF.enumerateFromToIntegral) + +{-# INLINE drainProduct #-} +drainProduct :: + Monad m + => Unfold m a b + -> Unfold m c d + -> (Unfold m a b -> Unfold m c d -> Unfold m e f) + -> e + -> m () +drainProduct unf1 unf2 f seed = drainGeneration (f unf1 unf2) seed + +{-# INLINE drainProductDefault #-} +drainProductDefault :: + Monad m + => Int + -> (Unfold m Int Int -> Unfold m Int Int -> Unfold m e f) + -> e + -> m () +drainProductDefault to = drainProduct src src + + where + + src = UF.supplySecond to UF.enumerateFromToIntegral + +------------------------------------------------------------------------------- +-- Operations on input +------------------------------------------------------------------------------- + +{-# INLINE lmap #-} +lmap :: Int -> Int -> IO () +lmap size start = + drainTransformationDefault (size + start) (UF.lmap (+ 1)) start + +#ifdef INSPECTION +inspect $ 'lmap `hasNoType` ''S.Step +inspect $ 'lmap `hasNoType` ''FL.Step +inspect $ 'lmap `hasNoType` ''SPEC +#endif + +{-# INLINE lmapM #-} +lmapM :: Int -> Int -> IO () +lmapM size start = + drainTransformationDefault (size + start) (UF.lmapM (return . (+) 1)) start + +#ifdef INSPECTION +inspect $ 'lmapM `hasNoType` ''S.Step +inspect $ 'lmapM `hasNoType` ''FL.Step +inspect $ 'lmapM `hasNoType` ''SPEC +#endif + +{-# INLINE both #-} +both :: Int -> Int -> IO () +both size start = + drainTransformationDefault (size + start) (UF.supply start) () + +#ifdef INSPECTION +inspect $ 'both `hasNoType` ''S.Step +inspect $ 'both `hasNoType` ''FL.Step +inspect $ 'both `hasNoType` ''SPEC +#endif + +{-# INLINE first #-} +first :: Int -> Int -> IO () +first size start = + drainTransformation + (UF.take size UF.enumerateFromThenIntegral) + (UF.supplyFirst start) + 1 + +#ifdef INSPECTION +inspect $ 'first `hasNoType` ''S.Step +inspect $ 'first `hasNoType` ''FL.Step +inspect $ 'first `hasNoType` ''SPEC +#endif + +{-# INLINE second #-} +second :: Int -> Int -> IO () +second size start = + drainTransformation + (UF.take size UF.enumerateFromThenIntegral) + (UF.supplySecond 1) + start + +#ifdef INSPECTION +inspect $ 'second `hasNoType` ''S.Step +inspect $ 'second `hasNoType` ''FL.Step +inspect $ 'second `hasNoType` ''SPEC +#endif + +{-# INLINE consInput #-} +consInput :: Int -> Int -> IO () +consInput size start = + drainTransformationDefault (size + start) UF.consInput start + +#ifdef INSPECTION +inspect $ 'consInput `hasNoType` ''S.Step +inspect $ 'consInput `hasNoType` ''FL.Step +inspect $ 'consInput `hasNoType` ''SPEC +inspect $ 'consInput `hasNoType` ''UF.ConsInputState +#endif + +{-# INLINE consInputWith #-} +consInputWith :: Int -> Int -> IO () +consInputWith size start = + drainTransformationDefault (size + start) (UF.consInputWith (+1)) start + +#ifdef INSPECTION +inspect $ 'consInputWith `hasNoType` ''S.Step +inspect $ 'consInputWith `hasNoType` ''FL.Step +inspect $ 'consInputWith `hasNoType` ''SPEC +inspect $ 'consInputWith `hasNoType` ''UF.ConsInputState +#endif + +{-# INLINE swap #-} +swap :: Int -> Int -> IO () +swap size start = + drainTransformation + (UF.take size UF.enumerateFromThenIntegral) + (UF.lmap Tuple.swap) + (1, start) + +#ifdef INSPECTION +inspect $ 'swap `hasNoType` ''S.Step +inspect $ 'swap `hasNoType` ''FL.Step +inspect $ 'swap `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Stream generation +------------------------------------------------------------------------------- + +-- 'functionM', 'function', 'identity' and 'fromEffect' generate a single +-- element per seed, so to process ~value elements we unfold them over an outer +-- source of value seeds. +{-# INLINE functionM #-} +functionM :: Int -> Int -> IO () +functionM value start = + drainGeneration + (UF.unfoldEach (UF.functionM return) (source (start + value))) start + +#ifdef INSPECTION +inspect $ 'functionM `hasNoType` ''S.Step +inspect $ 'functionM `hasNoType` ''FL.Step +inspect $ 'functionM `hasNoType` ''SPEC +inspect $ 'functionM `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE function #-} +function :: Int -> Int -> IO () +function value start = + drainGeneration + (UF.unfoldEach (UF.function id) (source (start + value))) start + +#ifdef INSPECTION +inspect $ 'function `hasNoType` ''S.Step +inspect $ 'function `hasNoType` ''FL.Step +inspect $ 'function `hasNoType` ''SPEC +inspect $ 'function `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE identity #-} +identity :: Int -> Int -> IO () +identity value start = + drainGeneration (UF.unfoldEach UF.identity (source (start + value))) start + +#ifdef INSPECTION +inspect $ 'identity `hasNoType` ''S.Step +inspect $ 'identity `hasNoType` ''FL.Step +inspect $ 'identity `hasNoType` ''SPEC +inspect $ 'identity `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE fromEffect #-} +fromEffect :: Int -> Int -> IO () +fromEffect value start = + drainGeneration + (UF.unfoldEach (UF.fromEffect (return start)) (source (start + value))) + start + +#ifdef INSPECTION +inspect $ 'fromEffect `hasNoType` ''S.Step +inspect $ 'fromEffect `hasNoType` ''FL.Step +inspect $ 'fromEffect `hasNoType` ''SPEC +inspect $ 'fromEffect `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE fromPure #-} +fromPure :: Int -> Int -> IO () +fromPure value start = + drainGeneration + (UF.unfoldEach (UF.fromPure start) (source (start + value))) + start + +#ifdef INSPECTION +inspect $ 'fromPure `hasNoType` ''S.Step +inspect $ 'fromPure `hasNoType` ''FL.Step +inspect $ 'fromPure `hasNoType` ''SPEC +inspect $ 'fromPure `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE functionMaybeM #-} +functionMaybeM :: Int -> Int -> IO () +functionMaybeM value start = + drainGeneration + (UF.unfoldEach (UF.functionMaybeM (return . Just)) (source (start + value))) + start + +#ifdef INSPECTION +inspect $ 'functionMaybeM `hasNoType` ''S.Step +inspect $ 'functionMaybeM `hasNoType` ''FL.Step +inspect $ 'functionMaybeM `hasNoType` ''SPEC +inspect $ 'functionMaybeM `hasNoType` ''Producer.ConcatState +#endif + +-- 'fromTuple' generates two elements per seed, so unfold it over value/2 tuples +-- to emit and drain ~value elements. +{-# INLINE fromTuple #-} +fromTuple :: Int -> Int -> IO () +fromTuple value start = + let outer = UF.map (\i -> (i, i)) (source (start + value `div` 2)) + in drainGeneration (UF.unfoldEach UF.fromTuple outer) start + +#ifdef INSPECTION +inspect $ 'fromTuple `hasNoType` ''S.Step +inspect $ 'fromTuple `hasNoType` ''FL.Step +inspect $ 'fromTuple `hasNoType` ''SPEC +inspect $ 'fromTuple `hasNoType` ''Producer.ConcatState +inspect $ 'fromTuple `hasNoType` ''Producer.TupleState +#endif + +{-# INLINE sourceUnfoldrM #-} +sourceUnfoldrM :: Monad m => Int -> Int -> Unfold m Int Int +sourceUnfoldrM size start = UF.unfoldrM step + + where + + step i = + return + $ if i < start + size + then Just (i, i + 1) + else Nothing + +{-# INLINE unfoldrM #-} +unfoldrM :: Int -> Int -> IO () +unfoldrM size start = drainGeneration (sourceUnfoldrM size start) start + +#ifdef INSPECTION +inspect $ 'unfoldrM `hasNoType` ''S.Step +inspect $ 'unfoldrM `hasNoType` ''FL.Step +inspect $ 'unfoldrM `hasNoType` ''SPEC +#endif + +{-# INLINE unfoldr #-} +unfoldr :: Int -> Int -> IO () +unfoldr size start = drainGeneration (UF.unfoldr step) start + where + step i = if i < start + size then Just (i, i + 1) else Nothing + +#ifdef INSPECTION +inspect $ 'unfoldr `hasNoType` ''S.Step +inspect $ 'unfoldr `hasNoType` ''FL.Step +inspect $ 'unfoldr `hasNoType` ''SPEC +#endif + +{-# INLINE fromList #-} +fromList :: Int -> Int -> IO () +fromList size start = drainGeneration UF.fromList [start .. start + size] + +#ifdef INSPECTION +inspect $ 'fromList `hasNoType` ''S.Step +inspect $ 'fromList `hasNoType` ''FL.Step +inspect $ 'fromList `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Stream transformation +------------------------------------------------------------------------------- + +{-# INLINE map #-} +map :: Int -> Int -> IO () +map size start = drainTransformationDefault (size + start) (UF.map (+1)) start + +#ifdef INSPECTION +inspect $ 'map `hasNoType` ''S.Step +inspect $ 'map `hasNoType` ''FL.Step +inspect $ 'map `hasNoType` ''SPEC +#endif + +{-# INLINE mapM #-} +mapM :: Int -> Int -> IO () +mapM size start = + drainTransformationDefault (size + start) (UF.mapM (return . (+) 1)) start + +#ifdef INSPECTION +inspect $ 'mapM `hasNoType` ''S.Step +inspect $ 'mapM `hasNoType` ''FL.Step +inspect $ 'mapM `hasNoType` ''SPEC +#endif + +{-# INLINE mapM2 #-} +mapM2 :: Int -> Int -> IO () +mapM2 size start = + drainTransformationDefault + size + (UF.mapM (\(a, b) -> return $ a + b) . UF.carryInput) + start + +#ifdef INSPECTION +inspect $ 'mapM2 `hasNoType` ''S.Step +inspect $ 'mapM2 `hasNoType` ''FL.Step +inspect $ 'mapM2 `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Stream filtering +------------------------------------------------------------------------------- + +{-# INLINE takeWhileM #-} +takeWhileM :: Int -> Int -> IO () +takeWhileM size start = + drainTransformationDefault + size + (UF.takeWhileM (\b -> return (b <= size + start))) + start + +#ifdef INSPECTION +inspect $ 'takeWhileM `hasNoType` ''S.Step +inspect $ 'takeWhileM `hasNoType` ''FL.Step +inspect $ 'takeWhileM `hasNoType` ''SPEC +#endif + +{-# INLINE takeWhile #-} +takeWhile :: Int -> Int -> IO () +takeWhile size start = + drainTransformationDefault + size + (UF.takeWhile (\b -> b <= size + start)) + start + +#ifdef INSPECTION +inspect $ 'takeWhile `hasNoType` ''S.Step +inspect $ 'takeWhile `hasNoType` ''FL.Step +inspect $ 'takeWhile `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Stream combination +------------------------------------------------------------------------------- + +{-# INLINE zipWith #-} +zipWith :: Int -> Int -> IO () +zipWith size start = + drainProductDefault (size + start) (UF.zipWith (+)) start + +#ifdef INSPECTION +inspect $ 'zipWith `hasNoType` ''S.Step +inspect $ 'zipWith `hasNoType` ''FL.Step +inspect $ 'zipWith `hasNoType` ''SPEC +#endif + +{-# INLINE zipWithM #-} +zipWithM :: Int -> Int -> IO () +zipWithM size start = + drainProductDefault + (size + start) + (UF.zipWithM (\a b -> return $ a + b)) + start + +#ifdef INSPECTION +inspect $ 'zipWithM `hasNoType` ''S.Step +inspect $ 'zipWithM `hasNoType` ''FL.Step +inspect $ 'zipWithM `hasNoType` ''SPEC +#endif + +{-# INLINE teeZipWith #-} +teeZipWith :: Int -> Int -> IO () +teeZipWith size start = + drainProductDefault (size + start) (UF.zipWith (+)) start + +#ifdef INSPECTION +inspect $ 'teeZipWith `hasNoType` ''S.Step +inspect $ 'teeZipWith `hasNoType` ''FL.Step +inspect $ 'teeZipWith `hasNoType` ''SPEC +#endif + +{-# INLINE interleave #-} +interleave :: Int -> Int -> IO () +interleave size start = + drainProductDefault (size + start) UF.interleave (start, start) + +#ifdef INSPECTION +inspect $ 'interleave `hasNoType` ''S.Step +inspect $ 'interleave `hasNoType` ''FL.Step +inspect $ 'interleave `hasNoType` ''SPEC +inspect $ 'interleave `hasNoType` ''Producer.InterleaveState +#endif + +{-# INLINE zipArrowWithM #-} +zipArrowWithM :: Int -> Int -> IO () +zipArrowWithM size start = + drainProductDefault + (size + start) + (UF.zipArrowWithM (\a b -> return (a + b))) + (start, start) + +#ifdef INSPECTION +inspect $ 'zipArrowWithM `hasNoType` ''S.Step +inspect $ 'zipArrowWithM `hasNoType` ''FL.Step +inspect $ 'zipArrowWithM `hasNoType` ''SPEC +#endif + +{-# INLINE zipArrowWith #-} +zipArrowWith :: Int -> Int -> IO () +zipArrowWith size start = + drainProductDefault (size + start) (UF.zipArrowWith (+)) (start, start) + +#ifdef INSPECTION +inspect $ 'zipArrowWith `hasNoType` ''S.Step +inspect $ 'zipArrowWith `hasNoType` ''FL.Step +inspect $ 'zipArrowWith `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Applicative +------------------------------------------------------------------------------- + +nthRoot :: Double -> Int -> Int +nthRoot n value = round (fromIntegral value**(1/n)) + +{-# INLINE toNullAp #-} +toNullAp :: Int -> Int -> IO () +toNullAp value start = + let end = start + nthRoot 2 value + s = source end + -- in UF.fold ((+) <$> s <*> s) FL.drain start + in UF.fold FL.drain (((+) `fmap` s) `UF.crossApply` s) start + +#ifdef INSPECTION +inspect $ 'toNullAp `hasNoType` ''S.Step +inspect $ 'toNullAp `hasNoType` ''FL.Step +inspect $ 'toNullAp `hasNoType` ''SPEC +inspect $ 'toNullAp `hasNoType` ''Producer.CrossApplyState +#endif + +{-# INLINE crossApplyFst #-} +crossApplyFst :: Int -> Int -> IO () +crossApplyFst value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (s `UF.crossApplyFst` s) start + +#ifdef INSPECTION +inspect $ 'crossApplyFst `hasNoType` ''S.Step +inspect $ 'crossApplyFst `hasNoType` ''FL.Step +inspect $ 'crossApplyFst `hasNoType` ''SPEC +inspect $ 'crossApplyFst `hasNoType` ''Producer.CrossApplyFstState +#endif + +{-# INLINE crossApplySnd #-} +crossApplySnd :: Int -> Int -> IO () +crossApplySnd value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (s `UF.crossApplySnd` s) start + +#ifdef INSPECTION +inspect $ 'crossApplySnd `hasNoType` ''S.Step +inspect $ 'crossApplySnd `hasNoType` ''FL.Step +inspect $ 'crossApplySnd `hasNoType` ''SPEC +inspect $ 'crossApplySnd `hasNoType` ''Producer.CrossApplyState +#endif + +{-# INLINE cross #-} +cross :: Int -> Int -> IO () +cross value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (s `UF.cross` s) start + +#ifdef INSPECTION +inspect $ 'cross `hasNoType` ''S.Step +inspect $ 'cross `hasNoType` ''FL.Step +inspect $ 'cross `hasNoType` ''SPEC +inspect $ 'cross `hasNoType` ''Producer.CrossState +#endif + +{-# INLINE fairCross #-} +fairCross :: Int -> Int -> IO () +fairCross value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (s `UF.fairCross` s) start + +#ifdef INSPECTION +inspect $ 'fairCross `hasNoType` ''S.Step +inspect $ 'fairCross `hasNoType` ''FL.Step +inspect $ 'fairCross `hasNoType` ''SPEC +inspect $ 'fairCross `hasNoType` ''Producer.FairCrossState +#endif + +{-# INLINE crossApply #-} +crossApply :: Int -> Int -> IO () +crossApply value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (UF.crossApply (UF.map (+) s) s) start + +#ifdef INSPECTION +inspect $ 'crossApply `hasNoType` ''S.Step +inspect $ 'crossApply `hasNoType` ''FL.Step +inspect $ 'crossApply `hasNoType` ''SPEC +inspect $ 'crossApply `hasNoType` ''Producer.CrossApplyState +#endif + +{-# INLINE crossWithM #-} +crossWithM :: Int -> Int -> IO () +crossWithM value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (UF.crossWithM (\a b -> return (a + b)) s s) start + +#ifdef INSPECTION +inspect $ 'crossWithM `hasNoType` ''S.Step +inspect $ 'crossWithM `hasNoType` ''FL.Step +inspect $ 'crossWithM `hasNoType` ''SPEC +inspect $ 'crossWithM `hasNoType` ''Producer.CrossState +#endif + +{-# INLINE crossWith #-} +crossWith :: Int -> Int -> IO () +crossWith value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (UF.crossWith (+) s s) start + +#ifdef INSPECTION +inspect $ 'crossWith `hasNoType` ''S.Step +inspect $ 'crossWith `hasNoType` ''FL.Step +inspect $ 'crossWith `hasNoType` ''SPEC +inspect $ 'crossWith `hasNoType` ''Producer.CrossState +#endif + +{-# INLINE fairCrossWithM #-} +fairCrossWithM :: Int -> Int -> IO () +fairCrossWithM value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (UF.fairCrossWithM (\a b -> return (a + b)) s s) start + +#ifdef INSPECTION +inspect $ 'fairCrossWithM `hasNoType` ''S.Step +inspect $ 'fairCrossWithM `hasNoType` ''FL.Step +inspect $ 'fairCrossWithM `hasNoType` ''SPEC +inspect $ 'fairCrossWithM `hasNoType` ''Producer.FairCrossState +#endif + +{-# INLINE fairCrossWith #-} +fairCrossWith :: Int -> Int -> IO () +fairCrossWith value start = + let end = start + nthRoot 2 value + s = source end + in UF.fold FL.drain (UF.fairCrossWith (+) s s) start + +#ifdef INSPECTION +inspect $ 'fairCrossWith `hasNoType` ''S.Step +inspect $ 'fairCrossWith `hasNoType` ''FL.Step +inspect $ 'fairCrossWith `hasNoType` ''SPEC +inspect $ 'fairCrossWith `hasNoType` ''Producer.FairCrossState +#endif + +------------------------------------------------------------------------------- +-- Monad +------------------------------------------------------------------------------- + +-- XXX to keep the benchmarks same as Stream we should use sourceUnfoldrM in +-- all of these, and other benchmarks too. +{-# INLINE concatMapM #-} +concatMapM :: Int -> Int -> Int -> IO () +concatMapM inner outer start = + drainGeneration (UF.concatMapM unfoldInGen unfoldOut) start + + where + + unfoldInGen i = return (UF.supplySecond (i + inner) UF.enumerateFromToIntegral) + unfoldOut = UF.supplySecond (start + outer) UF.enumerateFromToIntegral + +-- The 'bind'-based benchmarks use the Unfold monad ('UF.bind'), which is a +-- concatMap and does not fuse, so the 'Step' constructors remain. +#ifdef INSPECTION +-- inspect $ 'concatMapM `hasNoType` ''S.Step +inspect $ 'concatMapM `hasNoType` ''FL.Step +inspect $ 'concatMapM `hasNoType` ''SPEC +#endif + +{-# INLINE toNull #-} +toNull :: Int -> Int -> IO () +toNull value start = + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + return (x + y) + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + UF.fromPure (x + y) + in UF.fold FL.drain u start + +#ifdef INSPECTION +-- inspect $ 'toNull `hasNoType` ''S.Step +inspect $ 'toNull `hasNoType` ''FL.Step +inspect $ 'toNull `hasNoType` ''SPEC +#endif + +{-# INLINE toNull3 #-} +toNull3 :: Int -> Int -> IO () +toNull3 value start = + let end = start + nthRoot 3 value + src = source end + {- + u = do + x <- src + y <- src + z <- src + return (x + y + z) + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + src `UF.bind` \z -> + UF.fromPure (x + y + z) + in UF.fold FL.drain u start + +#ifdef INSPECTION +-- inspect $ 'toNull3 `hasNoType` ''S.Step +inspect $ 'toNull3 `hasNoType` ''FL.Step +inspect $ 'toNull3 `hasNoType` ''SPEC +#endif + +{-# INLINE toNullConcatMap #-} +toNullConcatMap :: Int -> Int -> IO () +toNullConcatMap value start = + let end = start + nthRoot 2 value + src = source end + u = UF.concatMap (\x -> + UF.concatMap (\y -> + UF.fromPure (x + y)) src) src + in UF.fold FL.drain u start + +{-# INLINE toNull3ConcatMap #-} +toNull3ConcatMap :: Int -> Int -> IO () +toNull3ConcatMap value start = + let end = start + nthRoot 3 value + src = source end + u = UF.concatMap (\x -> + UF.concatMap (\y -> + UF.concatMap (\z -> + UF.fromPure (x + y + z)) src) src) src + in UF.fold FL.drain u start + +{-# INLINE toList #-} +toList :: Int -> Int -> IO [Int] +toList value start = do + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + return (x + y) + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + UF.fromPure (x + y) + in UF.fold FL.toList u start + +#ifdef INSPECTION +-- inspect $ 'toList `hasNoType` ''S.Step +inspect $ 'toList `hasNoType` ''FL.Step +inspect $ 'toList `hasNoType` ''SPEC +#endif + +{-# INLINE toListSome #-} +toListSome :: Int -> Int -> IO [Int] +toListSome value start = do + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + return (x + y) + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + UF.fromPure (x + y) + in UF.fold FL.toList (UF.take 1000 u) start + +#ifdef INSPECTION +-- inspect $ 'toListSome `hasNoType` ''S.Step +inspect $ 'toListSome `hasNoType` ''FL.Step +inspect $ 'toListSome `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllOut #-} +filterAllOut :: Int -> Int -> IO () +filterAllOut value start = do + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + let s = x + y + in if s < 0 + then UF.fromPure s + else UF.nilM (return . const ()) + in UF.fold FL.drain u start + +#ifdef INSPECTION +-- inspect $ 'filterAllOut `hasNoType` ''S.Step +inspect $ 'filterAllOut `hasNoType` ''FL.Step +inspect $ 'filterAllOut `hasNoType` ''SPEC +#endif + +{-# INLINE filterAllIn #-} +filterAllIn :: Int -> Int -> IO () +filterAllIn value start = do + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + let s = x + y + in if s > 0 + then UF.fromPure s + else UF.nilM (return . const ()) + in UF.fold FL.drain u start + +#ifdef INSPECTION +-- inspect $ 'filterAllIn `hasNoType` ''S.Step +inspect $ 'filterAllIn `hasNoType` ''FL.Step +inspect $ 'filterAllIn `hasNoType` ''SPEC +#endif + +{-# INLINE filterSome #-} +filterSome :: Int -> Int -> IO () +filterSome value start = do + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + let s = x + y + in if s > 1100000 + then UF.fromPure s + else UF.nilM (return . const ()) + in UF.fold FL.drain u start + +#ifdef INSPECTION +-- inspect $ 'filterSome `hasNoType` ''S.Step +inspect $ 'filterSome `hasNoType` ''FL.Step +inspect $ 'filterSome `hasNoType` ''SPEC +#endif + +{-# INLINE breakAfterSome #-} +breakAfterSome :: Int -> Int -> IO () +breakAfterSome value start = + let end = start + nthRoot 2 value + src = source end + {- + u = do + x <- src + y <- src + -} + u = src `UF.bind` \x -> + src `UF.bind` \y -> + let s = x + y + in if s > 1100000 + then error "break" + else UF.fromPure s + in do + (_ :: Either ErrorCall ()) <- try $ UF.fold FL.drain u start + return () + +#ifdef INSPECTION +-- inspect $ 'breakAfterSome `hasNoType` ''S.Step +inspect $ 'breakAfterSome `hasNoType` ''FL.Step +inspect $ 'breakAfterSome `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Benchmark ops +------------------------------------------------------------------------------- + +{-# INLINE unfoldEach #-} +unfoldEach :: Int -> Int -> Int -> IO () +unfoldEach inner outer start = do + UF.fold + FL.drain + (UF.unfoldEach (sourceUnfoldrM inner start) (sourceUnfoldrM outer start)) + start + +#ifdef INSPECTION +inspect $ 'unfoldEach `hasNoType` ''S.Step +inspect $ 'unfoldEach `hasNoType` ''FL.Step +inspect $ 'unfoldEach `hasNoType` ''SPEC +inspect $ 'unfoldEach `hasNoType` ''Producer.ConcatState +#endif + +{-# INLINE unfoldEachInterleave #-} +unfoldEachInterleave :: Int -> Int -> Int -> IO () +unfoldEachInterleave inner outer start = do + UF.fold + FL.drain + (UF.unfoldEachInterleave + (sourceUnfoldrM inner start) (sourceUnfoldrM outer start)) + start + +-- 'unfoldEachInterleave' does not fuse: 'Step' and 'SPEC' are not eliminated. +#ifdef INSPECTION +-- inspect $ 'unfoldEachInterleave `hasNoType` ''S.Step +-- inspect $ 'unfoldEachInterleave `hasNoType` ''SPEC +inspect $ 'unfoldEachInterleave `hasNoType` ''FL.Step +#endif + +{-# INLINE concatMapPure #-} +concatMapPure :: Int -> Int -> Int -> IO () +concatMapPure inner outer start = + drainGeneration (UF.concatMap unfoldInGen unfoldOut) start + + where + + unfoldInGen i = UF.supplySecond (i + inner) UF.enumerateFromToIntegral + unfoldOut = UF.supplySecond (start + outer) UF.enumerateFromToIntegral + +#ifdef INSPECTION +-- inspect $ 'concatMapPure `hasNoType` ''S.Step +inspect $ 'concatMapPure `hasNoType` ''FL.Step +inspect $ 'concatMapPure `hasNoType` ''SPEC +#endif + +------------------------------------------------------------------------------- +-- Benchmarks +------------------------------------------------------------------------------- + +o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_transformation_input size = + [ (SpaceO_1, bgroup + "transformation/input" + [ benchIO "lmap" $ lmap size + , benchIO "lmapM" $ lmapM size + , benchIO "both" $ both size + , benchIO "first" $ first size + , benchIO "second" $ second size + , benchIO "consInput" $ consInput size + , benchIO "consInputWith" $ consInputWith size + , benchIO "swap" $ swap size + ]) + ] + +o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_generation size = + [ (SpaceO_1, bgroup + "generation" + [ benchIO "functionM" $ functionM size + , benchIO "function" $ function size + , benchIO "identity" $ identity size + , benchIO "fromEffect" $ fromEffect size + , benchIO "fromPure" $ fromPure size + , benchIO "functionMaybeM" $ functionMaybeM size + , benchIO "fromTuple" $ fromTuple size + , benchIO "unfoldrM" $ unfoldrM size + , benchIO "unfoldr" $ unfoldr size + , benchIO "fromList" $ fromList size + ]) + ] + +o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_transformation size = + [ (SpaceO_1, bgroup + "transformation" + [ benchIO "map" $ map size + , benchIO "mapM" $ mapM size + , benchIO "mapM2" $ mapM2 size + ]) + ] + +o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_filtering size = + [ (SpaceO_1, bgroup + "filtering" + [ benchIO "takeWhileM" $ takeWhileM size + , benchIO "takeWhile" $ takeWhile size + ]) + ] + +o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_zip size = + [ (SpaceO_1, bgroup + "zip" + [ benchIO "zipWithM" $ zipWithM size + , benchIO "zipWith" $ zipWith size + , benchIO "teeZipWith" $ teeZipWith size + , benchIO "interleave" $ interleave size + , benchIO "zipArrowWithM" $ zipArrowWithM size + , benchIO "zipArrowWith" $ zipArrowWith size + ]) + ] + +o_1_space_nested :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_nested size = + [ (SpaceO_1, bgroup + "nested" + [ benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size + , benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size + , benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size + , benchIO "cross outer=inner=(sqrt Max)" $ cross size + , benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size + , benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size + , benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size + , benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size + , benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size + , benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size + + , benchIO "unfoldEach inner=outer=(sqrt Max)" $ unfoldEach sqrtVal sqrtVal + , benchIO "unfoldEach inner=1 outer=Max" $ unfoldEach 1 size + , benchIO "unfoldEach inner=Max outer=1" $ unfoldEach size 1 + , benchIO "unfoldEachInterleave inner=outer=(sqrt Max)" + $ unfoldEachInterleave sqrtVal sqrtVal + , benchIO "unfoldEachInterleave inner=1 outer=Max" + $ unfoldEachInterleave 1 size + , benchIO "unfoldEachInterleave inner=Max outer=1" + $ unfoldEachInterleave size 1 + ]) + ] + + where + + sqrtVal = round $ sqrt (fromIntegral size :: Double) + +o_1_space_concat :: Int -> [(SpaceComplexity, Benchmark)] +o_1_space_concat size = + [ (SpaceO_1, bgroup + "concat" + [ benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal + , benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal + , benchIO "bind2" $ toNull size + , benchIO "bind3" $ toNull3 size + , benchIO "concatMap2" $ toNullConcatMap size + , benchIO "concatMap3" $ toNull3ConcatMap size + , benchIO "breakAfterSome2" $ breakAfterSome size + , benchIO "filterAllOut2" $ filterAllOut size + , benchIO "filterAllIn2" $ filterAllIn size + , benchIO "filterSome2" $ filterSome size + ]) + ] + + where + + sqrtVal = round $ sqrt (fromIntegral size :: Double) + +o_n_space_concat :: Int -> [(SpaceComplexity, Benchmark)] +o_n_space_concat size = + [ (SpaceO_n, bgroup + "concat" + [ benchIO "toList2" $ toList size + , benchIO "toListSome2" $ toListSome size + ]) + ] + +------------------------------------------------------------------------------- +-- Driver +------------------------------------------------------------------------------- + +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = + o_1_space_transformation_input size + ++ o_1_space_generation size + ++ o_1_space_transformation size + ++ o_1_space_filtering size + ++ o_1_space_zip size + ++ o_1_space_nested size + ++ o_1_space_concat size + ++ o_n_space_concat size diff --git a/benchmark/streamly-benchmarks.cabal b/benchmark/streamly-benchmarks.cabal index 234a11b911..ba93669e3f 100644 --- a/benchmark/streamly-benchmarks.cabal +++ b/benchmark/streamly-benchmarks.cabal @@ -596,8 +596,11 @@ benchmark Data.Unbox.Derive.TH benchmark Data.Unfold import: bench-options type: exitcode-stdio-1.0 - hs-source-dirs: . - main-is: Streamly/Benchmark/Data/Unfold.hs + hs-source-dirs: Streamly/Benchmark/Data + main-is: Unfold.hs + other-modules: + Unfold.Type + Unfold.Enumeration if impl(ghcjs) buildable: False else From 3c1bf3d4cc3e660b412881c5edca911e6e16f05f Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 18:18:21 +0530 Subject: [PATCH 09/15] Remove grouping of benchmarks for Unfold benchmarks --- benchmark/Streamly/Benchmark/Data/Unfold.hs | 119 ++++++-------- .../Benchmark/Data/Unfold/Enumeration.hs | 15 +- .../Streamly/Benchmark/Data/Unfold/Type.hs | 145 ++++++++---------- 3 files changed, 114 insertions(+), 165 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold.hs b/benchmark/Streamly/Benchmark/Data/Unfold.hs index 320730953a..74d2e42f82 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold.hs @@ -614,91 +614,70 @@ moduleName = "Data.Unfold" o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation_input size = - [ (SpaceO_1, bgroup - "transformation/input" - [ benchIO "discardFirst" $ discardFirst size - , benchIO "discardSecond" $ discardSecond size - ]) + [ (SpaceO_1, benchIO "discardFirst" $ discardFirst size) + , (SpaceO_1, benchIO "discardSecond" $ discardSecond size) ] o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_generation size = - [ (SpaceO_1, bgroup - "generation" - [ benchIO "fromStream" $ fromStream size - , benchIO "fromStreamK" $ fromStreamK size - , benchIO "fromStreamD" $ fromStreamD size - , benchIO "nilM" $ nilM size - , benchIO "nil" $ nil size - , benchIO "consM" $ consM size - , benchIO "fromListM" $ fromListM size - , benchIO "replicateM" $ replicateM size - , benchIO "repeatM" $ repeatM size - , benchIO "repeat" $ repeat size - , benchIO "iterateM" $ iterateM size - , benchIO "fromIndicesM" $ fromIndicesM size - ]) + [ (SpaceO_1, benchIO "fromStream" $ fromStream size) + , (SpaceO_1, benchIO "fromStreamK" $ fromStreamK size) + , (SpaceO_1, benchIO "fromStreamD" $ fromStreamD size) + , (SpaceO_1, benchIO "nilM" $ nilM size) + , (SpaceO_1, benchIO "nil" $ nil size) + , (SpaceO_1, benchIO "consM" $ consM size) + , (SpaceO_1, benchIO "fromListM" $ fromListM size) + , (SpaceO_1, benchIO "replicateM" $ replicateM size) + , (SpaceO_1, benchIO "repeatM" $ repeatM size) + , (SpaceO_1, benchIO "repeat" $ repeat size) + , (SpaceO_1, benchIO "iterateM" $ iterateM size) + , (SpaceO_1, benchIO "fromIndicesM" $ fromIndicesM size) ] o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation size = - [ (SpaceO_1, bgroup - "transformation" - [ benchIO "postscan" $ postscan size - , benchIO "scanl" $ scanl size - , benchIO "scanlMany" $ scanlMany size - ]) + [ (SpaceO_1, benchIO "postscan" $ postscan size) + , (SpaceO_1, benchIO "scanl" $ scanl size) + , (SpaceO_1, benchIO "scanlMany" $ scanlMany size) ] o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_filtering size = - [ (SpaceO_1, bgroup - "filtering" - [ benchIO "take" $ take size - , benchIO "filter" $ filter size - , benchIO "filterM" $ filterM size - , benchIO "dropOne" $ dropOne size - , benchIO "dropAll" $ dropAll size - , benchIO "dropWhileTrue" $ dropWhileTrue size - , benchIO "dropWhileFalse" $ dropWhileFalse size - , benchIO "dropWhileMTrue" $ dropWhileMTrue size - , benchIO "dropWhileMFalse" $ dropWhileMFalse size - , benchIO "dropWhile" $ dropWhile size - , benchIO "mapMaybe" $ mapMaybe size - , benchIO "mapMaybeM" $ mapMaybeM size - , benchIO "catMaybes" $ catMaybes size - ]) + [ (SpaceO_1, benchIO "take" $ take size) + , (SpaceO_1, benchIO "filter" $ filter size) + , (SpaceO_1, benchIO "filterM" $ filterM size) + , (SpaceO_1, benchIO "dropOne" $ dropOne size) + , (SpaceO_1, benchIO "dropAll" $ dropAll size) + , (SpaceO_1, benchIO "dropWhileTrue" $ dropWhileTrue size) + , (SpaceO_1, benchIO "dropWhileFalse" $ dropWhileFalse size) + , (SpaceO_1, benchIO "dropWhileMTrue" $ dropWhileMTrue size) + , (SpaceO_1, benchIO "dropWhileMFalse" $ dropWhileMFalse size) + , (SpaceO_1, benchIO "dropWhile" $ dropWhile size) + , (SpaceO_1, benchIO "mapMaybe" $ mapMaybe size) + , (SpaceO_1, benchIO "mapMaybeM" $ mapMaybeM size) + , (SpaceO_1, benchIO "catMaybes" $ catMaybes size) ] o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_zip size = - [ (SpaceO_1, bgroup - "zip" - [ benchIO "eitherLeft" $ eitherLeft size - , benchIO "zipRepeat" $ zipRepeat size - ]) + [ (SpaceO_1, benchIO "eitherLeft" $ eitherLeft size) + , (SpaceO_1, benchIO "zipRepeat" $ zipRepeat size) ] o_1_space_nested :: BenchEnv -> Int -> [(SpaceComplexity, Benchmark)] o_1_space_nested env size = - [ (SpaceO_1, bgroup - "nested" - [ benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size - , mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env - $ \inh _ -> foldManySepBy inh - ]) + [ (SpaceO_1, benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size) + , (SpaceO_1, mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env + $ \inh _ -> foldManySepBy inh) ] o_1_space_resource_management :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_resource_management size = - [ (SpaceO_1, bgroup - "resource-management" - [ benchIO "before" $ before size - , benchIO "after_" $ after_ size - , benchIO "afterIO" $ afterIO size - , benchIO "finallyIO" $ finallyIO size - , benchIO "bracketIO" $ bracketIO size - ]) + [ (SpaceO_1, benchIO "before" $ before size) + , (SpaceO_1, benchIO "after_" $ after_ size) + , (SpaceO_1, benchIO "afterIO" $ afterIO size) + , (SpaceO_1, benchIO "finallyIO" $ finallyIO size) + , (SpaceO_1, benchIO "bracketIO" $ bracketIO size) ] ------------------------------------------------------------------------------- @@ -760,16 +739,14 @@ inspect $ hasNoTypeClasses 'readWriteBracket_Unfold o_1_space_copy_read_exceptions :: BenchEnv -> [(SpaceComplexity, Benchmark)] o_1_space_copy_read_exceptions env = - [ (SpaceO_1, bgroup "exceptions" - [ mkBenchSmall "UF.onException" env $ \inh _ -> - readWriteOnExceptionUnfold inh (nullH env) - , mkBenchSmall "UF.handle" env $ \inh _ -> - readWriteHandleExceptionUnfold inh (nullH env) - , mkBenchSmall "UF.finally_" env $ \inh _ -> - readWriteFinally_Unfold inh (nullH env) - , mkBenchSmall "UF.bracket_" env $ \inh _ -> - readWriteBracket_Unfold inh (nullH env) - ]) + [ (SpaceO_1, mkBenchSmall "UF.onException" env $ \inh _ -> + readWriteOnExceptionUnfold inh (nullH env)) + , (SpaceO_1, mkBenchSmall "UF.handle" env $ \inh _ -> + readWriteHandleExceptionUnfold inh (nullH env)) + , (SpaceO_1, mkBenchSmall "UF.finally_" env $ \inh _ -> + readWriteFinally_Unfold inh (nullH env)) + , (SpaceO_1, mkBenchSmall "UF.bracket_" env $ \inh _ -> + readWriteBracket_Unfold inh (nullH env)) ] diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs index 59a9805a1a..d66fd2c360 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs @@ -135,15 +135,12 @@ inspect $ 'enumerateFromToFractional `hasNoType` ''SPEC o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_generation size = - [ (SpaceO_1, bgroup - "generation" - [ benchIO "enumerateFromThenIntegral" $ enumerateFromThenIntegral size - , benchIO "enumerateFromToIntegral" $ enumerateFromToIntegral size - , benchIO "enumerateFromIntegral" $ enumerateFromIntegral size - , benchIO "enumerateFromStepNum" $ enumerateFromStepNum size - , benchIO "enumerateFromNum" $ enumerateFromNum size - , benchIO "enumerateFromToFractional" $ enumerateFromToFractional size - ]) + [ (SpaceO_1, benchIO "enumerateFromThenIntegral" $ enumerateFromThenIntegral size) + , (SpaceO_1, benchIO "enumerateFromToIntegral" $ enumerateFromToIntegral size) + , (SpaceO_1, benchIO "enumerateFromIntegral" $ enumerateFromIntegral size) + , (SpaceO_1, benchIO "enumerateFromStepNum" $ enumerateFromStepNum size) + , (SpaceO_1, benchIO "enumerateFromNum" $ enumerateFromNum size) + , (SpaceO_1, benchIO "enumerateFromToFractional" $ enumerateFromToFractional size) ] ------------------------------------------------------------------------------- diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs index f2f0c60093..6f414576df 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -936,93 +936,74 @@ inspect $ 'concatMapPure `hasNoType` ''SPEC o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation_input size = - [ (SpaceO_1, bgroup - "transformation/input" - [ benchIO "lmap" $ lmap size - , benchIO "lmapM" $ lmapM size - , benchIO "both" $ both size - , benchIO "first" $ first size - , benchIO "second" $ second size - , benchIO "consInput" $ consInput size - , benchIO "consInputWith" $ consInputWith size - , benchIO "swap" $ swap size - ]) + [ (SpaceO_1, benchIO "lmap" $ lmap size) + , (SpaceO_1, benchIO "lmapM" $ lmapM size) + , (SpaceO_1, benchIO "both" $ both size) + , (SpaceO_1, benchIO "first" $ first size) + , (SpaceO_1, benchIO "second" $ second size) + , (SpaceO_1, benchIO "consInput" $ consInput size) + , (SpaceO_1, benchIO "consInputWith" $ consInputWith size) + , (SpaceO_1, benchIO "swap" $ swap size) ] o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_generation size = - [ (SpaceO_1, bgroup - "generation" - [ benchIO "functionM" $ functionM size - , benchIO "function" $ function size - , benchIO "identity" $ identity size - , benchIO "fromEffect" $ fromEffect size - , benchIO "fromPure" $ fromPure size - , benchIO "functionMaybeM" $ functionMaybeM size - , benchIO "fromTuple" $ fromTuple size - , benchIO "unfoldrM" $ unfoldrM size - , benchIO "unfoldr" $ unfoldr size - , benchIO "fromList" $ fromList size - ]) + [ (SpaceO_1, benchIO "functionM" $ functionM size) + , (SpaceO_1, benchIO "function" $ function size) + , (SpaceO_1, benchIO "identity" $ identity size) + , (SpaceO_1, benchIO "fromEffect" $ fromEffect size) + , (SpaceO_1, benchIO "fromPure" $ fromPure size) + , (SpaceO_1, benchIO "functionMaybeM" $ functionMaybeM size) + , (SpaceO_1, benchIO "fromTuple" $ fromTuple size) + , (SpaceO_1, benchIO "unfoldrM" $ unfoldrM size) + , (SpaceO_1, benchIO "unfoldr" $ unfoldr size) + , (SpaceO_1, benchIO "fromList" $ fromList size) ] o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_transformation size = - [ (SpaceO_1, bgroup - "transformation" - [ benchIO "map" $ map size - , benchIO "mapM" $ mapM size - , benchIO "mapM2" $ mapM2 size - ]) + [ (SpaceO_1, benchIO "map" $ map size) + , (SpaceO_1, benchIO "mapM" $ mapM size) + , (SpaceO_1, benchIO "mapM2" $ mapM2 size) ] o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_filtering size = - [ (SpaceO_1, bgroup - "filtering" - [ benchIO "takeWhileM" $ takeWhileM size - , benchIO "takeWhile" $ takeWhile size - ]) + [ (SpaceO_1, benchIO "takeWhileM" $ takeWhileM size) + , (SpaceO_1, benchIO "takeWhile" $ takeWhile size) ] o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_zip size = - [ (SpaceO_1, bgroup - "zip" - [ benchIO "zipWithM" $ zipWithM size - , benchIO "zipWith" $ zipWith size - , benchIO "teeZipWith" $ teeZipWith size - , benchIO "interleave" $ interleave size - , benchIO "zipArrowWithM" $ zipArrowWithM size - , benchIO "zipArrowWith" $ zipArrowWith size - ]) + [ (SpaceO_1, benchIO "zipWithM" $ zipWithM size) + , (SpaceO_1, benchIO "zipWith" $ zipWith size) + , (SpaceO_1, benchIO "teeZipWith" $ teeZipWith size) + , (SpaceO_1, benchIO "interleave" $ interleave size) + , (SpaceO_1, benchIO "zipArrowWithM" $ zipArrowWithM size) + , (SpaceO_1, benchIO "zipArrowWith" $ zipArrowWith size) ] o_1_space_nested :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_nested size = - [ (SpaceO_1, bgroup - "nested" - [ benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size - , benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size - , benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size - , benchIO "cross outer=inner=(sqrt Max)" $ cross size - , benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size - , benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size - , benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size - , benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size - , benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size - , benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size - - , benchIO "unfoldEach inner=outer=(sqrt Max)" $ unfoldEach sqrtVal sqrtVal - , benchIO "unfoldEach inner=1 outer=Max" $ unfoldEach 1 size - , benchIO "unfoldEach inner=Max outer=1" $ unfoldEach size 1 - , benchIO "unfoldEachInterleave inner=outer=(sqrt Max)" - $ unfoldEachInterleave sqrtVal sqrtVal - , benchIO "unfoldEachInterleave inner=1 outer=Max" - $ unfoldEachInterleave 1 size - , benchIO "unfoldEachInterleave inner=Max outer=1" - $ unfoldEachInterleave size 1 - ]) + [ (SpaceO_1, benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size) + , (SpaceO_1, benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size) + , (SpaceO_1, benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size) + , (SpaceO_1, benchIO "cross outer=inner=(sqrt Max)" $ cross size) + , (SpaceO_1, benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size) + , (SpaceO_1, benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size) + , (SpaceO_1, benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size) + , (SpaceO_1, benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size) + , (SpaceO_1, benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size) + , (SpaceO_1, benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size) + , (SpaceO_1, benchIO "unfoldEach inner=outer=(sqrt Max)" $ unfoldEach sqrtVal sqrtVal) + , (SpaceO_1, benchIO "unfoldEach inner=1 outer=Max" $ unfoldEach 1 size) + , (SpaceO_1, benchIO "unfoldEach inner=Max outer=1" $ unfoldEach size 1) + , (SpaceO_1, benchIO "unfoldEachInterleave inner=outer=(sqrt Max)" + $ unfoldEachInterleave sqrtVal sqrtVal) + , (SpaceO_1, benchIO "unfoldEachInterleave inner=1 outer=Max" + $ unfoldEachInterleave 1 size) + , (SpaceO_1, benchIO "unfoldEachInterleave inner=Max outer=1" + $ unfoldEachInterleave size 1) ] where @@ -1031,19 +1012,16 @@ o_1_space_nested size = o_1_space_concat :: Int -> [(SpaceComplexity, Benchmark)] o_1_space_concat size = - [ (SpaceO_1, bgroup - "concat" - [ benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal - , benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal - , benchIO "bind2" $ toNull size - , benchIO "bind3" $ toNull3 size - , benchIO "concatMap2" $ toNullConcatMap size - , benchIO "concatMap3" $ toNull3ConcatMap size - , benchIO "breakAfterSome2" $ breakAfterSome size - , benchIO "filterAllOut2" $ filterAllOut size - , benchIO "filterAllIn2" $ filterAllIn size - , benchIO "filterSome2" $ filterSome size - ]) + [ (SpaceO_1, benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal) + , (SpaceO_1, benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal) + , (SpaceO_1, benchIO "bind2" $ toNull size) + , (SpaceO_1, benchIO "bind3" $ toNull3 size) + , (SpaceO_1, benchIO "concatMap2" $ toNullConcatMap size) + , (SpaceO_1, benchIO "concatMap3" $ toNull3ConcatMap size) + , (SpaceO_1, benchIO "breakAfterSome2" $ breakAfterSome size) + , (SpaceO_1, benchIO "filterAllOut2" $ filterAllOut size) + , (SpaceO_1, benchIO "filterAllIn2" $ filterAllIn size) + , (SpaceO_1, benchIO "filterSome2" $ filterSome size) ] where @@ -1052,11 +1030,8 @@ o_1_space_concat size = o_n_space_concat :: Int -> [(SpaceComplexity, Benchmark)] o_n_space_concat size = - [ (SpaceO_n, bgroup - "concat" - [ benchIO "toList2" $ toList size - , benchIO "toListSome2" $ toListSome size - ]) + [ (SpaceO_n, benchIO "toList2" $ toList size) + , (SpaceO_n, benchIO "toListSome2" $ toListSome size) ] ------------------------------------------------------------------------------- From cde4a132327d17a8d48510ad66418bb1f3005bce Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 18:38:10 +0530 Subject: [PATCH 10/15] Flatten the benchmark lists for Unfold benchmarks --- benchmark/Streamly/Benchmark/Data/Unfold.hs | 131 +++++++----------- .../Benchmark/Data/Unfold/Enumeration.hs | 11 +- .../Streamly/Benchmark/Data/Unfold/Type.hs | 67 ++------- 3 files changed, 59 insertions(+), 150 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold.hs b/benchmark/Streamly/Benchmark/Data/Unfold.hs index 74d2e42f82..6c634ae4b0 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold.hs @@ -612,74 +612,6 @@ inspect $ 'foldManySepBy `hasNoType` ''SPEC moduleName :: String moduleName = "Data.Unfold" -o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_transformation_input size = - [ (SpaceO_1, benchIO "discardFirst" $ discardFirst size) - , (SpaceO_1, benchIO "discardSecond" $ discardSecond size) - ] - -o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_generation size = - [ (SpaceO_1, benchIO "fromStream" $ fromStream size) - , (SpaceO_1, benchIO "fromStreamK" $ fromStreamK size) - , (SpaceO_1, benchIO "fromStreamD" $ fromStreamD size) - , (SpaceO_1, benchIO "nilM" $ nilM size) - , (SpaceO_1, benchIO "nil" $ nil size) - , (SpaceO_1, benchIO "consM" $ consM size) - , (SpaceO_1, benchIO "fromListM" $ fromListM size) - , (SpaceO_1, benchIO "replicateM" $ replicateM size) - , (SpaceO_1, benchIO "repeatM" $ repeatM size) - , (SpaceO_1, benchIO "repeat" $ repeat size) - , (SpaceO_1, benchIO "iterateM" $ iterateM size) - , (SpaceO_1, benchIO "fromIndicesM" $ fromIndicesM size) - ] - -o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_transformation size = - [ (SpaceO_1, benchIO "postscan" $ postscan size) - , (SpaceO_1, benchIO "scanl" $ scanl size) - , (SpaceO_1, benchIO "scanlMany" $ scanlMany size) - ] - -o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_filtering size = - [ (SpaceO_1, benchIO "take" $ take size) - , (SpaceO_1, benchIO "filter" $ filter size) - , (SpaceO_1, benchIO "filterM" $ filterM size) - , (SpaceO_1, benchIO "dropOne" $ dropOne size) - , (SpaceO_1, benchIO "dropAll" $ dropAll size) - , (SpaceO_1, benchIO "dropWhileTrue" $ dropWhileTrue size) - , (SpaceO_1, benchIO "dropWhileFalse" $ dropWhileFalse size) - , (SpaceO_1, benchIO "dropWhileMTrue" $ dropWhileMTrue size) - , (SpaceO_1, benchIO "dropWhileMFalse" $ dropWhileMFalse size) - , (SpaceO_1, benchIO "dropWhile" $ dropWhile size) - , (SpaceO_1, benchIO "mapMaybe" $ mapMaybe size) - , (SpaceO_1, benchIO "mapMaybeM" $ mapMaybeM size) - , (SpaceO_1, benchIO "catMaybes" $ catMaybes size) - ] - -o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_zip size = - [ (SpaceO_1, benchIO "eitherLeft" $ eitherLeft size) - , (SpaceO_1, benchIO "zipRepeat" $ zipRepeat size) - ] - -o_1_space_nested :: BenchEnv -> Int -> [(SpaceComplexity, Benchmark)] -o_1_space_nested env size = - [ (SpaceO_1, benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size) - , (SpaceO_1, mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env - $ \inh _ -> foldManySepBy inh) - ] - -o_1_space_resource_management :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_resource_management size = - [ (SpaceO_1, benchIO "before" $ before size) - , (SpaceO_1, benchIO "after_" $ after_ size) - , (SpaceO_1, benchIO "afterIO" $ afterIO size) - , (SpaceO_1, benchIO "finallyIO" $ finallyIO size) - , (SpaceO_1, benchIO "bracketIO" $ bracketIO size) - ] - ------------------------------------------------------------------------------- -- Unfold Exception Benchmarks ------------------------------------------------------------------------------- @@ -737,9 +669,49 @@ inspect $ hasNoTypeClasses 'readWriteBracket_Unfold -- inspect $ 'readWriteBracket_Unfold `hasNoType` ''S.Step #endif -o_1_space_copy_read_exceptions :: BenchEnv -> [(SpaceComplexity, Benchmark)] -o_1_space_copy_read_exceptions env = - [ (SpaceO_1, mkBenchSmall "UF.onException" env $ \inh _ -> +benchmarks :: BenchEnv -> Int -> [(SpaceComplexity, Benchmark)] +benchmarks env size = + [ (SpaceO_1, benchIO "discardFirst" $ discardFirst size) + , (SpaceO_1, benchIO "discardSecond" $ discardSecond size) + , (SpaceO_1, benchIO "fromStream" $ fromStream size) + , (SpaceO_1, benchIO "fromStreamK" $ fromStreamK size) + , (SpaceO_1, benchIO "fromStreamD" $ fromStreamD size) + , (SpaceO_1, benchIO "nilM" $ nilM size) + , (SpaceO_1, benchIO "nil" $ nil size) + , (SpaceO_1, benchIO "consM" $ consM size) + , (SpaceO_1, benchIO "fromListM" $ fromListM size) + , (SpaceO_1, benchIO "replicateM" $ replicateM size) + , (SpaceO_1, benchIO "repeatM" $ repeatM size) + , (SpaceO_1, benchIO "repeat" $ repeat size) + , (SpaceO_1, benchIO "iterateM" $ iterateM size) + , (SpaceO_1, benchIO "fromIndicesM" $ fromIndicesM size) + , (SpaceO_1, benchIO "postscan" $ postscan size) + , (SpaceO_1, benchIO "scanl" $ scanl size) + , (SpaceO_1, benchIO "scanlMany" $ scanlMany size) + , (SpaceO_1, benchIO "take" $ take size) + , (SpaceO_1, benchIO "filter" $ filter size) + , (SpaceO_1, benchIO "filterM" $ filterM size) + , (SpaceO_1, benchIO "dropOne" $ dropOne size) + , (SpaceO_1, benchIO "dropAll" $ dropAll size) + , (SpaceO_1, benchIO "dropWhileTrue" $ dropWhileTrue size) + , (SpaceO_1, benchIO "dropWhileFalse" $ dropWhileFalse size) + , (SpaceO_1, benchIO "dropWhileMTrue" $ dropWhileMTrue size) + , (SpaceO_1, benchIO "dropWhileMFalse" $ dropWhileMFalse size) + , (SpaceO_1, benchIO "dropWhile" $ dropWhile size) + , (SpaceO_1, benchIO "mapMaybe" $ mapMaybe size) + , (SpaceO_1, benchIO "mapMaybeM" $ mapMaybeM size) + , (SpaceO_1, benchIO "catMaybes" $ catMaybes size) + , (SpaceO_1, benchIO "eitherLeft" $ eitherLeft size) + , (SpaceO_1, benchIO "zipRepeat" $ zipRepeat size) + , (SpaceO_1, benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size) + , (SpaceO_1, mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env + $ \inh _ -> foldManySepBy inh) + , (SpaceO_1, benchIO "before" $ before size) + , (SpaceO_1, benchIO "after_" $ after_ size) + , (SpaceO_1, benchIO "afterIO" $ afterIO size) + , (SpaceO_1, benchIO "finallyIO" $ finallyIO size) + , (SpaceO_1, benchIO "bracketIO" $ bracketIO size) + , (SpaceO_1, mkBenchSmall "UF.onException" env $ \inh _ -> readWriteOnExceptionUnfold inh (nullH env)) , (SpaceO_1, mkBenchSmall "UF.handle" env $ \inh _ -> readWriteHandleExceptionUnfold inh (nullH env)) @@ -749,7 +721,6 @@ o_1_space_copy_read_exceptions env = readWriteBracket_Unfold inh (nullH env)) ] - ------------------------------------------------------------------------------- -- Driver ------------------------------------------------------------------------------- @@ -763,18 +734,10 @@ main = do where allBenchmarks env size = - let allBenches = Prelude.concat - [ Type.benchmarks size - , Enumeration.benchmarks size - , o_1_space_transformation_input size - , o_1_space_generation size - , o_1_space_transformation size - , o_1_space_filtering size - , o_1_space_zip size - , o_1_space_nested env size - , o_1_space_resource_management size - , o_1_space_copy_read_exceptions env - ] + let allBenches = + Type.benchmarks size + ++ Enumeration.benchmarks size + ++ benchmarks env size get x = Prelude.map snd $ Prelude.filter ((==) x . fst) allBenches o_1_space = get SpaceO_1 o_n_space = get SpaceO_n diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs index d66fd2c360..eae2e1806b 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs @@ -133,8 +133,8 @@ inspect $ 'enumerateFromToFractional `hasNoType` ''SPEC -- Benchmarks ------------------------------------------------------------------------------- -o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_generation size = +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = [ (SpaceO_1, benchIO "enumerateFromThenIntegral" $ enumerateFromThenIntegral size) , (SpaceO_1, benchIO "enumerateFromToIntegral" $ enumerateFromToIntegral size) , (SpaceO_1, benchIO "enumerateFromIntegral" $ enumerateFromIntegral size) @@ -142,10 +142,3 @@ o_1_space_generation size = , (SpaceO_1, benchIO "enumerateFromNum" $ enumerateFromNum size) , (SpaceO_1, benchIO "enumerateFromToFractional" $ enumerateFromToFractional size) ] - -------------------------------------------------------------------------------- --- Driver -------------------------------------------------------------------------------- - -benchmarks :: Int -> [(SpaceComplexity, Benchmark)] -benchmarks size = o_1_space_generation size diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs index 6f414576df..23cf69dfb7 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -934,8 +934,8 @@ inspect $ 'concatMapPure `hasNoType` ''SPEC -- Benchmarks ------------------------------------------------------------------------------- -o_1_space_transformation_input :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_transformation_input size = +benchmarks :: Int -> [(SpaceComplexity, Benchmark)] +benchmarks size = [ (SpaceO_1, benchIO "lmap" $ lmap size) , (SpaceO_1, benchIO "lmapM" $ lmapM size) , (SpaceO_1, benchIO "both" $ both size) @@ -944,11 +944,7 @@ o_1_space_transformation_input size = , (SpaceO_1, benchIO "consInput" $ consInput size) , (SpaceO_1, benchIO "consInputWith" $ consInputWith size) , (SpaceO_1, benchIO "swap" $ swap size) - ] - -o_1_space_generation :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_generation size = - [ (SpaceO_1, benchIO "functionM" $ functionM size) + , (SpaceO_1, benchIO "functionM" $ functionM size) , (SpaceO_1, benchIO "function" $ function size) , (SpaceO_1, benchIO "identity" $ identity size) , (SpaceO_1, benchIO "fromEffect" $ fromEffect size) @@ -958,34 +954,18 @@ o_1_space_generation size = , (SpaceO_1, benchIO "unfoldrM" $ unfoldrM size) , (SpaceO_1, benchIO "unfoldr" $ unfoldr size) , (SpaceO_1, benchIO "fromList" $ fromList size) - ] - -o_1_space_transformation :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_transformation size = - [ (SpaceO_1, benchIO "map" $ map size) + , (SpaceO_1, benchIO "map" $ map size) , (SpaceO_1, benchIO "mapM" $ mapM size) , (SpaceO_1, benchIO "mapM2" $ mapM2 size) - ] - -o_1_space_filtering :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_filtering size = - [ (SpaceO_1, benchIO "takeWhileM" $ takeWhileM size) + , (SpaceO_1, benchIO "takeWhileM" $ takeWhileM size) , (SpaceO_1, benchIO "takeWhile" $ takeWhile size) - ] - -o_1_space_zip :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_zip size = - [ (SpaceO_1, benchIO "zipWithM" $ zipWithM size) + , (SpaceO_1, benchIO "zipWithM" $ zipWithM size) , (SpaceO_1, benchIO "zipWith" $ zipWith size) , (SpaceO_1, benchIO "teeZipWith" $ teeZipWith size) , (SpaceO_1, benchIO "interleave" $ interleave size) , (SpaceO_1, benchIO "zipArrowWithM" $ zipArrowWithM size) , (SpaceO_1, benchIO "zipArrowWith" $ zipArrowWith size) - ] - -o_1_space_nested :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_nested size = - [ (SpaceO_1, benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size) + , (SpaceO_1, benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size) , (SpaceO_1, benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size) , (SpaceO_1, benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size) , (SpaceO_1, benchIO "cross outer=inner=(sqrt Max)" $ cross size) @@ -1004,15 +984,7 @@ o_1_space_nested size = $ unfoldEachInterleave 1 size) , (SpaceO_1, benchIO "unfoldEachInterleave inner=Max outer=1" $ unfoldEachInterleave size 1) - ] - - where - - sqrtVal = round $ sqrt (fromIntegral size :: Double) - -o_1_space_concat :: Int -> [(SpaceComplexity, Benchmark)] -o_1_space_concat size = - [ (SpaceO_1, benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal) + , (SpaceO_1, benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal) , (SpaceO_1, benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal) , (SpaceO_1, benchIO "bind2" $ toNull size) , (SpaceO_1, benchIO "bind3" $ toNull3 size) @@ -1022,29 +994,10 @@ o_1_space_concat size = , (SpaceO_1, benchIO "filterAllOut2" $ filterAllOut size) , (SpaceO_1, benchIO "filterAllIn2" $ filterAllIn size) , (SpaceO_1, benchIO "filterSome2" $ filterSome size) + , (SpaceO_n, benchIO "toList2" $ toList size) + , (SpaceO_n, benchIO "toListSome2" $ toListSome size) ] where sqrtVal = round $ sqrt (fromIntegral size :: Double) - -o_n_space_concat :: Int -> [(SpaceComplexity, Benchmark)] -o_n_space_concat size = - [ (SpaceO_n, benchIO "toList2" $ toList size) - , (SpaceO_n, benchIO "toListSome2" $ toListSome size) - ] - -------------------------------------------------------------------------------- --- Driver -------------------------------------------------------------------------------- - -benchmarks :: Int -> [(SpaceComplexity, Benchmark)] -benchmarks size = - o_1_space_transformation_input size - ++ o_1_space_generation size - ++ o_1_space_transformation size - ++ o_1_space_filtering size - ++ o_1_space_zip size - ++ o_1_space_nested size - ++ o_1_space_concat size - ++ o_n_space_concat size From af415e0c1c5cbc32cde52d43a78f60cbc3cf2584 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Fri, 12 Jun 2026 19:10:21 +0530 Subject: [PATCH 11/15] Reorder unfold benchmarks as per the source module exports --- benchmark/Streamly/Benchmark/Data/Unfold.hs | 50 +++++++++----- .../Streamly/Benchmark/Data/Unfold/Type.hs | 69 +++++++++++-------- 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold.hs b/benchmark/Streamly/Benchmark/Data/Unfold.hs index 6c634ae4b0..48a859ac9a 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold.hs @@ -671,54 +671,66 @@ inspect $ hasNoTypeClasses 'readWriteBracket_Unfold benchmarks :: BenchEnv -> Int -> [(SpaceComplexity, Benchmark)] benchmarks env size = - [ (SpaceO_1, benchIO "discardFirst" $ discardFirst size) - , (SpaceO_1, benchIO "discardSecond" $ discardSecond size) - , (SpaceO_1, benchIO "fromStream" $ fromStream size) - , (SpaceO_1, benchIO "fromStreamK" $ fromStreamK size) - , (SpaceO_1, benchIO "fromStreamD" $ fromStreamD size) - , (SpaceO_1, benchIO "nilM" $ nilM size) + -- Basic Constructors + [ (SpaceO_1, benchIO "nilM" $ nilM size) , (SpaceO_1, benchIO "nil" $ nil size) , (SpaceO_1, benchIO "consM" $ consM size) - , (SpaceO_1, benchIO "fromListM" $ fromListM size) - , (SpaceO_1, benchIO "replicateM" $ replicateM size) + -- Generators , (SpaceO_1, benchIO "repeatM" $ repeatM size) , (SpaceO_1, benchIO "repeat" $ repeat size) - , (SpaceO_1, benchIO "iterateM" $ iterateM size) + , (SpaceO_1, benchIO "replicateM" $ replicateM size) , (SpaceO_1, benchIO "fromIndicesM" $ fromIndicesM size) + , (SpaceO_1, benchIO "iterateM" $ iterateM size) + -- From Containers + , (SpaceO_1, benchIO "fromListM" $ fromListM size) + -- From Stream + , (SpaceO_1, benchIO "fromStreamK" $ fromStreamK size) + , (SpaceO_1, benchIO "fromStreamD" $ fromStreamD size) + , (SpaceO_1, benchIO "fromStream" $ fromStream size) + -- Mapping on Input + , (SpaceO_1, benchIO "discardFirst" $ discardFirst size) + , (SpaceO_1, benchIO "discardSecond" $ discardSecond size) + -- Mapping on Output , (SpaceO_1, benchIO "postscan" $ postscan size) , (SpaceO_1, benchIO "scanl" $ scanl size) , (SpaceO_1, benchIO "scanlMany" $ scanlMany size) + , (SpaceO_1, mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env + $ \inh _ -> foldManySepBy inh) + -- Either Wrapped Input + , (SpaceO_1, benchIO "eitherLeft" $ eitherLeft size) + -- Filtering , (SpaceO_1, benchIO "take" $ take size) , (SpaceO_1, benchIO "filter" $ filter size) , (SpaceO_1, benchIO "filterM" $ filterM size) , (SpaceO_1, benchIO "dropOne" $ dropOne size) , (SpaceO_1, benchIO "dropAll" $ dropAll size) + , (SpaceO_1, benchIO "dropWhile" $ dropWhile size) , (SpaceO_1, benchIO "dropWhileTrue" $ dropWhileTrue size) , (SpaceO_1, benchIO "dropWhileFalse" $ dropWhileFalse size) , (SpaceO_1, benchIO "dropWhileMTrue" $ dropWhileMTrue size) , (SpaceO_1, benchIO "dropWhileMFalse" $ dropWhileMFalse size) - , (SpaceO_1, benchIO "dropWhile" $ dropWhile size) , (SpaceO_1, benchIO "mapMaybe" $ mapMaybe size) , (SpaceO_1, benchIO "mapMaybeM" $ mapMaybeM size) , (SpaceO_1, benchIO "catMaybes" $ catMaybes size) - , (SpaceO_1, benchIO "eitherLeft" $ eitherLeft size) - , (SpaceO_1, benchIO "zipRepeat" $ zipRepeat size) + -- Cross product , (SpaceO_1, benchIO "innerJoin outer=inner=(sqrt Max)" $ innerJoin size) - , (SpaceO_1, mkBench "foldMany (Fold.takeEndBy_ (== lf) Fold.drain)" env - $ \inh _ -> foldManySepBy inh) + -- Zip + , (SpaceO_1, benchIO "zipRepeat" $ zipRepeat size) + -- Resource Management , (SpaceO_1, benchIO "before" $ before size) - , (SpaceO_1, benchIO "after_" $ after_ size) , (SpaceO_1, benchIO "afterIO" $ afterIO size) + , (SpaceO_1, benchIO "after_" $ after_ size) , (SpaceO_1, benchIO "finallyIO" $ finallyIO size) + , (SpaceO_1, mkBenchSmall "UF.finally_" env $ \inh _ -> + readWriteFinally_Unfold inh (nullH env)) , (SpaceO_1, benchIO "bracketIO" $ bracketIO size) + , (SpaceO_1, mkBenchSmall "UF.bracket_" env $ \inh _ -> + readWriteBracket_Unfold inh (nullH env)) + -- Exceptions , (SpaceO_1, mkBenchSmall "UF.onException" env $ \inh _ -> readWriteOnExceptionUnfold inh (nullH env)) , (SpaceO_1, mkBenchSmall "UF.handle" env $ \inh _ -> readWriteHandleExceptionUnfold inh (nullH env)) - , (SpaceO_1, mkBenchSmall "UF.finally_" env $ \inh _ -> - readWriteFinally_Unfold inh (nullH env)) - , (SpaceO_1, mkBenchSmall "UF.bracket_" env $ \inh _ -> - readWriteBracket_Unfold inh (nullH env)) ] ------------------------------------------------------------------------------- diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs index 23cf69dfb7..05bce0ad8f 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -936,45 +936,35 @@ inspect $ 'concatMapPure `hasNoType` ''SPEC benchmarks :: Int -> [(SpaceComplexity, Benchmark)] benchmarks size = - [ (SpaceO_1, benchIO "lmap" $ lmap size) - , (SpaceO_1, benchIO "lmapM" $ lmapM size) - , (SpaceO_1, benchIO "both" $ both size) - , (SpaceO_1, benchIO "first" $ first size) - , (SpaceO_1, benchIO "second" $ second size) - , (SpaceO_1, benchIO "consInput" $ consInput size) - , (SpaceO_1, benchIO "consInputWith" $ consInputWith size) - , (SpaceO_1, benchIO "swap" $ swap size) + -- Basic Constructors + [ (SpaceO_1, benchIO "unfoldrM" $ unfoldrM size) + , (SpaceO_1, benchIO "unfoldr" $ unfoldr size) , (SpaceO_1, benchIO "functionM" $ functionM size) , (SpaceO_1, benchIO "function" $ function size) + , (SpaceO_1, benchIO "functionMaybeM" $ functionMaybeM size) , (SpaceO_1, benchIO "identity" $ identity size) + -- From Values , (SpaceO_1, benchIO "fromEffect" $ fromEffect size) , (SpaceO_1, benchIO "fromPure" $ fromPure size) - , (SpaceO_1, benchIO "functionMaybeM" $ functionMaybeM size) - , (SpaceO_1, benchIO "fromTuple" $ fromTuple size) - , (SpaceO_1, benchIO "unfoldrM" $ unfoldrM size) - , (SpaceO_1, benchIO "unfoldr" $ unfoldr size) + -- From Containers , (SpaceO_1, benchIO "fromList" $ fromList size) + , (SpaceO_1, benchIO "fromTuple" $ fromTuple size) + -- Transformations + , (SpaceO_1, benchIO "lmap" $ lmap size) + , (SpaceO_1, benchIO "lmapM" $ lmapM size) + , (SpaceO_1, benchIO "swap" $ swap size) , (SpaceO_1, benchIO "map" $ map size) , (SpaceO_1, benchIO "mapM" $ mapM size) - , (SpaceO_1, benchIO "mapM2" $ mapM2 size) + , (SpaceO_1, benchIO "both" $ both size) + , (SpaceO_1, benchIO "first" $ first size) + , (SpaceO_1, benchIO "second" $ second size) + , (SpaceO_1, benchIO "consInput" $ consInput size) + , (SpaceO_1, benchIO "consInputWith" $ consInputWith size) + -- Trimming , (SpaceO_1, benchIO "takeWhileM" $ takeWhileM size) , (SpaceO_1, benchIO "takeWhile" $ takeWhile size) - , (SpaceO_1, benchIO "zipWithM" $ zipWithM size) - , (SpaceO_1, benchIO "zipWith" $ zipWith size) - , (SpaceO_1, benchIO "teeZipWith" $ teeZipWith size) + -- Nesting , (SpaceO_1, benchIO "interleave" $ interleave size) - , (SpaceO_1, benchIO "zipArrowWithM" $ zipArrowWithM size) - , (SpaceO_1, benchIO "zipArrowWith" $ zipArrowWith size) - , (SpaceO_1, benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size) - , (SpaceO_1, benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size) - , (SpaceO_1, benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size) - , (SpaceO_1, benchIO "cross outer=inner=(sqrt Max)" $ cross size) - , (SpaceO_1, benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size) - , (SpaceO_1, benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size) - , (SpaceO_1, benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size) - , (SpaceO_1, benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size) - , (SpaceO_1, benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size) - , (SpaceO_1, benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size) , (SpaceO_1, benchIO "unfoldEach inner=outer=(sqrt Max)" $ unfoldEach sqrtVal sqrtVal) , (SpaceO_1, benchIO "unfoldEach inner=1 outer=Max" $ unfoldEach 1 size) , (SpaceO_1, benchIO "unfoldEach inner=Max outer=1" $ unfoldEach size 1) @@ -984,18 +974,37 @@ benchmarks size = $ unfoldEachInterleave 1 size) , (SpaceO_1, benchIO "unfoldEachInterleave inner=Max outer=1" $ unfoldEachInterleave size 1) + -- Applicative + , (SpaceO_1, benchIO "crossApply outer=inner=(sqrt Max)" $ toNullAp size) + , (SpaceO_1, benchIO "crossApply2 outer=inner=(sqrt Max)" $ crossApply size) + , (SpaceO_1, benchIO "crossApplySnd outer=inner=(sqrt Max)" $ crossApplySnd size) + , (SpaceO_1, benchIO "crossApplyFst outer=inner=(sqrt Max)" $ crossApplyFst size) + , (SpaceO_1, benchIO "crossWithM outer=inner=(sqrt Max)" $ crossWithM size) + , (SpaceO_1, benchIO "crossWith outer=inner=(sqrt Max)" $ crossWith size) + , (SpaceO_1, benchIO "cross outer=inner=(sqrt Max)" $ cross size) + , (SpaceO_1, benchIO "fairCrossWithM outer=inner=(sqrt Max)" $ fairCrossWithM size) + , (SpaceO_1, benchIO "fairCrossWith outer=inner=(sqrt Max)" $ fairCrossWith size) + , (SpaceO_1, benchIO "fairCross outer=inner=(sqrt Max)" $ fairCross size) + -- Monad , (SpaceO_1, benchIO "concatMapM outer=inner=(sqrt Max)" $ concatMapM sqrtVal sqrtVal) , (SpaceO_1, benchIO "concatMapPure outer=inner=(sqrt Max)" $ concatMapPure sqrtVal sqrtVal) - , (SpaceO_1, benchIO "bind2" $ toNull size) - , (SpaceO_1, benchIO "bind3" $ toNull3 size) , (SpaceO_1, benchIO "concatMap2" $ toNullConcatMap size) , (SpaceO_1, benchIO "concatMap3" $ toNull3ConcatMap size) + , (SpaceO_1, benchIO "bind2" $ toNull size) + , (SpaceO_1, benchIO "bind3" $ toNull3 size) , (SpaceO_1, benchIO "breakAfterSome2" $ breakAfterSome size) , (SpaceO_1, benchIO "filterAllOut2" $ filterAllOut size) , (SpaceO_1, benchIO "filterAllIn2" $ filterAllIn size) , (SpaceO_1, benchIO "filterSome2" $ filterSome size) , (SpaceO_n, benchIO "toList2" $ toList size) , (SpaceO_n, benchIO "toListSome2" $ toListSome size) + , (SpaceO_1, benchIO "zipArrowWithM" $ zipArrowWithM size) + , (SpaceO_1, benchIO "zipArrowWith" $ zipArrowWith size) + , (SpaceO_1, benchIO "zipWithM" $ zipWithM size) + , (SpaceO_1, benchIO "zipWith" $ zipWith size) + , (SpaceO_1, benchIO "teeZipWith" $ teeZipWith size) + -- Deprecated + , (SpaceO_1, benchIO "mapM2" $ mapM2 size) ] where From e4a4e8af5fab7ed99f1163471002e658d83a1b1d Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Sat, 13 Jun 2026 00:54:23 +0530 Subject: [PATCH 12/15] Reduce heap requirement for Unfold benchmarks --- benchmark/Streamly/Benchmark/Data/Unfold/Type.hs | 4 +++- benchmark/streamly-benchmarks.cabal | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs index 05bce0ad8f..617e99e5b1 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -898,7 +898,8 @@ inspect $ 'unfoldEach `hasNoType` ''SPEC inspect $ 'unfoldEach `hasNoType` ''Producer.ConcatState #endif -{-# INLINE unfoldEachInterleave #-} +-- NOTE: Inlining this blows up the heap requirement to 1 GB. +-- {-# INLINE unfoldEachInterleave #-} unfoldEachInterleave :: Int -> Int -> Int -> IO () unfoldEachInterleave inner outer start = do UF.fold @@ -998,6 +999,7 @@ benchmarks size = , (SpaceO_1, benchIO "filterSome2" $ filterSome size) , (SpaceO_n, benchIO "toList2" $ toList size) , (SpaceO_n, benchIO "toListSome2" $ toListSome size) + -- zipWith , (SpaceO_1, benchIO "zipArrowWithM" $ zipArrowWithM size) , (SpaceO_1, benchIO "zipArrowWith" $ zipArrowWith size) , (SpaceO_1, benchIO "zipWithM" $ zipWithM size) diff --git a/benchmark/streamly-benchmarks.cabal b/benchmark/streamly-benchmarks.cabal index ba93669e3f..66297535a2 100644 --- a/benchmark/streamly-benchmarks.cabal +++ b/benchmark/streamly-benchmarks.cabal @@ -605,8 +605,6 @@ benchmark Data.Unfold buildable: False else buildable: True - if flag(limit-build-mem) - ghc-options: +RTS -M1000M -RTS benchmark Data.Unfold.Prelude import: bench-options From 7190c9fcf8117654f49b098081f0482f64347413 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Sat, 13 Jun 2026 01:11:19 +0530 Subject: [PATCH 13/15] Fix hlint in Stream/Parse/Split benchmarks --- .../Benchmark/Data/Stream/Parse/Split.hs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs index 250a783b68..bad4f252ca 100644 --- a/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs +++ b/benchmark/Streamly/Benchmark/Data/Stream/Parse/Split.hs @@ -59,9 +59,9 @@ toarr = Array.fromList . map (fromIntegral . ord) -- | Split on line feed. splitOn :: Handle -> IO Int splitOn inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.splitSepBy_ (== lf) Fold.drain - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION inspect $ hasNoTypeClasses 'splitOn @@ -92,8 +92,8 @@ inspect $ 'wordsBy `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read -- | Split on a word8 sequence. splitOnSeq :: String -> Handle -> IO Int splitOnSeq str inh = - (Stream.fold Fold.length $ Stream.splitSepBySeq_ (toarr str) Fold.drain - $ Handle.read inh) -- >>= print + Stream.fold Fold.length $ Stream.splitSepBySeq_ (toarr str) Fold.drain + $ Handle.read inh -- >>= print #ifdef INSPECTION -- splitOnSeq/splitOnSeq100k: sequence-matching state machine; Step constructors survive. @@ -105,10 +105,10 @@ inspect $ 'splitOnSeq `hasNoType` ''SPEC takeEndBy :: Word8 -> Handle -> IO Int takeEndBy c inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBy (== c) $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION inspect $ hasNoTypeClasses 'takeEndBy @@ -121,10 +121,10 @@ inspect $ 'takeEndBy `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read takeEndBy_ :: Word8 -> Handle -> IO Int takeEndBy_ c inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBy_ (== c) $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION inspect $ hasNoTypeClasses 'takeEndBy_ @@ -137,10 +137,10 @@ inspect $ 'takeEndBy_ `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read takeEndBySeq :: String -> Handle -> IO Int takeEndBySeq str inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBySeq (toarr str) $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- takeEndBySeq/takeEndBySeq_: KR state machine; Step constructors survive @@ -153,10 +153,10 @@ inspect $ 'takeEndBySeq `hasNoType` ''SPEC takeEndBySeq_ :: String -> Handle -> IO Int takeEndBySeq_ str inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBySeq_ (toarr str) $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- inspect $ hasNoTypeClasses 'takeEndBySeq_ @@ -168,10 +168,10 @@ inspect $ 'takeEndBySeq_ `hasNoType` ''SPEC takeEndBySeq100k :: Handle -> IO Int takeEndBySeq100k inh = do arr <- Stream.fold Array.create $ Stream.replicate 100000 123 - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBySeq arr $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- inspect $ hasNoTypeClasses 'takeEndBySeq100k @@ -183,10 +183,10 @@ inspect $ 'takeEndBySeq100k `hasNoType` ''SPEC takeEndBySeq_100k :: Handle -> IO Int takeEndBySeq_100k inh = do arr <- Stream.fold Array.create $ Stream.replicate 100000 123 - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.takeEndBySeq_ arr $ Stream.filter (/= fromIntegral (ord 'a')) - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- inspect $ hasNoTypeClasses 'takeEndBySeq_100k @@ -199,9 +199,9 @@ inspect $ 'takeEndBySeq_100k `hasNoType` ''SPEC splitOnSeq100k :: Handle -> IO Int splitOnSeq100k inh = do arr <- Stream.fold Array.create $ Stream.replicate 100000 123 - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.splitSepBySeq_ arr Fold.drain - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- inspect $ hasNoTypeClasses 'splitOnSeq100k @@ -213,9 +213,9 @@ inspect $ 'splitOnSeq100k `hasNoType` ''SPEC -- | Split on suffix sequence. splitOnSuffixSeq :: String -> Handle -> IO Int splitOnSuffixSeq str inh = - (Stream.fold Fold.length + Stream.fold Fold.length $ Stream.splitOnSuffixSeq False (toarr str) Fold.drain - $ Handle.read inh) -- >>= print + $ Handle.read inh -- >>= print #ifdef INSPECTION -- splitOnSuffixSeq/splitWithSuffixSeq: sequence-matching state machine; Step constructors survive. From 62c7c98a67d22e0e2065de4daa7cdc4997160b3c Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Sat, 13 Jun 2026 01:15:33 +0530 Subject: [PATCH 14/15] Fix hlint in Data.Unfold benchmarks --- .../Streamly/Benchmark/Data/Unfold/Enumeration.hs | 12 ++++++------ benchmark/Streamly/Benchmark/Data/Unfold/Type.hs | 12 +++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs index eae2e1806b..f22b6b1196 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Enumeration.hs @@ -48,7 +48,7 @@ benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f {-# INLINE drainGeneration #-} drainGeneration :: Monad m => Unfold m a b -> a -> m () -drainGeneration unf seed = UF.fold FL.drain unf seed +drainGeneration = UF.fold FL.drain ------------------------------------------------------------------------------- -- Stream generation @@ -82,8 +82,8 @@ inspect $ 'enumerateFromToIntegral `hasNoType` ''SPEC {-# INLINE enumerateFromIntegral #-} enumerateFromIntegral :: Int -> Int -> IO () -enumerateFromIntegral size start = - drainGeneration (UF.take size UF.enumerateFromIntegral) start +enumerateFromIntegral size = + drainGeneration (UF.take size UF.enumerateFromIntegral) #ifdef INSPECTION inspect $ 'enumerateFromIntegral `hasNoType` ''S.Step @@ -94,7 +94,7 @@ inspect $ 'enumerateFromIntegral `hasNoType` ''SPEC {-# INLINE enumerateFromStepNum #-} enumerateFromStepNum :: Int -> Int -> IO () enumerateFromStepNum size start = - drainGeneration (UF.take size (UF.enumerateFromThenNum)) (start, 1) + drainGeneration (UF.take size UF.enumerateFromThenNum) (start, 1) #ifdef INSPECTION inspect $ 'enumerateFromStepNum `hasNoType` ''S.Step @@ -104,7 +104,7 @@ inspect $ 'enumerateFromStepNum `hasNoType` ''SPEC {-# INLINE enumerateFromNum #-} enumerateFromNum :: Int -> Int -> IO () -enumerateFromNum size start = drainGeneration (UF.take size UF.enumerateFromNum) start +enumerateFromNum size = drainGeneration (UF.take size UF.enumerateFromNum) #ifdef INSPECTION inspect $ 'enumerateFromNum `hasNoType` ''S.Step @@ -115,7 +115,7 @@ inspect $ 'enumerateFromNum `hasNoType` ''SPEC {-# INLINE enumerateFromToFractional #-} enumerateFromToFractional :: Int -> Int -> IO () enumerateFromToFractional size start = - let intToDouble x = (fromInteger (fromIntegral x)) :: Double + let intToDouble x = fromInteger (fromIntegral x) :: Double in drainGeneration ( UF.supplySecond (intToDouble $ start + size) diff --git a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs index 617e99e5b1..c7189ba533 100644 --- a/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs +++ b/benchmark/Streamly/Benchmark/Data/Unfold/Type.hs @@ -64,12 +64,12 @@ source n = UF.supplySecond n UF.enumerateFromToIntegral {-# INLINE drainGeneration #-} drainGeneration :: Monad m => Unfold m a b -> a -> m () -drainGeneration unf seed = UF.fold FL.drain unf seed +drainGeneration = UF.fold FL.drain {-# INLINE drainTransformation #-} drainTransformation :: Monad m => Unfold m a b -> (Unfold m a b -> Unfold m c d) -> c -> m () -drainTransformation unf f seed = drainGeneration (f unf) seed +drainTransformation unf f = drainGeneration (f unf) {-# INLINE drainTransformationDefault #-} drainTransformationDefault :: @@ -85,7 +85,7 @@ drainProduct :: -> (Unfold m a b -> Unfold m c d -> Unfold m e f) -> e -> m () -drainProduct unf1 unf2 f seed = drainGeneration (f unf1 unf2) seed +drainProduct unf1 unf2 f = drainGeneration (f unf1 unf2) {-# INLINE drainProductDefault #-} drainProductDefault :: @@ -153,11 +153,10 @@ inspect $ 'first `hasNoType` ''SPEC {-# INLINE second #-} second :: Int -> Int -> IO () -second size start = +second size = drainTransformation (UF.take size UF.enumerateFromThenIntegral) (UF.supplySecond 1) - start #ifdef INSPECTION inspect $ 'second `hasNoType` ''S.Step @@ -377,11 +376,10 @@ inspect $ 'mapM `hasNoType` ''SPEC {-# INLINE mapM2 #-} mapM2 :: Int -> Int -> IO () -mapM2 size start = +mapM2 size = drainTransformationDefault size (UF.mapM (\(a, b) -> return $ a + b) . UF.carryInput) - start #ifdef INSPECTION inspect $ 'mapM2 `hasNoType` ''S.Step From 467c509f849a012b866a330c2893770abe7c5c39 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Sat, 13 Jun 2026 01:19:04 +0530 Subject: [PATCH 15/15] Disable hlint in benchmarks and tests --- .circleci/config.yml | 2 +- .hlint.ignore | 20 -------------------- .hlint.yaml | 9 --------- 3 files changed, 1 insertion(+), 30 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 30e1790cf9..fad20a3ddb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -178,7 +178,7 @@ jobs: DISABLE_SDIST_BUILD: "yes" HLINT_VERSION: 3.10 HLINT_OPTIONS: lint - HLINT_TARGETS: core/src src test benchmark + HLINT_TARGETS: core/src src command: | bash -c "$PACKCHECK hlint" || exit 1 echo "Checking trailing spaces..." diff --git a/.hlint.ignore b/.hlint.ignore index 7e4ca4a230..f22377dfa1 100644 --- a/.hlint.ignore +++ b/.hlint.ignore @@ -5,23 +5,3 @@ src/Streamly/Internal/Data/SmallArray/Type.hs src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs src/Streamly/Internal/Data/Stream/Serial.hs src/Streamly/Internal/Data/Stream/Zip.hs -test/Streamly/Test/Data/Array.hs -test/Streamly/Test/Data/Parser.hs -test/Streamly/Test/Data/Parser/Common.hs -test/Streamly/Test/Data/ParserK.hs -test/Streamly/Test/Data/SmallArray.hs -test/Streamly/Test/Data/Unfold.hs -test/Streamly/Test/FileSystem/Event.hs -test/Streamly/Test/Prelude/Concurrent.hs -test/lib/Streamly/Test/Prelude/Common.hs -test/Streamly/Test/Prelude/Fold.hs -test/Streamly/Test/Prelude/Rate.hs -test/Streamly/Test/Prelude/Serial.hs -test/Streamly/Test/Unicode/Stream.hs -test/Streamly/Test/Data/Unbox.hs -benchmark/lib/Streamly/Benchmark/Common.hs -benchmark/lib/Streamly/Benchmark/Common/Handle.hs -benchmark/Streamly/Benchmark/Data/Array.hs -benchmark/Streamly/Benchmark/Data/ParserK.hs -benchmark/Streamly/Benchmark/Data/Unfold.hs -benchmark/Streamly/Benchmark/FileSystem/Handle.hs diff --git a/.hlint.yaml b/.hlint.yaml index 6ac51232c8..18b5993ba8 100644 --- a/.hlint.yaml +++ b/.hlint.yaml @@ -34,14 +34,6 @@ - ignore: {name: "Redundant <*", within: Stream.Expand} - ignore: {name: "Redundant bracket", within: Stream.Split} - ignore: {name: "Use isDigit", within: Streamly.Internal.Unicode.Parser} -# These tests intentionally test functor/monoid laws, so the hints are -# expected and should be suppressed -- ignore: {name: "Functor law", within: [Streamly.Test.Data.Stream.MkType.Ahead, Streamly.Test.Data.Stream.MkType.ZipSerial, Streamly.Test.Data.Stream.MkType.ZipAsync]} -- ignore: {name: "Monoid law, left identity", within: [Streamly.Test.Data.Stream.MkType.Ahead, Streamly.Test.Data.Stream.MkType.ZipSerial, Streamly.Test.Data.Stream.MkType.ZipAsync]} -- ignore: {name: "Monoid law, right identity", within: [Streamly.Test.Data.Stream.MkType.Ahead, Streamly.Test.Data.Stream.MkType.ZipSerial, Streamly.Test.Data.Stream.MkType.ZipAsync]} -- ignore: {name: "Use fold", within: Streamly.Test.Data.Stream.MkType.Ahead} -- ignore: {name: "Fuse foldr/map", within: Streamly.Test.Data.Stream.MkType.Ahead} -- ignore: {name: "Avoid lambda using `infix`", within: Streamly.Test.Data.Stream.MkType.Ahead} # Specify additional command line arguments # @@ -50,7 +42,6 @@ , --cpp-include=src/Streamly/Internal/Data/Stream , --cpp-include=core/src/Streamly/Internal/Data/Array , --cpp-include=core/src/doctest - , --cpp-include=test , --cpp-define=CABAL_OS_LINUX , --cpp-define=linux_HOST_OS ]