-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathBuilder.hs
More file actions
201 lines (173 loc) · 6.51 KB
/
Builder.hs
File metadata and controls
201 lines (173 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
-- |
-- Module : Streamly.Benchmark.Prelude
-- Copyright : (c) 2020 Composewell Technologies
--
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
module Main where
import Streamly.Prelude (SerialT, serially)
import qualified Streamly.Internal.Data.Stream.IsStream as Stream
import qualified Streamly.Internal.Data.Builder as Builder
import qualified Streamly.Internal.Data.Monoid.Builder as MBuilder
import Gauge
import Streamly.Benchmark.Common
import Streamly.Benchmark.Prelude
moduleName :: String
moduleName = "Data.Builder"
-------------------------------------------------------------------------------
-- Right Associated Appends
-------------------------------------------------------------------------------
{-# INLINE appendListSourceR #-}
appendListSourceR :: Int -> Int -> [Int]
appendListSourceR value n =
Prelude.foldr (Prelude.++) [] (Prelude.fmap (: []) [n..n+value])
{-# INLINE appendListBuilderSourceR #-}
appendListBuilderSourceR :: Int -> Int -> [Int]
appendListBuilderSourceR value n =
Builder.use $ foldMap (Builder.add . (: [])) [n..n+value]
{-# INLINE consListBuilderSourceR #-}
consListBuilderSourceR :: Int -> Int -> [Int]
consListBuilderSourceR value n =
Builder.use $ foldMap Builder.mk [n..n+value]
{-# INLINE appendSourceR #-}
appendSourceR :: Int -> Int -> SerialT m Int
appendSourceR value n = foldMap Stream.yield [n..n+value]
{-# INLINE consStreamBuilderSourceR #-}
consStreamBuilderSourceR :: Int -> Int -> SerialT m Int
consStreamBuilderSourceR value n =
Builder.use $ foldMap Builder.mk [n..n+value]
{-# INLINE appendStreamBuilderSourceR #-}
appendStreamBuilderSourceR :: Int -> Int -> SerialT m Int
appendStreamBuilderSourceR value n =
Builder.use $ foldMap (Builder.add . Stream.yield) [n..n+value]
o_1_space_appendR :: Int -> [Benchmark]
o_1_space_appendR value =
[ bgroup "appendR"
[ benchPure "singleton lists" (appendListSourceR value) id
, benchIOSrc serially "singleton streams" (appendSourceR value)
, benchPure "consed list builders" (consListBuilderSourceR value) id
, benchPure
"singleton list builders" (appendListBuilderSourceR value) id
, benchIOSrc
serially
"singleton stream builders"
(appendStreamBuilderSourceR value)
, benchIOSrc
serially
"consed stream builders"
(consStreamBuilderSourceR value)
]
]
-------------------------------------------------------------------------------
-- Left Associated Appends
-------------------------------------------------------------------------------
{-# INLINE appendListSourceL #-}
appendListSourceL :: Int -> Int -> [Int]
appendListSourceL value n =
Prelude.foldl (Prelude.++) [] (map (: []) [n..n+value])
{-# INLINE appendListBuilderSourceL #-}
appendListBuilderSourceL :: Int -> Int -> [Int]
appendListBuilderSourceL value n =
Builder.use
$ Prelude.foldl
(<>) mempty (map (Builder.add . (: [])) [n..n+value])
{-# INLINE appendListMonoidBuilderSourceL #-}
appendListMonoidBuilderSourceL :: Int -> Int -> [Int]
appendListMonoidBuilderSourceL value n =
MBuilder.use
$ Prelude.foldl (<>) mempty (map (MBuilder.mk . (: [])) [n..n+value])
{-# INLINE consListBuilderSourceL #-}
consListBuilderSourceL :: Int -> Int -> [Int]
consListBuilderSourceL value n =
Builder.use
$ Prelude.foldl (<>) mempty (map Builder.mk [n..n+value])
{-# INLINE appendSourceL #-}
appendSourceL :: Int -> Int -> SerialT m Int
appendSourceL value n =
Prelude.foldl
(Prelude.<>) Stream.nil (Prelude.fmap Stream.yield [n..n+value])
{-# INLINE appendStreamBuilderSourceL #-}
appendStreamBuilderSourceL :: Int -> Int -> SerialT m Int
appendStreamBuilderSourceL value n =
Builder.use
$ Prelude.foldl
(<>)
mempty
(map (Builder.add . Stream.yield) [n..n+value])
{-# INLINE consStreamBuilderSourceL #-}
consStreamBuilderSourceL :: Int -> Int -> SerialT m Int
consStreamBuilderSourceL value n =
Builder.use
$ Prelude.foldl (<>) mempty (map Builder.mk [n..n+value])
{-# INLINE appendStreamMonoidBuilderSourceL #-}
appendStreamMonoidBuilderSourceL :: Int -> Int -> SerialT m Int
appendStreamMonoidBuilderSourceL value n =
MBuilder.use
$ Prelude.foldl
(<>) mempty (map (MBuilder.mk . Stream.yield) [n..n+value])
-- Use builder of streams and concat
{-# INLINE streamConcatStreamBuilderSourceL #-}
streamConcatStreamBuilderSourceL :: Monad m => Int -> Int -> SerialT m Int
streamConcatStreamBuilderSourceL value n =
Stream.concat
$ Builder.use
$ Prelude.foldl
(<>)
mempty
(map (Builder.mk . Stream.yield) [n..n+value])
o_1_space_appendL :: Int -> [Benchmark]
o_1_space_appendL value =
[ bgroup "appendL"
[ benchPure
"singleton lists (n/100)" (appendListSourceL (value `div` 100)) id
, benchPure
"singleton list builders"
(appendListBuilderSourceL value)
id
, benchPure
"singleton list monoid builders"
(appendListMonoidBuilderSourceL value)
id
, benchPure
"consed list builders"
(consListBuilderSourceL value)
id
, benchIOSrc
serially
"singleton streams (n/100)"
(appendSourceL (value `div` 100))
, benchIOSrc
serially
"singleton stream builders"
(appendStreamBuilderSourceL value)
, benchIOSrc
serially
"consed stream builders"
(consStreamBuilderSourceL value)
, benchIOSrc
serially
"singleton stream monoid builders"
(appendStreamMonoidBuilderSourceL value)
, benchIOSrc
serially
"Stream.concat stream builders"
(streamConcatStreamBuilderSourceL value)
]
]
-------------------------------------------------------------------------------
-- Main
-------------------------------------------------------------------------------
-- In addition to gauge options, the number of elements in the stream can be
-- passed using the --stream-size option.
--
main :: IO ()
main = do
(value, cfg, benches) <- parseCLIOpts defaultStreamSize
value `seq` runMode (mode cfg) cfg benches (allBenchmarks value)
where
allBenchmarks size =
[ bgroup (o_1_space_prefix moduleName) $ Prelude.concat
[ o_1_space_appendR size
, o_1_space_appendL size
]
]