on master help sign in
Move the concat benchmark to Unfold module
e686510Harendra Kumar
benchmark/Streamly/Benchmark/Data/NestedUnfoldOps.hsdeleted−40
@@ -1,40 +0,0 @@
1−-- |
2−-- Module : NestedUnfoldOps
3−-- Copyright : (c) 2019 Composewell Technologies
4−--
5−-- License : BSD3
6−-- Maintainer : streamly@composewell.com
7−
8−module Streamly.Benchmark.Data.NestedUnfoldOps where
9−
10−import Control.Monad.IO.Class (MonadIO (..))
11−import Streamly.Internal.Data.Unfold (Unfold)
12−
13−import qualified Streamly.Internal.Data.Unfold as UF
14−import qualified Streamly.Internal.Data.Fold as FL
15−
16−-- n * (n + 1) / 2 == linearCount
17−concatCount :: Int -> Int
18−concatCount linearCount =
19− round (((1 + 8 * fromIntegral linearCount)**(1/2::Double) - 1) / 2)
20−
21−-------------------------------------------------------------------------------
22−-- Stream generation and elimination
23−-------------------------------------------------------------------------------
24−
25−-- generate numbers up to the argument value
26−{-# INLINE source #-}
27−source :: Monad m => Int -> Unfold m Int Int
28−source n = UF.enumerateFromToIntegral n
29−
30−-------------------------------------------------------------------------------
31−-- Benchmark ops
32−-------------------------------------------------------------------------------
33−
34−{-# INLINE concat #-}
35−concat :: MonadIO m => Int -> Int -> m ()
36−concat linearCount start = do
37− let end = start + concatCount linearCount
38− UF.fold
39− (UF.concat (source end) (source end))
40− FL.drain start
benchmark/Streamly/Benchmark/Data/Unfold.hs+36−11
@@ -26,7 +26,6 @@ import System.IO (Handle, hClose)
26import System.Random (randomRIO)
26import System.Random (randomRIO)
27
27
28import qualified Prelude
28import qualified Prelude
29−import qualified Streamly.Benchmark.Data.NestedUnfoldOps as Nested
30import qualified Streamly.FileSystem.Handle as FH
29import qualified Streamly.FileSystem.Handle as FH
31import qualified Streamly.Internal.Data.Fold as FL
30import qualified Streamly.Internal.Data.Fold as FL
32import qualified Streamly.Internal.Data.Unfold as UF
31import qualified Streamly.Internal.Data.Unfold as UF
@@ -48,6 +47,15 @@ import Test.Inspection
48benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark
47benchIO :: (NFData b) => String -> (Int -> IO b) -> Benchmark
49benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f
48benchIO name f = bench name $ nfIO $ randomRIO (1,1) >>= f
50
49
50+-------------------------------------------------------------------------------
51+-- Stream generation and elimination
52+-------------------------------------------------------------------------------
53+
54+-- generate numbers up to the argument value
55+{-# INLINE source #-}
56+source :: Monad m => Int -> Unfold m Int Int
57+source n = UF.enumerateFromToIntegral n
58+
51-------------------------------------------------------------------------------
59-------------------------------------------------------------------------------
52-- Benchmark helpers
60-- Benchmark helpers
53-------------------------------------------------------------------------------
61-------------------------------------------------------------------------------
@@ -404,7 +412,7 @@ teeZipWith size start =
404toNullAp :: Monad m => Int -> Int -> m ()
412toNullAp :: Monad m => Int -> Int -> m ()
405toNullAp value start =
413toNullAp value start =
406 let end = start + nthRoot 2 value
414 let end = start + nthRoot 2 value
407− s = Nested.source end
415+ s = source end
408 in UF.fold ((+) <$> s <*> s) FL.drain start
416 in UF.fold ((+) <$> s <*> s) FL.drain start
409
417
410{-# INLINE _apDiscardFst #-}
418{-# INLINE _apDiscardFst #-}
@@ -437,7 +445,7 @@ concatMapM value start =
437toNull :: Monad m => Int -> Int -> m ()
445toNull :: Monad m => Int -> Int -> m ()
438toNull value start =
446toNull value start =
439 let end = start + nthRoot 2 value
447 let end = start + nthRoot 2 value
440− src = Nested.source end
448+ src = source end
441 u = do
449 u = do
442 x <- src
450 x <- src
443 y <- src
451 y <- src
@@ -449,7 +457,7 @@ toNull value start =
449toNull3 :: Monad m => Int -> Int -> m ()
457toNull3 :: Monad m => Int -> Int -> m ()
450toNull3 value start =
458toNull3 value start =
451 let end = start + nthRoot 3 value
459 let end = start + nthRoot 3 value
452− src = Nested.source end
460+ src = source end
453 u = do
461 u = do
454 x <- src
462 x <- src
455 y <- src
463 y <- src
@@ -461,7 +469,7 @@ toNull3 value start =
461toList :: Monad m => Int -> Int -> m [Int]
469toList :: Monad m => Int -> Int -> m [Int]
462toList value start = do
470toList value start = do
463 let end = start + nthRoot 2 value
471 let end = start + nthRoot 2 value
464− src = Nested.source end
472+ src = source end
465 u = do
473 u = do
466 x <- src
474 x <- src
467 y <- src
475 y <- src
@@ -472,7 +480,7 @@ toList value start = do
472toListSome :: Monad m => Int -> Int -> m [Int]
480toListSome :: Monad m => Int -> Int -> m [Int]
473toListSome value start = do
481toListSome value start = do
474 let end = start + nthRoot 2 value
482 let end = start + nthRoot 2 value
475− src = Nested.source end
483+ src = source end
476 u = do
484 u = do
477 x <- src
485 x <- src
478 y <- src
486 y <- src
@@ -483,7 +491,7 @@ toListSome value start = do
483filterAllOut :: Monad m => Int -> Int -> m ()
491filterAllOut :: Monad m => Int -> Int -> m ()
484filterAllOut value start = do
492filterAllOut value start = do
485 let end = start + nthRoot 2 value
493 let end = start + nthRoot 2 value
486− src = Nested.source end
494+ src = source end
487 u = do
495 u = do
488 x <- src
496 x <- src
489 y <- src
497 y <- src
@@ -497,7 +505,7 @@ filterAllOut value start = do
497filterAllIn :: Monad m => Int -> Int -> m ()
505filterAllIn :: Monad m => Int -> Int -> m ()
498filterAllIn value start = do
506filterAllIn value start = do
499 let end = start + nthRoot 2 value
507 let end = start + nthRoot 2 value
500− src = Nested.source end
508+ src = source end
501 u = do
509 u = do
502 x <- src
510 x <- src
503 y <- src
511 y <- src
@@ -511,7 +519,7 @@ filterAllIn value start = do
511filterSome :: Monad m => Int -> Int -> m ()
519filterSome :: Monad m => Int -> Int -> m ()
512filterSome value start = do
520filterSome value start = do
513 let end = start + nthRoot 2 value
521 let end = start + nthRoot 2 value
514− src = Nested.source end
522+ src = source end
515 u = do
523 u = do
516 x <- src
524 x <- src
517 y <- src
525 y <- src
@@ -525,7 +533,7 @@ filterSome value start = do
525breakAfterSome :: Int -> Int -> IO ()
533breakAfterSome :: Int -> Int -> IO ()
526breakAfterSome value start =
534breakAfterSome value start =
527 let end = start + nthRoot 2 value
535 let end = start + nthRoot 2 value
528− src = Nested.source end
536+ src = source end
529 u = do
537 u = do
530 x <- src
538 x <- src
531 y <- src
539 y <- src
@@ -537,6 +545,23 @@ breakAfterSome value start =
537 (_ :: Either ErrorCall ()) <- try $ UF.fold u FL.drain start
545 (_ :: Either ErrorCall ()) <- try $ UF.fold u FL.drain start
538 return ()
546 return ()
539
547
548+-------------------------------------------------------------------------------
549+-- Benchmark ops
550+-------------------------------------------------------------------------------
551+
552+-- n * (n + 1) / 2 == linearCount
553+concatCount :: Int -> Int
554+concatCount linearCount =
555+ round (((1 + 8 * fromIntegral linearCount)**(1/2::Double) - 1) / 2)
556+
557+{-# INLINE concat #-}
558+concat :: Monad m => Int -> Int -> m ()
559+concat linearCount start = do
560+ let end = start + concatCount linearCount
561+ UF.fold
562+ (UF.concat (source end) (source end))
563+ FL.drain start
564+
540-------------------------------------------------------------------------------
565-------------------------------------------------------------------------------
541-- Benchmarks
566-- Benchmarks
542-------------------------------------------------------------------------------
567-------------------------------------------------------------------------------
@@ -649,7 +674,7 @@ o_1_space_nested size =
649 , benchIO "filterAllIn" $ filterAllIn size
674 , benchIO "filterAllIn" $ filterAllIn size
650 , benchIO "filterSome" $ filterSome size
675 , benchIO "filterSome" $ filterSome size
651
676
652− , benchIO "concat" $ Nested.concat size
677+ , benchIO "concat" $ concat size
653 ]
678 ]
654 ]
679 ]
655
680
benchmark/streamly-benchmarks.cabal−1
@@ -256,7 +256,6 @@ benchmark Data.Unfold
256 type: exitcode-stdio-1.0
256 type: exitcode-stdio-1.0
257 hs-source-dirs: .
257 hs-source-dirs: .
258 main-is: Streamly/Benchmark/Data/Unfold.hs
258 main-is: Streamly/Benchmark/Data/Unfold.hs
259− other-modules: Streamly.Benchmark.Data.NestedUnfoldOps
260 if impl(ghcjs)
259 if impl(ghcjs)
261 buildable: False
260 buildable: False
262 else
261 else