@@ -7,9 +7,12 @@
77-- Stability : experimental
88-- Portability : GHC
99
10+{-# LANGUAGE CPP #-}
11+#ifdef INSPECTION
1012{-# LANGUAGE TemplateHaskell #-}
1113
1214{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
15+#endif
1316
1417module Streamly.Benchmark.FileIO.Stream
1518 (
@@ -27,6 +30,7 @@ module Streamly.Benchmark.FileIO.Stream
2730 , copyCodecChar8
2831 , copyCodecUtf8
2932 , chunksOf
33+ , chunksOfD
3034 , splitOn
3135 , splitOnSuffix
3236 , wordsBy
@@ -40,30 +44,32 @@ import Data.Word (Word8)
4044import System.IO (Handle)
4145import Prelude hiding (last, length)
4246
43−import Streamly.Streams.StreamD.Type (Step(..), GroupState)
44−import Streamly.Benchmark.Inspection (hinspect)
45−import qualified Streamly.Memory.Array.Types as AT
46−
4747import qualified Streamly.FileSystem.Handle as FH
4848import qualified Streamly.FileSystem.Handle.Internal as FH
4949import qualified Streamly.Memory.Array as A
50+import qualified Streamly.Memory.Array.Types as AT
5051import qualified Streamly.Prelude.Internal as S
5152import qualified Streamly.Fold as FL
5253import qualified Streamly.Data.String as SS
5354import qualified Streamly.Internal as Internal
5455import qualified Streamly.Streams.StreamD as D
5556
57+#ifdef INSPECTION
58+import Streamly.Streams.StreamD.Type (Step(..), GroupState)
5659import Test.Inspection
60+#endif
5761
5862-- | Get the last byte from a file bytestream.
5963{-# INLINE last #-}
6064last :: Handle -> IO (Maybe Word8)
6165last = S.last . FH.read
6266
63−hinspect $ hasNoTypeClasses 'last
64−hinspect $ 'last `hasNoType` ''Step
65−hinspect $ 'last `hasNoType` ''AT.FlattenState
66−hinspect $ 'last `hasNoType` ''D.ConcatMapUState
67+#ifdef INSPECTION
68+inspect $ hasNoTypeClasses 'last
69+inspect $ 'last `hasNoType` ''Step
70+inspect $ 'last `hasNoType` ''AT.FlattenState
71+inspect $ 'last `hasNoType` ''D.ConcatMapUState
72+#endif
6773
6874-- assert that flattenArrays constructors are not present
6975-- | Count the number of bytes in a file.
@@ -71,10 +77,12 @@ hinspect $ 'last `hasNoType` ''D.ConcatMapUState
7177countBytes :: Handle -> IO Int
7278countBytes = S.length . FH.read
7379
74−hinspect $ hasNoTypeClasses 'countBytes
75−hinspect $ 'countBytes `hasNoType` ''Step
76−hinspect $ 'countBytes `hasNoType` ''AT.FlattenState
77−hinspect $ 'countBytes `hasNoType` ''D.ConcatMapUState
80+#ifdef INSPECTION
81+inspect $ hasNoTypeClasses 'countBytes
82+inspect $ 'countBytes `hasNoType` ''Step
83+inspect $ 'countBytes `hasNoType` ''AT.FlattenState
84+inspect $ 'countBytes `hasNoType` ''D.ConcatMapUState
85+#endif
7886
7987-- | Count the number of lines in a file.
8088{-# INLINE countLines #-}
@@ -85,10 +93,12 @@ countLines =
8593 . SS.decodeChar8
8694 . FH.read
8795
88−hinspect $ hasNoTypeClasses 'countLines
89−hinspect $ 'countLines `hasNoType` ''Step
90−hinspect $ 'countLines `hasNoType` ''AT.FlattenState
91−hinspect $ 'countLines `hasNoType` ''D.ConcatMapUState
96+#ifdef INSPECTION
97+inspect $ hasNoTypeClasses 'countLines
98+inspect $ 'countLines `hasNoType` ''Step
99+inspect $ 'countLines `hasNoType` ''AT.FlattenState
100+inspect $ 'countLines `hasNoType` ''D.ConcatMapUState
101+#endif
92102
93103-- | Count the number of lines in a file.
94104{-# INLINE countLinesU #-}
@@ -99,49 +109,59 @@ countLinesU inh =
99109 $ SS.decodeChar8
100110 $ Internal.concatMapU Internal.readU (FH.readArrays inh)
101111
102−hinspect $ hasNoTypeClasses 'countLinesU
103−hinspect $ 'countLinesU `hasNoType` ''Step
104−hinspect $ 'countLinesU `hasNoType` ''D.ConcatMapUState
112+#ifdef INSPECTION
113+inspect $ hasNoTypeClasses 'countLinesU
114+inspect $ 'countLinesU `hasNoType` ''Step
115+inspect $ 'countLinesU `hasNoType` ''D.ConcatMapUState
116+#endif
105117
106118-- | Sum the bytes in a file.
107119{-# INLINE sumBytes #-}
108120sumBytes :: Handle -> IO Word8
109121sumBytes = S.sum . FH.read
110122
111−hinspect $ hasNoTypeClasses 'sumBytes
112−hinspect $ 'sumBytes `hasNoType` ''Step
113−hinspect $ 'sumBytes `hasNoType` ''AT.FlattenState
114−hinspect $ 'sumBytes `hasNoType` ''D.ConcatMapUState
123+#ifdef INSPECTION
124+inspect $ hasNoTypeClasses 'sumBytes
125+inspect $ 'sumBytes `hasNoType` ''Step
126+inspect $ 'sumBytes `hasNoType` ''AT.FlattenState
127+inspect $ 'sumBytes `hasNoType` ''D.ConcatMapUState
128+#endif
115129
116130-- | Send the file contents to /dev/null
117131{-# INLINE cat #-}
118132cat :: Handle -> Handle -> IO ()
119133cat devNull inh = S.runFold (FH.write devNull) $ FH.read inh
120134
121−hinspect $ hasNoTypeClasses 'cat
122−hinspect $ 'cat `hasNoType` ''Step
123−hinspect $ 'cat `hasNoType` ''AT.FlattenState
124−hinspect $ 'cat `hasNoType` ''D.ConcatMapUState
135+#ifdef INSPECTION
136+inspect $ hasNoTypeClasses 'cat
137+inspect $ 'cat `hasNoType` ''Step
138+inspect $ 'cat `hasNoType` ''AT.FlattenState
139+inspect $ 'cat `hasNoType` ''D.ConcatMapUState
140+#endif
125141
126142-- | Send the file contents to /dev/null
127143{-# INLINE catStreamWrite #-}
128144catStreamWrite :: Handle -> Handle -> IO ()
129145catStreamWrite devNull inh = FH.writeS devNull $ FH.read inh
130146
131−hinspect $ hasNoTypeClasses 'catStreamWrite
132−hinspect $ 'catStreamWrite `hasNoType` ''Step
133−hinspect $ 'catStreamWrite `hasNoType` ''AT.FlattenState
134−hinspect $ 'catStreamWrite `hasNoType` ''D.ConcatMapUState
147+#ifdef INSPECTION
148+inspect $ hasNoTypeClasses 'catStreamWrite
149+inspect $ 'catStreamWrite `hasNoType` ''Step
150+inspect $ 'catStreamWrite `hasNoType` ''AT.FlattenState
151+inspect $ 'catStreamWrite `hasNoType` ''D.ConcatMapUState
152+#endif
135153
136154-- | Copy file
137155{-# INLINE copy #-}
138156copy :: Handle -> Handle -> IO ()
139157copy inh outh = S.runFold (FH.write outh) (FH.read inh)
140158
141−hinspect $ hasNoTypeClasses 'copy
142−hinspect $ 'copy `hasNoType` ''Step
143−hinspect $ 'copy `hasNoType` ''AT.FlattenState
144−hinspect $ 'copy `hasNoType` ''D.ConcatMapUState
159+#ifdef INSPECTION
160+inspect $ hasNoTypeClasses 'copy
161+inspect $ 'copy `hasNoType` ''Step
162+inspect $ 'copy `hasNoType` ''AT.FlattenState
163+inspect $ 'copy `hasNoType` ''D.ConcatMapUState
164+#endif
145165
146166-- | Copy file
147167{-# INLINE copyCodecChar8 #-}
@@ -152,10 +172,12 @@ copyCodecChar8 inh outh =
152172 $ SS.decodeChar8
153173 $ FH.read inh
154174
155−hinspect $ hasNoTypeClasses 'copyCodecChar8
156−hinspect $ 'copyCodecChar8 `hasNoType` ''Step
157−hinspect $ 'copyCodecChar8 `hasNoType` ''AT.FlattenState
158−hinspect $ 'copyCodecChar8 `hasNoType` ''D.ConcatMapUState
175+#ifdef INSPECTION
176+inspect $ hasNoTypeClasses 'copyCodecChar8
177+inspect $ 'copyCodecChar8 `hasNoType` ''Step
178+inspect $ 'copyCodecChar8 `hasNoType` ''AT.FlattenState
179+inspect $ 'copyCodecChar8 `hasNoType` ''D.ConcatMapUState
180+#endif
159181
160182-- | Copy file
161183{-# INLINE copyCodecUtf8 #-}
@@ -166,10 +188,12 @@ copyCodecUtf8 inh outh =
166188 $ SS.decodeUtf8
167189 $ FH.read inh
168190
169−hinspect $ hasNoTypeClasses 'copyCodecUtf8
170−-- hinspect $ 'copyCodecUtf8 `hasNoType` ''Step
171−-- hinspect $ 'copyCodecUtf8 `hasNoType` ''AT.FlattenState
172−-- hinspect $ 'copyCodecUtf8 `hasNoType` ''D.ConcatMapUState
191+#ifdef INSPECTION
192+inspect $ hasNoTypeClasses 'copyCodecUtf8
193+-- inspect $ 'copyCodecUtf8 `hasNoType` ''Step
194+-- inspect $ 'copyCodecUtf8 `hasNoType` ''AT.FlattenState
195+-- inspect $ 'copyCodecUtf8 `hasNoType` ''D.ConcatMapUState
196+#endif
173197
174198-- | Slice in chunks of size n and get the count of chunks.
175199{-# INLINE chunksOf #-}
@@ -178,11 +202,13 @@ chunksOf n inh =
178202 -- writeNUnsafe gives 2.5x boost here over writeN.
179203 S.length $ S.chunksOf n (AT.writeNUnsafe n) (FH.read inh)
180204
181−hinspect $ hasNoTypeClasses 'chunksOf
182−hinspect $ 'chunksOf `hasNoType` ''Step
183−hinspect $ 'chunksOf `hasNoType` ''AT.FlattenState
184−hinspect $ 'chunksOf `hasNoType` ''D.ConcatMapUState
185−hinspect $ 'chunksOf `hasNoType` ''GroupState
205+#ifdef INSPECTION
206+inspect $ hasNoTypeClasses 'chunksOf
207+inspect $ 'chunksOf `hasNoType` ''Step
208+inspect $ 'chunksOf `hasNoType` ''AT.FlattenState
209+inspect $ 'chunksOf `hasNoType` ''D.ConcatMapUState
210+inspect $ 'chunksOf `hasNoType` ''GroupState
211+#endif
186212
187213-- This is to make sure that the concatMap in FH.read, groupsOf and foldlM'
188214-- together can fuse.
@@ -195,11 +221,13 @@ chunksOfD n inh =
195221 $ D.groupsOf n (AT.writeNUnsafe n)
196222 $ D.fromStreamK (FH.read inh)
197223
198−hinspect $ hasNoTypeClasses 'chunksOf
199−hinspect $ 'chunksOf `hasNoType` ''Step
200−hinspect $ 'chunksOfD `hasNoType` ''GroupState
201−hinspect $ 'chunksOfD `hasNoType` ''AT.FlattenState
202−hinspect $ 'chunksOfD `hasNoType` ''D.ConcatMapUState
224+#ifdef INSPECTION
225+inspect $ hasNoTypeClasses 'chunksOf
226+inspect $ 'chunksOf `hasNoType` ''Step
227+inspect $ 'chunksOfD `hasNoType` ''GroupState
228+inspect $ 'chunksOfD `hasNoType` ''AT.FlattenState
229+inspect $ 'chunksOfD `hasNoType` ''D.ConcatMapUState
230+#endif
203231
204232-- | Lines and unlines
205233{-# INLINE linesUnlinesCopy #-}
@@ -212,10 +240,12 @@ linesUnlinesCopy inh outh =
212240 $ SS.decodeChar8
213241 $ FH.read inh
214242
215−-- hinspect $ hasNoTypeClasses 'linesUnlinesCopy
216−-- hinspect $ 'linesUnlinesCopy `hasNoType` ''Step
217−-- hinspect $ 'linesUnlinesCopy `hasNoType` ''AT.FlattenState
218−-- hinspect $ 'linesUnlinesCopy `hasNoType` ''D.ConcatMapUState
243+#ifdef INSPECTION
244+-- inspect $ hasNoTypeClasses 'linesUnlinesCopy
245+-- inspect $ 'linesUnlinesCopy `hasNoType` ''Step
246+-- inspect $ 'linesUnlinesCopy `hasNoType` ''AT.FlattenState
247+-- inspect $ 'linesUnlinesCopy `hasNoType` ''D.ConcatMapUState
248+#endif
219249
220250-- | Word, unwords and copy
221251{-# INLINE wordsUnwordsCopy #-}
@@ -228,10 +258,12 @@ wordsUnwordsCopy inh outh =
228258 $ SS.decodeChar8
229259 $ FH.read inh
230260
231−-- hinspect $ hasNoTypeClasses 'wordsUnwordsCopy
232−-- hinspect $ 'wordsUnwordsCopy `hasNoType` ''Step
233−-- hinspect $ 'wordsUnwordsCopy `hasNoType` ''AT.FlattenState
234−-- hinspect $ 'wordsUnwordsCopy `hasNoType` ''D.ConcatMapUState
261+#ifdef INSPECTION
262+-- inspect $ hasNoTypeClasses 'wordsUnwordsCopy
263+-- inspect $ 'wordsUnwordsCopy `hasNoType` ''Step
264+-- inspect $ 'wordsUnwordsCopy `hasNoType` ''AT.FlattenState
265+-- inspect $ 'wordsUnwordsCopy `hasNoType` ''D.ConcatMapUState
266+#endif
235267
236268lf :: Word8
237269lf = fromIntegral (ord '\n')
@@ -267,10 +299,12 @@ splitOn inh =
267299 (S.length $ S.splitOn (== lf) FL.drain
268300 $ FH.read inh) -- >>= print
269301
270−hinspect $ hasNoTypeClasses 'splitOn
271−hinspect $ 'splitOn `hasNoType` ''Step
272−hinspect $ 'splitOn `hasNoType` ''AT.FlattenState
273−hinspect $ 'splitOn `hasNoType` ''D.ConcatMapUState
302+#ifdef INSPECTION
303+inspect $ hasNoTypeClasses 'splitOn
304+inspect $ 'splitOn `hasNoType` ''Step
305+inspect $ 'splitOn `hasNoType` ''AT.FlattenState
306+inspect $ 'splitOn `hasNoType` ''D.ConcatMapUState
307+#endif
274308
275309-- | Split suffix on line feed.
276310{-# INLINE splitOnSuffix #-}
@@ -279,10 +313,12 @@ splitOnSuffix inh =
279313 (S.length $ S.splitOnSuffix (== lf) FL.drain
280314 $ FH.read inh) -- >>= print
281315
282−hinspect $ hasNoTypeClasses 'splitOnSuffix
283−hinspect $ 'splitOnSuffix `hasNoType` ''Step
284−hinspect $ 'splitOnSuffix `hasNoType` ''AT.FlattenState
285−hinspect $ 'splitOnSuffix `hasNoType` ''D.ConcatMapUState
316+#ifdef INSPECTION
317+inspect $ hasNoTypeClasses 'splitOnSuffix
318+inspect $ 'splitOnSuffix `hasNoType` ''Step
319+inspect $ 'splitOnSuffix `hasNoType` ''AT.FlattenState
320+inspect $ 'splitOnSuffix `hasNoType` ''D.ConcatMapUState
321+#endif
286322
287323-- | Words by space
288324{-# INLINE wordsBy #-}
@@ -291,10 +327,12 @@ wordsBy inh =
291327 (S.length $ S.wordsBy isSp FL.drain
292328 $ FH.read inh) -- >>= print
293329
294−hinspect $ hasNoTypeClasses 'wordsBy
295−hinspect $ 'wordsBy `hasNoType` ''Step
296−hinspect $ 'wordsBy `hasNoType` ''AT.FlattenState
297−hinspect $ 'wordsBy `hasNoType` ''D.ConcatMapUState
330+#ifdef INSPECTION
331+inspect $ hasNoTypeClasses 'wordsBy
332+inspect $ 'wordsBy `hasNoType` ''Step
333+inspect $ 'wordsBy `hasNoType` ''AT.FlattenState
334+inspect $ 'wordsBy `hasNoType` ''D.ConcatMapUState
335+#endif
298336
299337-- | Split on a character sequence.
300338{-# INLINE splitOnSeq #-}
@@ -303,10 +341,12 @@ splitOnSeq str inh =
303341 (S.length $ Internal.splitOnSeq (toarr str) FL.drain
304342 $ FH.read inh) -- >>= print
305343
306−hinspect $ hasNoTypeClasses 'splitOnSeq
307−-- hinspect $ 'splitOnSeq `hasNoType` ''Step
308−-- hinspect $ 'splitOnSeq `hasNoType` ''AT.FlattenState
309−-- hinspect $ 'splitOnSeq `hasNoType` ''D.ConcatMapUState
344+#ifdef INSPECTION
345+inspect $ hasNoTypeClasses 'splitOnSeq
346+-- inspect $ 'splitOnSeq `hasNoType` ''Step
347+-- inspect $ 'splitOnSeq `hasNoType` ''AT.FlattenState
348+-- inspect $ 'splitOnSeq `hasNoType` ''D.ConcatMapUState
349+#endif
310350
311351-- | Split on suffix sequence.
312352{-# INLINE splitOnSuffixSeq #-}
@@ -315,7 +355,9 @@ splitOnSuffixSeq str inh =
315355 (S.length $ Internal.splitOnSuffixSeq (toarr str) FL.drain
316356 $ FH.read inh) -- >>= print
317357
318−hinspect $ hasNoTypeClasses 'splitOnSuffixSeq
319−-- hinspect $ 'splitOnSuffixSeq `hasNoType` ''Step
320−-- hinspect $ 'splitOnSuffixSeq `hasNoType` ''AT.FlattenState
321−-- hinspect $ 'splitOnSuffixSeq `hasNoType` ''D.ConcatMapUState
358+#ifdef INSPECTION
359+inspect $ hasNoTypeClasses 'splitOnSuffixSeq
360+-- inspect $ 'splitOnSuffixSeq `hasNoType` ''Step
361+-- inspect $ 'splitOnSuffixSeq `hasNoType` ''AT.FlattenState
362+-- inspect $ 'splitOnSuffixSeq `hasNoType` ''D.ConcatMapUState
363+#endif