-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathFileSystem.hs
More file actions
406 lines (356 loc) · 15.2 KB
/
Copy pathFileSystem.hs
File metadata and controls
406 lines (356 loc) · 15.2 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
--
-- Module : CrossModule.FileSystem
-- Copyright : (c) 2019 Composewell Technologies
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
-- Stability : experimental
-- Portability : GHC
--
-- Benchmarks that combine operations from multiple library modules, primarily
-- to test fusion and performance across module boundaries (e.g. file I/O
-- fused with stream folds and chunking operations).
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
#ifdef __HADDOCK_VERSION__
#undef INSPECTION
#endif
#ifdef INSPECTION
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
#endif
module CrossModule.FileSystem (benchmarks) where
import Data.Functor.Identity (runIdentity)
import Data.Word (Word8)
import Foreign.Ptr (Ptr)
import GHC.Classes (IP)
import GHC.Magic (inline)
import GHC.Magic (noinline)
import GHC.Stack (CallStack, SrcLoc)
import Streamly.Internal.Data.Array (Array)
import Streamly.Internal.Data.MutArray (MutArray)
import Streamly.Internal.Data.MutByteArray (PinnedState)
import System.IO (Handle)
import Unsafe.Coerce (UnsafeEquality)
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.FileSystem.Handle as FH
import qualified Streamly.Internal.Data.Array as A
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Data.Parser as PR
import qualified Streamly.Internal.Data.Stream as IP
import qualified Streamly.Data.Stream as S
import Fusion.Plugin.Types
import Test.Tasty.Bench hiding (env)
import Prelude hiding (last, length)
import Streamly.Benchmark.Common
import Streamly.Benchmark.Common.Handle
#ifdef INSPECTION
import Streamly.Internal.Data.MutByteArray (Unbox)
import Streamly.Internal.Data.Stream (Step(..), FoldMany)
import qualified Streamly.Internal.Data.MutArray as MutArray
import qualified Streamly.Internal.Data.Producer as Producer
import Test.Inspection
#endif
-------------------------------------------------------------------------------
-- Handle read
-------------------------------------------------------------------------------
-- | Get the last byte from a file bytestream.
{-# ANN readLast (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN readLast (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Maybe,''Word8,''Ptr
,''PinnedState]) #-}
{-# ANN readLast (PermitTypeClasses [''IP]) #-}
{-# NOINLINE readLast #-}
readLast :: Handle -> IO (Maybe Word8)
readLast = S.fold Fold.latest . S.unfold FH.reader
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'readLast
inspect $ 'readLast `hasNoType` ''Step -- S.unfold
inspect $ 'readLast `hasNoType` ''Producer.ConcatState -- FH.read/UF.many
inspect $ 'readLast `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read
#endif
-- | Count the number of bytes in a file.
{-# ANN readCountBytes (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN readCountBytes (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Ptr,''PinnedState]) #-}
{-# ANN readCountBytes (PermitTypeClasses [''IP]) #-}
{-# NOINLINE readCountBytes #-}
readCountBytes :: Handle -> IO Int
readCountBytes = S.fold Fold.length . S.unfold FH.reader
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'readCountBytes
inspect $ 'readCountBytes `hasNoType` ''Step -- S.unfold
inspect $ 'readCountBytes `hasNoType` ''Producer.ConcatState -- FH.read/UF.many
inspect $ 'readCountBytes `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read
#endif
-- | Sum the bytes in a file.
{-# ANN readSumBytes (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN readSumBytes (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Word8,''Ptr,''PinnedState]) #-}
{-# ANN readSumBytes (PermitTypeClasses [''IP]) #-}
{-# NOINLINE readSumBytes #-}
readSumBytes :: Handle -> IO Word8
readSumBytes = S.fold Fold.sum . S.unfold FH.reader
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'readSumBytes
inspect $ 'readSumBytes `hasNoType` ''Step
inspect $ 'readSumBytes `hasNoType` ''Producer.ConcatState -- FH.read/UF.many
inspect $ 'readSumBytes `hasNoType` ''MutArray.ArrayUnsafe -- FH.read/A.read
#endif
-------------------------------------------------------------------------------
-- reduce after grouping in chunks
-------------------------------------------------------------------------------
{-# ANN chunksOfSum (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN chunksOfSum (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Ptr,''PinnedState]) #-}
{-# ANN chunksOfSum (PermitTypeClasses [''IP]) #-}
{-# NOINLINE chunksOfSum #-}
chunksOfSum :: Int -> Handle -> IO Int
chunksOfSum n inh =
S.fold Fold.length $ IP.groupsOf n FL.sum (S.unfold FH.reader inh)
-- NOTE: these three rely on the caller applying 'inline'/'noinline' (GHC.Magic)
-- at each call site to get either a fully inlined or a real out-of-line call
-- from the same definition, so they are intentionally left without a NOINLINE
-- pragma/ANN -- adding NOINLINE here would remove the unfolding that 'inline'
-- needs and defeat the small-n variant.
foldMany1ChunksOfSum :: Int -> Handle -> IO Int
foldMany1ChunksOfSum n inh =
S.fold Fold.length
$ IP.foldManyPost (FL.take n FL.sum) (S.unfold FH.reader inh)
foldManyChunksOfSum :: Int -> Handle -> IO Int
foldManyChunksOfSum n inh =
S.fold Fold.length
$ IP.foldMany (FL.take n FL.sum) (S.unfold FH.reader inh)
parseManyChunksOfSum :: Int -> Handle -> IO Int
parseManyChunksOfSum n inh =
S.fold Fold.length
$ IP.parseMany (PR.fromFold $ FL.take n FL.sum) (S.unfold FH.reader inh)
-- XXX investigate why we need an INLINE in this case (GHC)
-- Even though allocations remain the same in both cases inlining improves time
-- by 4x.
-- | Slice in chunks of size n and get the count of chunks.
{-# INLINE groupsOf #-}
groupsOf :: Int -> Handle -> IO Int
groupsOf n inh =
-- writeNUnsafe gives 2.5x boost here over writeN.
S.fold Fold.length
$ IP.groupsOf n (A.unsafeCreateOf n) (S.unfold FH.reader inh)
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'groupsOf
inspect $ 'groupsOf `hasNoType` ''Step
inspect $ 'groupsOf `hasNoType` ''FoldMany
inspect $ 'groupsOf `hasNoType` ''MutArray.ArrayUnsafe -- AT.writeNUnsafe
-- FH.read/A.read
inspect $ 'groupsOf `hasNoType` ''Producer.ConcatState -- FH.read/UF.many
#endif
{-# INLINE chunksOf #-}
chunksOf :: Int -> Handle -> IO Int
chunksOf n inh =
S.fold Fold.length $ A.chunksOf n (S.unfold FH.reader inh)
-------------------------------------------------------------------------------
-- read chunked using toChunks
-------------------------------------------------------------------------------
-- | Get the last byte from a file bytestream.
{-# ANN toChunksLast (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN toChunksLast (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Maybe,''Word8,''Ptr
,''PinnedState]) #-}
{-# ANN toChunksLast (PermitTypeClasses [''IP]) #-}
{-# NOINLINE toChunksLast #-}
toChunksLast :: Handle -> IO (Maybe Word8)
toChunksLast inh = do
let s = FH.readChunks inh
larr <- IP.last s
return $ case larr of
Nothing -> Nothing
Just arr -> A.getIndex (A.length arr - 1) arr
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksLast
inspect $ 'toChunksLast `hasNoType` ''Step
#endif
-- | Count the number of bytes in a file.
{-# ANN toChunksSumLengths (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN toChunksSumLengths (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Ptr,''PinnedState]) #-}
{-# ANN toChunksSumLengths (PermitTypeClasses [''IP]) #-}
{-# NOINLINE toChunksSumLengths #-}
toChunksSumLengths :: Handle -> IO Int
toChunksSumLengths inh =
let s = FH.readChunks inh
in IP.fold Fold.sum (IP.map A.length s)
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSumLengths
inspect $ 'toChunksSumLengths `hasNoType` ''Step
#endif
-- | Sum the bytes in a file.
{-# ANN toChunksCountBytes (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''Array]) #-}
{-# ANN toChunksCountBytes (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''Ptr,''PinnedState]) #-}
{-# ANN toChunksCountBytes (PermitTypeClasses [''IP]) #-}
{-# NOINLINE toChunksCountBytes #-}
toChunksCountBytes :: Handle -> IO Word8
toChunksCountBytes inh = do
let foldlArr' f z = runIdentity . IP.foldl' f z . A.read
let s = FH.readChunks inh
IP.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksCountBytes
inspect $ 'toChunksCountBytes `hasNoType` ''Step
#endif
-------------------------------------------------------------------------------
-- Splitting
-------------------------------------------------------------------------------
-- | Count the number of lines in a file.
{-# ANN toChunksSplitOnSuffix (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''MutArray,''Array]) #-}
{-# ANN toChunksSplitOnSuffix (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''MutArray,''Ptr
,''PinnedState]) #-}
{-# ANN toChunksSplitOnSuffix (PermitTypeClasses [''IP]) #-}
{-# NOINLINE toChunksSplitOnSuffix #-}
toChunksSplitOnSuffix :: Handle -> IO Int
toChunksSplitOnSuffix =
IP.fold Fold.length
. A.compactEndByByte_ 10
. FH.readChunks
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
inspect $ 'toChunksSplitOnSuffix `hasNoType` ''Step
#endif
-- | Count the number of words in a file.
{-# ANN toChunksSplitOn (PermitPatternMatches
[''[],''Int,''UnsafeEquality,''IO,''MutArray,''Array]) #-}
{-# ANN toChunksSplitOn (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''MutArray,''Ptr
,''PinnedState]) #-}
{-# ANN toChunksSplitOn (PermitTypeClasses [''IP]) #-}
{-# NOINLINE toChunksSplitOn #-}
toChunksSplitOn :: Handle -> IO Int
toChunksSplitOn =
IP.fold Fold.length
. A.compactSepByByte_ 32
. FH.readChunks
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'toChunksSplitOn
inspect $ 'toChunksSplitOn `hasNoType` ''Step
#endif
-------------------------------------------------------------------------------
-- copy with group/ungroup transformations
-------------------------------------------------------------------------------
-- | Lines and unlines
{-# ANN copyChunksSplitInterposeSuffix (PermitPatternMatches
[''IO,''[],''Int,''UnsafeEquality,''Array,''MutArray]) #-}
{-# ANN copyChunksSplitInterposeSuffix (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''(),''MutArray,''Ptr,''Bool
,''PinnedState]) #-}
{-# ANN copyChunksSplitInterposeSuffix (PermitTypeClasses [''IP]) #-}
{-# NOINLINE copyChunksSplitInterposeSuffix #-}
copyChunksSplitInterposeSuffix :: Handle -> Handle -> IO ()
copyChunksSplitInterposeSuffix inh outh =
IP.fold (FH.write outh)
$ A.concatEndBy 10 . A.compactEndByByte_ 10
$ FH.readChunks inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Unbox]
inspect $ 'copyChunksSplitInterposeSuffix `hasNoType` ''Step
#endif
-- | Words and unwords
{-# ANN copyChunksSplitInterpose (PermitPatternMatches
[''IO,''[],''Int,''UnsafeEquality,''MutArray,''Array,''(,),''Maybe]) #-}
{-# ANN copyChunksSplitInterpose (PermitConstructions
[''Int,''SrcLoc,''CallStack,''[],''Array,''(,),''MutArray,''Maybe,''()
,''Ptr,''Bool,''PinnedState]) #-}
{-# ANN copyChunksSplitInterpose (PermitTypeClasses [''IP]) #-}
{-# NOINLINE copyChunksSplitInterpose #-}
copyChunksSplitInterpose :: Handle -> Handle -> IO ()
copyChunksSplitInterpose inh outh =
IP.fold (FH.write outh)
-- XXX requires @-fspec-constr-recursive=12@.
-- XXX this is not correct word splitting combinator
$ A.concatSepBy 32 . A.compactSepByByte_ 32
$ FH.readChunks inh
#ifdef INSPECTION
inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Unbox]
inspect $ 'copyChunksSplitInterpose `hasNoType` ''Step
#endif
benchmarks :: BenchEnv -> [(SpaceComplexity, Benchmark)]
benchmarks env =
map (\b -> (SpaceO_1, b))
[ mkBench "Fold.latest" env $ \inh _ ->
readLast inh
, mkBench "Fold.sum" env $ \inh _ ->
readSumBytes inh
, mkBench "Fold.length (wc -c)" env $ \inh _ ->
readCountBytes inh
-- XXX all these require @-fspec-constr-recursive=12@.
, mkBench ("Stream.groupsOf " ++ show (bigSize env) ++ " . Fold.sum") env $
\inh _ ->
chunksOfSum (bigSize env) inh
, mkBench "Stream.groupsOf 1 . Fold.sum" env $ \inh _ ->
chunksOfSum 1 inh
-- XXX investigate why we need inline/noinline in these cases (GHC)
, mkBench
("Stream.foldManyPost " ++ show (bigSize env) ++ " . Fold.sum")
env
$ \inh _ -> noinline foldMany1ChunksOfSum (bigSize env) inh
, mkBench
"Stream.foldManyPost 1 . Fold.sum"
env
$ \inh _ -> inline foldMany1ChunksOfSum 1 inh
, mkBench
("Stream.foldMany " ++ show (bigSize env) ++ " . Fold.sum")
env
$ \inh _ -> noinline foldManyChunksOfSum (bigSize env) inh
, mkBench
"Stream.foldMany 1 . Fold.sum"
env
$ \inh _ -> inline foldManyChunksOfSum 1 inh
-- parseMany with file IO
, mkBench
("Stream.parseMany (Fold.take " ++ show (bigSize env) ++ " Fold.sum)")
env
$ \inh _ -> noinline parseManyChunksOfSum (bigSize env) inh
, mkBench
"Stream.parseMany (Fold.take 1 Fold.sum)"
env
$ \inh _ -> inline parseManyChunksOfSum 1 inh
-- folding chunks to arrays
, mkBenchSmall "Stream.groupsOf 1 . Array.unsafeCreateOf" env $ \inh _ ->
groupsOf 1 inh
, mkBench "Stream.groupsOf 10 . Array.unsafeCreateOf" env $ \inh _ ->
groupsOf 10 inh
, mkBench "Stream.groupsOf 1000 . Array.unsafeCreateOf" env $ \inh _ ->
groupsOf 1000 inh
-- chunksOf may use a different impl than groupsOf
, mkBenchSmall "Array.chunksOf 1" env $ \inh _ ->
chunksOf 1 inh
, mkBench "Array.chunksOf 10" env $ \inh _ ->
chunksOf 10 inh
, mkBench "Array.chunksOf 1000" env $ \inh _ ->
chunksOf 1000 inh
-- read chunked using 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
-- copy with group/ungroup transformations
, mkBench "interposeSuffix . splitOnSuffix" env $ \inh outh ->
copyChunksSplitInterposeSuffix inh outh
, mkBenchSmall "interpose . splitOn" env $ \inh outh ->
copyChunksSplitInterpose inh outh
]