on 0d49be3 help sign in
148 lines 4.4 kB history raw
1-- |
2-- Module : Streamly.Benchmark.Data.ArrayOps
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
10{-# LANGUAGE CPP #-}
11{-# LANGUAGE DeriveAnyClass #-}
12{-# LANGUAGE DeriveGeneric #-}
13{-# LANGUAGE FlexibleContexts #-}
14{-# LANGUAGE ScopedTypeVariables #-}
16module Streamly.Benchmark.Data.ArrayOps where
18import Control.Monad.IO.Class (MonadIO)
19import Prelude (Int, Bool, (+), ($), (==), (>), (.), Maybe(..), undefined)
20import qualified Prelude as P
21#ifdef DEVBUILD
22import qualified Data.Foldable as F
23#endif
25import qualified Streamly as S hiding (foldMapWith, runStream)
26import qualified Streamly.Data.Array as A
27import qualified Streamly.Prelude as S
29value :: Int
30value = 100000
32-------------------------------------------------------------------------------
33-- Benchmark ops
34-------------------------------------------------------------------------------
36-------------------------------------------------------------------------------
37-- Stream generation and elimination
38-------------------------------------------------------------------------------
40type Stream = A.Array
42{-# INLINE sourceUnfoldr #-}
43sourceUnfoldr :: MonadIO m => Int -> m (Stream Int)
44sourceUnfoldr n = S.fold (A.writeN value) $ S.unfoldr step n
45 where
46 step cnt =
47 if cnt > n + value
48 then Nothing
49 else (Just (cnt, cnt + 1))
51{-# INLINE sourceIntFromTo #-}
52sourceIntFromTo :: MonadIO m => Int -> m (Stream Int)
53sourceIntFromTo n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
55{-# INLINE sourceIntFromToFromStream #-}
56sourceIntFromToFromStream :: MonadIO m => Int -> m (Stream Int)
57sourceIntFromToFromStream n = S.fold A.write $ S.enumerateFromTo n (n + value)
59{-# INLINE sourceIntFromToFromList #-}
60sourceIntFromToFromList :: MonadIO m => Int -> m (Stream Int)
61sourceIntFromToFromList n = P.return $ A.fromList $ [n..n + value]
63{-# INLINE sourceFromList #-}
64sourceFromList :: MonadIO m => Int -> m (Stream Int)
65sourceFromList n = S.fold (A.writeN value) $ S.fromList [n..n+value]
66{-
67{-# INLINE sourceIsList #-}
68sourceIsList :: Int -> Stream Int
69sourceIsList n = GHC.fromList [n..n+value]
71{-# INLINE sourceIsString #-}
72sourceIsString :: Int -> Stream P.Char
73sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
74-}
75-------------------------------------------------------------------------------
76-- Transformation
77-------------------------------------------------------------------------------
79{-# INLINE composeN #-}
80composeN :: P.Monad m
81 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
82composeN n f x =
83 case n of
84 1 -> f x
85 2 -> f x P.>>= f
86 3 -> f x P.>>= f P.>>= f
87 4 -> f x P.>>= f P.>>= f P.>>= f
88 _ -> undefined
90{-# INLINE scanl' #-}
91{-# INLINE scanl1' #-}
92{-# INLINE map #-}
94scanl', scanl1', map
95 :: MonadIO m => Int -> Stream Int -> m (Stream Int)
97{-# INLINE onArray #-}
98onArray
99 :: MonadIO m => (S.SerialT m Int -> S.SerialT m Int)
100 -> Stream Int
101 -> m (Stream Int)
102onArray f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
104scanl' n = composeN n $ onArray $ S.scanl' (+) 0
105scanl1' n = composeN n $ onArray $ S.scanl1' (+)
106map n = composeN n $ onArray $ S.map (+1)
108{-# INLINE eqInstance #-}
109eqInstance :: Stream Int -> Bool
110eqInstance src = src == src
112{-# INLINE eqInstanceNotEq #-}
113eqInstanceNotEq :: Stream Int -> Bool
114eqInstanceNotEq src = src P./= src
116{-# INLINE ordInstance #-}
117ordInstance :: Stream Int -> Bool
118ordInstance src = src P.< src
120{-# INLINE ordInstanceMin #-}
121ordInstanceMin :: Stream Int -> Stream Int
122ordInstanceMin src = P.min src src
124{-# INLINE showInstance #-}
125showInstance :: Stream Int -> P.String
126showInstance src = P.show src
128{-# INLINE readInstance #-}
129readInstance :: P.String -> Stream Int
130readInstance str =
131 let r = P.reads str
132 in case r of
133 [(x,"")] -> x
134 _ -> P.error "readInstance: no parse"
136{-# INLINE pureFoldl' #-}
137pureFoldl' :: MonadIO m => Stream Int -> m Int
138pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
140#ifdef DEVBUILD
141{-# INLINE foldableFoldl' #-}
142foldableFoldl' :: Stream Int -> Int
143foldableFoldl' = F.foldl' (+) 0
145{-# INLINE foldableSum #-}
146foldableSum :: Stream Int -> Int
147foldableSum = P.sum
148#endif