@@ -0,0 +1,136 @@
1+-- |
2+-- Module : Streamly.Benchmark.Data.SmallArrayOps
3+-- Copyright : (c) 2019 Composewell Technologies
4+--
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+-- Stability : experimental
8+-- Portability : GHC
9+
10+{-# LANGUAGE CPP #-}
11+{-# LANGUAGE DeriveAnyClass #-}
12+{-# LANGUAGE DeriveGeneric #-}
13+{-# LANGUAGE FlexibleContexts #-}
14+{-# LANGUAGE ScopedTypeVariables #-}
15+
16+module Streamly.Benchmark.Data.SmallArrayOps where
17+
18+import Control.Monad.IO.Class (MonadIO)
19+import Prelude (Int, Bool, (+), ($), (==), (>), (.), Maybe(..), undefined)
20+import qualified Prelude as P
21+#ifdef DEVBUILD
22+import qualified Data.Foldable as F
23+#endif
24+
25+import qualified Streamly as S hiding (foldMapWith, runStream)
26+import qualified Streamly.Data.SmallArray as A
27+import qualified Streamly.Prelude as S
28+
29+value :: Int
30+value = 128
31+
32+-------------------------------------------------------------------------------
33+-- Benchmark ops
34+-------------------------------------------------------------------------------
35+
36+-------------------------------------------------------------------------------
37+-- Stream generation and elimination
38+-------------------------------------------------------------------------------
39+
40+type Stream = A.SmallArray
41+
42+{-# INLINE sourceUnfoldr #-}
43+sourceUnfoldr :: MonadIO m => Int -> m (Stream Int)
44+sourceUnfoldr 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))
50+
51+{-# INLINE sourceIntFromTo #-}
52+sourceIntFromTo :: MonadIO m => Int -> m (Stream Int)
53+sourceIntFromTo n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
54+
55+{-# INLINE sourceIntFromToFromList #-}
56+sourceIntFromToFromList :: MonadIO m => Int -> m (Stream Int)
57+sourceIntFromToFromList n = P.return $ (A.fromListN value) $ [n..n + value]
58+
59+{-# INLINE sourceFromList #-}
60+sourceFromList :: MonadIO m => Int -> m (Stream Int)
61+sourceFromList n = S.fold (A.writeN value) $ S.fromList [n..n+value]
62+
63+-------------------------------------------------------------------------------
64+-- Transformation
65+-------------------------------------------------------------------------------
66+
67+{-# INLINE composeN #-}
68+composeN :: P.Monad m
69+ => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
70+composeN n f x =
71+ case n of
72+ 1 -> f x
73+ 2 -> f x P.>>= f
74+ 3 -> f x P.>>= f P.>>= f
75+ 4 -> f x P.>>= f P.>>= f P.>>= f
76+ _ -> undefined
77+
78+{-# INLINE scanl' #-}
79+{-# INLINE scanl1' #-}
80+{-# INLINE map #-}
81+
82+scanl', scanl1', map
83+ :: MonadIO m => Int -> Stream Int -> m (Stream Int)
84+
85+{-# INLINE onArray #-}
86+onArray
87+ :: MonadIO m => (S.SerialT m Int -> S.SerialT m Int)
88+ -> Stream Int
89+ -> m (Stream Int)
90+onArray f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
91+
92+scanl' n = composeN n $ onArray $ S.scanl' (+) 0
93+scanl1' n = composeN n $ onArray $ S.scanl1' (+)
94+map n = composeN n $ onArray $ S.map (+1)
95+
96+{-# INLINE eqInstance #-}
97+eqInstance :: Stream Int -> Bool
98+eqInstance src = src == src
99+
100+{-# INLINE eqInstanceNotEq #-}
101+eqInstanceNotEq :: Stream Int -> Bool
102+eqInstanceNotEq src = src P./= src
103+
104+{-# INLINE ordInstance #-}
105+ordInstance :: Stream Int -> Bool
106+ordInstance src = src P.< src
107+
108+{-# INLINE ordInstanceMin #-}
109+ordInstanceMin :: Stream Int -> Stream Int
110+ordInstanceMin src = P.min src src
111+
112+{-# INLINE showInstance #-}
113+showInstance :: Stream Int -> P.String
114+showInstance src = P.show src
115+
116+{-# INLINE readInstance #-}
117+readInstance :: P.String -> Stream Int
118+readInstance str =
119+ let r = P.reads str
120+ in case r of
121+ [(x,"")] -> x
122+ _ -> P.error "readInstance: no parse"
123+
124+{-# INLINE pureFoldl' #-}
125+pureFoldl' :: MonadIO m => Stream Int -> m Int
126+pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
127+
128+#ifdef DEVBUILD
129+{-# INLINE foldableFoldl' #-}
130+foldableFoldl' :: Stream Int -> Int
131+foldableFoldl' = F.foldl' (+) 0
132+
133+{-# INLINE foldableSum #-}
134+foldableSum :: Stream Int -> Int
135+foldableSum = P.sum
136+#endif