on master help sign in
Add multiple micro-benchmarks

* bench:FileSystem.Handle - splitWithSuffix * bench:Prelude.Serial - findM - scanl1M' - concatMapM

commit8477487b33491db74ac94b8da513267f7e780243 authoradithyaov <adithya@composewell.com> dateThu 12 Nov 2020, 09:05 (6 years ago) parent053a38c
2 files changed+40
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+15 benchmark/Streamly/Benchmark/Prelude/Serial.hs+25
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+15
@@ -418,6 +418,19 @@ inspect $ 'splitOnSuffix `hasNoType` ''IUF.ConcatState -- FH.read/UF.concat
418418inspect $ 'splitOnSuffix `hasNoType` ''A.ReadUState -- FH.read/A.read
419419#endif
420420
421+-- | Split suffix with line feed.
422+splitWithSuffix :: Handle -> IO Int
423+splitWithSuffix inh =
424+ (S.length $ S.splitWithSuffix (== lf) FL.drain
425+ $ S.unfold FH.read inh) -- >>= print
426+
427+#ifdef INSPECTION
428+inspect $ hasNoTypeClasses 'splitWithSuffix
429+inspect $ 'splitWithSuffix `hasNoType` ''Step
430+inspect $ 'splitWithSuffix `hasNoType` ''IUF.ConcatState -- FH.read/UF.concat
431+inspect $ 'splitWithSuffix `hasNoType` ''A.ReadUState -- FH.read/A.read
432+#endif
433+
421434-- | Split on line feed.
422435parseManySepBy :: Handle -> IO Int
423436parseManySepBy inh =
@@ -477,6 +490,8 @@ o_1_space_reduce_read_split env =
477490 splitOn inh
478491 , mkBench "S.splitOnSuffix (== lf) FL.drain" env $ \inh _ ->
479492 splitOnSuffix inh
493+ , mkBench "S.splitWithSuffix (== lf) FL.drain" env $ \inh _ ->
494+ splitWithSuffix inh
480495 , mkBench "S.splitOnSeq \"\" FL.drain" env $ \inh _ ->
481496 splitOnSeq "" inh
482497 , mkBench "S.splitOnSuffixSeq \"\" FL.drain" env $ \inh _ ->
benchmark/Streamly/Benchmark/Prelude/Serial.hs+25
@@ -497,6 +497,10 @@ or value = S.or . S.map (> (value + 1))
497497find :: Monad m => Int -> SerialT m Int -> m (Maybe Int)
498498find value = S.find (== (value + 1))
499499
500+{-# INLINE findM #-}
501+findM :: Monad m => Int -> SerialT m Int -> m (Maybe Int)
502+findM value = S.findM (\z -> return $ z == (value + 1))
503+
500504{-# INLINE findIndex #-}
501505findIndex :: Monad m => Int -> SerialT m Int -> m (Maybe Int)
502506findIndex value = S.findIndex (== (value + 1))
@@ -606,6 +610,7 @@ o_1_space_elimination_folds value =
606610
607611 , bench "the" $ nfIO $ randomRIO (1,1) >>= the . repeat value
608612 , benchIOSink value "find" (find value)
613+ , benchIOSink value "findM" (findM value)
609614 -- , benchIOSink value "lookupFirst" (lookup 1)
610615 , benchIOSink value "lookupNever" (lookup (value + 1))
611616 , benchIOSink value "(!!)" ((!!) value)
@@ -867,6 +872,10 @@ scanlM' n = composeN n $ S.scanlM' (\b a -> return $ b + a) (return 0)
867872scanl1' :: MonadIO m => Int -> SerialT m Int -> m ()
868873scanl1' n = composeN n $ S.scanl1' (+)
869874
875+{-# INLINE scanl1M' #-}
876+scanl1M' :: MonadIO m => Int -> SerialT m Int -> m ()
877+scanl1M' n = composeN n $ S.scanl1M' (\b a -> return $ b + a)
878+
870879{-# INLINE postscanl' #-}
871880postscanl' :: MonadIO m => Int -> SerialT m Int -> m ()
872881postscanl' n = composeN n $ S.postscanl' (+) 0
@@ -952,6 +961,7 @@ o_1_space_mapping value =
952961 , benchIOSink value "scanl'" (scanl' 1)
953962 , benchIOSink value "scanl1'" (scanl1' 1)
954963 , benchIOSink value "scanlM'" (scanlM' 1)
964+ , benchIOSink value "scanl1M'" (scanl1M' 1)
955965 , benchIOSink value "postscanl'" (postscanl' 1)
956966 , benchIOSink value "postscanlM'" (postscanlM' 1)
957967
@@ -968,6 +978,7 @@ o_1_space_mappingX4 value =
968978 , benchIOSink value "scanl'" (scanl' 4)
969979 , benchIOSink value "scanl1'" (scanl1' 4)
970980 , benchIOSink value "scanlM'" (scanlM' 4)
981+ , benchIOSink value "scanl1M'" (scanl1M' 4)
971982 , benchIOSink value "postscanl'" (postscanl' 4)
972983 , benchIOSink value "postscanlM'" (postscanlM' 4)
973984
@@ -1752,6 +1763,13 @@ concatMap outer inner n =
17521763 (\_ -> sourceUnfoldrM inner n)
17531764 (sourceUnfoldrM outer n)
17541765
1766+{-# INLINE concatMapM #-}
1767+concatMapM :: Int -> Int -> Int -> IO ()
1768+concatMapM outer inner n =
1769+ S.drain $ S.concatMapM
1770+ (\_ -> return $ sourceUnfoldrM inner n)
1771+ (sourceUnfoldrM outer n)
1772+
17551773#ifdef INSPECTION
17561774inspect $ hasNoTypeClasses 'concatMap
17571775inspect $ 'concatMap `hasNoType` ''SPEC
@@ -1842,6 +1860,13 @@ o_1_space_concat value = sqrtVal `seq`
18421860 , benchIOSrc1 "concatMap (1 of n)"
18431861 (concatMap 1 value)
18441862
1863+ , benchIOSrc1 "concatMapM (n of 1)"
1864+ (concatMapM value 1)
1865+ , benchIOSrc1 "concatMapM (sqrt n of sqrt n)"
1866+ (concatMapM sqrtVal sqrtVal)
1867+ , benchIOSrc1 "concatMapM (1 of n)"
1868+ (concatMapM 1 value)
1869+
18451870 -- This is for comparison with foldMapWith
18461871 , benchIOSrc serially "concatMapWithId (n of 1) (fromFoldable)"
18471872 (S.concatMapWith serial id . sourceConcatMapId value)