-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathStream.hs
More file actions
310 lines (262 loc) · 10 KB
/
Stream.hs
File metadata and controls
310 lines (262 loc) · 10 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
-- |
-- Module : Streamly.Benchmark.Data.ParserD
-- Copyright : (c) 2020 Composewell Technologies
--
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
#ifdef __HADDOCK_VERSION__
#undef INSPECTION
#endif
#ifdef INSPECTION
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
#endif
module Main
(
main
) where
import Control.DeepSeq (NFData(..))
import Control.Monad (void, when)
import Control.Monad.Catch (MonadCatch)
import Data.Functor.Identity (runIdentity)
import Data.Maybe (isJust)
import Data.Word (Word8)
import Streamly.Internal.Data.Stream (Stream)
import Streamly.Internal.Data.StreamK (StreamK)
import System.IO (Handle)
import System.Random (randomRIO)
import Prelude hiding ()
import qualified Streamly.Data.Stream as Stream
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.Fold as Fold
import qualified Streamly.Internal.Data.Parser as Parser
import qualified Streamly.Internal.Data.Stream as Stream
import qualified Streamly.Internal.Data.StreamK as StreamK
import qualified Streamly.Internal.FileSystem.Handle as Handle
import qualified Streamly.Internal.Unicode.Stream as Unicode
import Test.Tasty.Bench hiding (env)
import Streamly.Benchmark.Common
import Streamly.Benchmark.Common.Handle
import Control.Monad.IO.Class (MonadIO)
#ifdef INSPECTION
import Streamly.Internal.Data.MutByteArray (Unbox)
import Streamly.Internal.Data.Stream (Step(..))
import Test.Inspection
#endif
-------------------------------------------------------------------------------
-- Utilities
-------------------------------------------------------------------------------
-- XXX these can be moved to the common module
{-# INLINE sourceUnfoldrM #-}
sourceUnfoldrM :: MonadIO m => Int -> Int -> Stream.Stream m Int
sourceUnfoldrM value n = Stream.unfoldrM step n
where
step cnt =
if cnt > n + value
then return Nothing
else return (Just (cnt, cnt + 1))
{-# INLINE benchIO #-}
benchIO
:: NFData b
=> String -> (Int -> Stream IO a) -> (Stream IO a -> IO b) -> Benchmark
benchIO name src sink =
bench name $ nfIO $ randomRIO (1,1) >>= sink . src
-------------------------------------------------------------------------------
-- read chunked using toChunks
-------------------------------------------------------------------------------
-- | Get the last byte from a file bytestream.
toChunksLast :: Handle -> IO (Maybe Word8)
toChunksLast inh = do
let s = Handle.readChunks inh
larr <- Stream.last s
return $ case larr of
Nothing -> Nothing
Just arr -> Array.getIndex (Array.length arr - 1) arr
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksLast
inspect $ 'toChunksLast `hasNoType` ''Step
#endif
-- | Count the number of bytes in a file.
toChunksSumLengths :: Handle -> IO Int
toChunksSumLengths inh =
let s = Handle.readChunks inh
in Stream.fold Fold.sum (Stream.map Array.length s)
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSumLengths
inspect $ 'toChunksSumLengths `hasNoType` ''Step
#endif
-- | Sum the bytes in a file.
toChunksCountBytes :: Handle -> IO Word8
toChunksCountBytes inh = do
let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.read
let s = Handle.readChunks inh
Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksCountBytes
inspect $ 'toChunksCountBytes `hasNoType` ''Step
#endif
toChunksDecodeUtf8Arrays :: Handle -> IO ()
toChunksDecodeUtf8Arrays =
Stream.drain . Unicode.decodeUtf8Chunks . Handle.readChunks
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
-- inspect $ 'toChunksDecodeUtf8ArraysLenient `hasNoType` ''Step
#endif
-------------------------------------------------------------------------------
-- Splitting
-------------------------------------------------------------------------------
-- | Count the number of lines in a file.
toChunksSplitOnSuffix :: Handle -> IO Int
toChunksSplitOnSuffix =
Stream.fold Fold.length
. Array.compactEndByByte_ 10
. Handle.readChunks
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
inspect $ 'toChunksSplitOnSuffix `hasNoType` ''Step
#endif
-- XXX use a word splitting combinator instead of splitOn and test it.
-- | Count the number of words in a file.
toChunksSplitOn :: Handle -> IO Int
toChunksSplitOn =
Stream.fold Fold.length
. Array.compactSepByByte_ 32
. Handle.readChunks
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSplitOn
inspect $ 'toChunksSplitOn `hasNoType` ''Step
#endif
o_1_space_read_chunked :: BenchEnv -> [Benchmark]
o_1_space_read_chunked env =
-- read using toChunks instead of read
[ bgroup "reduce/toChunks"
[ mkBench "Stream.last" env $ \inH _ ->
toChunksLast inH
-- Note: this cannot be fairly compared with GNU wc -c or wc -m as
-- wc uses lseek to just determine the file size rather than reading
-- and counting characters.
, mkBench "Stream.sum . Stream.map Array.length" env $ \inH _ ->
toChunksSumLengths inH
, mkBench "splitOnSuffix" env $ \inH _ ->
toChunksSplitOnSuffix inH
, mkBench "splitOn" env $ \inH _ ->
toChunksSplitOn inH
, mkBench "countBytes" env $ \inH _ ->
toChunksCountBytes inH
, mkBenchSmall "decodeUtf8Arrays" env $ \inH _ ->
toChunksDecodeUtf8Arrays inH
]
]
-------------------------------------------------------------------------------
-- copy with group/ungroup transformations
-------------------------------------------------------------------------------
-- | Lines and unlines
{-# NOINLINE copyChunksSplitInterposeSuffix #-}
copyChunksSplitInterposeSuffix :: Handle -> Handle -> IO ()
copyChunksSplitInterposeSuffix inh outh =
Stream.fold (Handle.write outh)
$ Array.concatEndBy 10 . Array.compactEndByByte_ 10
$ Handle.readChunks inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Unbox]
inspect $ 'copyChunksSplitInterposeSuffix `hasNoType` ''Step
#endif
-- | Words and unwords
{-# NOINLINE copyChunksSplitInterpose #-}
copyChunksSplitInterpose :: Handle -> Handle -> IO ()
copyChunksSplitInterpose inh outh =
Stream.fold (Handle.write outh)
-- XXX requires @-fspec-constr-recursive=12@.
-- XXX this is not correct word splitting combinator
$ Array.concatSepBy 32 . Array.compactSepByByte_ 32
$ Handle.readChunks inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Unbox]
inspect $ 'copyChunksSplitInterpose `hasNoType` ''Step
#endif
o_1_space_copy_toChunks_group_ungroup :: BenchEnv -> [Benchmark]
o_1_space_copy_toChunks_group_ungroup env =
[ bgroup "copy/toChunks/group-ungroup"
[ mkBench "interposeSuffix . splitOnSuffix" env $ \inh outh ->
copyChunksSplitInterposeSuffix inh outh
, mkBenchSmall "interpose . splitOn" env $ \inh outh ->
copyChunksSplitInterpose inh outh
]
]
-------------------------------------------------------------------------------
-- Parsers
-------------------------------------------------------------------------------
{-# INLINE drainWhile #-}
drainWhile :: MonadCatch m => (a -> Bool) -> Parser.Parser a m ()
drainWhile p = Parser.takeWhile p Fold.drain
-------------------------------------------------------------------------------
-- Folds and parsers
-------------------------------------------------------------------------------
{-# INLINE fold #-}
fold :: Stream IO (Array.Array Int) -> IO ()
fold s = void $ Array.foldBreak Fold.drain $ StreamK.fromStream s
{-# INLINE parse #-}
parse :: Int -> Stream IO (Array.Array Int) -> IO ()
parse value s =
void $ Array.parseBreak
(Array.toParserK (drainWhile (< value)))
(StreamK.fromStream s)
{-# INLINE foldBreak #-}
foldBreak :: StreamK IO (Array.Array Int) -> IO ()
foldBreak s = do
(r, s1) <- Array.foldBreak Fold.one s
when (isJust r) $ foldBreak s1
{-# INLINE parseBreak #-}
parseBreak :: StreamK IO (Array.Array Int) -> IO ()
parseBreak s = do
r <- Array.parseBreak (Array.toParserK Parser.one) s
case r of
(Left _, _) -> return ()
(Right _, s1) -> parseBreak s1
o_1_space_serial_array ::
Int -> [Array.Array Int] -> [Array.Array Int] -> [Benchmark]
o_1_space_serial_array bound arraysSmall arraysBig =
[ benchIO "fold (of 100)" (\_ -> Stream.fromList arraysSmall) fold
, benchIO "fold (single)" (\_ -> Stream.fromList arraysBig) fold
, benchIO
"foldBreak (recursive, small arrays)"
(\_ -> Stream.fromList arraysSmall)
(foldBreak . StreamK.fromStream)
, benchIO "parse (of 100)" (\_ -> Stream.fromList arraysSmall)
$ parse bound
, benchIO "parse (single)" (\_ -> Stream.fromList arraysBig)
$ parse bound
, benchIO
"parseBreak (recursive, small arrays)"
(\_ -> Stream.fromList arraysSmall)
(parseBreak . StreamK.fromStream)
]
-------------------------------------------------------------------------------
-- Driver
-------------------------------------------------------------------------------
moduleName :: String
moduleName = "Data.Array.Stream"
main :: IO ()
main = do
env <- mkHandleBenchEnv
runWithCLIOptsEnv defaultStreamSize alloc (allBenchmarks env)
where
alloc value =
if value <= 0
then return (undefined, undefined)
else
do
small <- Stream.toList $ Array.chunksOf 100 $ sourceUnfoldrM value 0
big <- Stream.toList $ Array.chunksOf value $ sourceUnfoldrM value 0
return (small, big)
allBenchmarks env arrays value =
let (arraysSmall, arraysBig) = arrays
in [ bgroup (o_1_space_prefix moduleName) $ Prelude.concat
[ o_1_space_read_chunked env
, o_1_space_serial_array value arraysSmall arraysBig
, o_1_space_copy_toChunks_group_ungroup env
]
]