@@ -40,15 +40,18 @@ import Data.Word (Word8)
4040import System.IO (Handle)
4141import Prelude hiding (last, length)
4242
43−import Streamly.Streams.StreamD.Type (Step(..))
43+import Streamly.Streams.StreamD.Type (Step(..), GroupState)
4444import Streamly.Benchmark.Inspection (hinspect)
45+import qualified Streamly.Memory.Array.Types as AT
4546
4647import qualified Streamly.FileSystem.Handle as FH
48+import qualified Streamly.FileSystem.Handle.Internal as FH
4749import qualified Streamly.Memory.Array as A
48−import qualified Streamly.Prelude as S
50+import qualified Streamly.Prelude.Internal as S
4951import qualified Streamly.Fold as FL
5052import qualified Streamly.Data.String as SS
5153import qualified Streamly.Internal as Internal
54+import qualified Streamly.Streams.StreamD as D
5255
5356import Test.Inspection
5457
@@ -59,7 +62,9 @@ last = S.last . FH.read
5962
6063hinspect $ hasNoTypeClasses 'last
6164hinspect $ 'last `hasNoType` ''Step
65+hinspect $ 'last `hasNoType` ''AT.FlattenState
6266
67+-- assert that flattenArrays constructors are not present
6368-- | Count the number of bytes in a file.
6469{-# INLINE countBytes #-}
6570countBytes :: Handle -> IO Int
@@ -67,6 +72,7 @@ countBytes = S.length . FH.read
6772
6873hinspect $ hasNoTypeClasses 'countBytes
6974hinspect $ 'countBytes `hasNoType` ''Step
75+hinspect $ 'countBytes `hasNoType` ''AT.FlattenState
7076
7177-- | Count the number of lines in a file.
7278{-# INLINE countLines #-}
@@ -79,6 +85,7 @@ countLines =
7985
8086hinspect $ hasNoTypeClasses 'countLines
8187hinspect $ 'countLines `hasNoType` ''Step
88+hinspect $ 'countLines `hasNoType` ''AT.FlattenState
8289
8390-- | Count the number of lines in a file.
8491{-# INLINE countLinesU #-}
@@ -91,6 +98,7 @@ countLinesU inh =
9198
9299hinspect $ hasNoTypeClasses 'countLinesU
93100hinspect $ 'countLinesU `hasNoType` ''Step
101+hinspect $ 'countLinesU `hasNoType` ''Either
94102
95103-- | Sum the bytes in a file.
96104{-# INLINE sumBytes #-}
@@ -99,6 +107,7 @@ sumBytes = S.sum . FH.read
99107
100108hinspect $ hasNoTypeClasses 'sumBytes
101109hinspect $ 'sumBytes `hasNoType` ''Step
110+hinspect $ 'sumBytes `hasNoType` ''AT.FlattenState
102111
103112-- | Send the file contents to /dev/null
104113{-# INLINE cat #-}
@@ -107,6 +116,7 @@ cat devNull inh = S.runFold (FH.write devNull) $ FH.read inh
107116
108117hinspect $ hasNoTypeClasses 'cat
109118hinspect $ 'cat `hasNoType` ''Step
119+hinspect $ 'cat `hasNoType` ''AT.FlattenState
110120
111121-- | Send the file contents to /dev/null
112122{-# INLINE catStreamWrite #-}
@@ -115,6 +125,7 @@ catStreamWrite devNull inh = Internal.writeS devNull $ FH.read inh
115125
116126hinspect $ hasNoTypeClasses 'catStreamWrite
117127hinspect $ 'catStreamWrite `hasNoType` ''Step
128+hinspect $ 'catStreamWrite `hasNoType` ''AT.FlattenState
118129
119130-- | Copy file
120131{-# INLINE copy #-}
@@ -123,6 +134,7 @@ copy inh outh = S.runFold (FH.write outh) (FH.read inh)
123134
124135hinspect $ hasNoTypeClasses 'copy
125136hinspect $ 'copy `hasNoType` ''Step
137+hinspect $ 'copy `hasNoType` ''AT.FlattenState
126138
127139-- | Copy file
128140{-# INLINE copyCodecChar8 #-}
@@ -135,6 +147,7 @@ copyCodecChar8 inh outh =
135147
136148hinspect $ hasNoTypeClasses 'copyCodecChar8
137149hinspect $ 'copyCodecChar8 `hasNoType` ''Step
150+hinspect $ 'copyCodecChar8 `hasNoType` ''AT.FlattenState
138151
139152-- | Copy file
140153{-# INLINE copyCodecUtf8 #-}
@@ -147,15 +160,34 @@ copyCodecUtf8 inh outh =
147160
148161hinspect $ hasNoTypeClasses 'copyCodecUtf8
149162-- hinspect $ 'copyCodecUtf8 `hasNoType` ''Step
163+-- hinspect $ 'copyCodecUtf8 `hasNoType` ''AT.FlattenState
150164
151165-- | Slice in chunks of size n and get the count of chunks.
152166{-# INLINE chunksOf #-}
153167chunksOf :: Int -> Handle -> IO Int
154168chunksOf n inh =
155− S.length $ S.chunksOf n (A.writeN n) (FH.read inh)
169+ S.length $ S.chunksOf n (AT.writeNUnsafe n) (FH.read inh)
156170
157171hinspect $ hasNoTypeClasses 'chunksOf
158172hinspect $ 'chunksOf `hasNoType` ''Step
173+-- hinspect $ 'chunksOf `hasNoType` ''AT.FlattenState
174+-- hinspect $ 'chunksOf `hasNoType` ''GroupState
175+
176+-- This is to make sure that the concatMap in FH.read, groupsOf and foldlM'
177+-- together can fuse.
178+--
179+-- | Slice in chunks of size n and get the count of chunks.
180+{-# INLINE chunksOfD #-}
181+chunksOfD :: Int -> Handle -> IO Int
182+chunksOfD n inh =
183+ D.foldlM' (\i _ -> return $ i + 1) 0
184+ $ D.groupsOf n (AT.writeNUnsafe n)
185+ $ D.fromStreamK (FH.read inh)
186+
187+hinspect $ hasNoTypeClasses 'chunksOf
188+hinspect $ 'chunksOf `hasNoType` ''Step
189+-- hinspect $ 'chunksOfD `hasNoType` ''GroupState
190+-- hinspect $ 'chunksOfD `hasNoType` ''AT.FlattenState
159191
160192-- | Lines and unlines
161193{-# INLINE linesUnlinesCopy #-}
@@ -170,6 +202,7 @@ linesUnlinesCopy inh outh =
170202
171203-- hinspect $ hasNoTypeClasses 'linesUnlinesCopy
172204-- hinspect $ 'linesUnlinesCopy `hasNoType` ''Step
205+-- hinspect $ 'linesUnlinesCopy `hasNoType` ''AT.FlattenState
173206
174207-- | Word, unwords and copy
175208{-# INLINE wordsUnwordsCopy #-}
@@ -184,6 +217,7 @@ wordsUnwordsCopy inh outh =
184217
185218-- hinspect $ hasNoTypeClasses 'wordsUnwordsCopy
186219-- hinspect $ 'wordsUnwordsCopy `hasNoType` ''Step
220+-- hinspect $ 'wordsUnwordsCopy `hasNoType` ''AT.FlattenState
187221
188222lf :: Word8
189223lf = fromIntegral (ord '\n')
@@ -221,6 +255,7 @@ splitOn inh =
221255
222256hinspect $ hasNoTypeClasses 'splitOn
223257hinspect $ 'splitOn `hasNoType` ''Step
258+hinspect $ 'splitOn `hasNoType` ''AT.FlattenState
224259
225260-- | Split suffix on line feed.
226261{-# INLINE splitOnSuffix #-}
@@ -231,6 +266,7 @@ splitOnSuffix inh =
231266
232267hinspect $ hasNoTypeClasses 'splitOnSuffix
233268hinspect $ 'splitOnSuffix `hasNoType` ''Step
269+hinspect $ 'splitOnSuffix `hasNoType` ''AT.FlattenState
234270
235271-- | Words by space
236272{-# INLINE wordsBy #-}
@@ -241,6 +277,7 @@ wordsBy inh =
241277
242278hinspect $ hasNoTypeClasses 'wordsBy
243279hinspect $ 'wordsBy `hasNoType` ''Step
280+hinspect $ 'wordsBy `hasNoType` ''AT.FlattenState
244281
245282-- | Split on a character sequence.
246283{-# INLINE splitOnSeq #-}
@@ -251,6 +288,7 @@ splitOnSeq str inh =
251288
252289hinspect $ hasNoTypeClasses 'splitOnSeq
253290-- hinspect $ 'splitOnSeq `hasNoType` ''Step
291+-- hinspect $ 'splitOnSeq `hasNoType` ''AT.FlattenState
254292
255293-- | Split on suffix sequence.
256294{-# INLINE splitOnSuffixSeq #-}
@@ -261,3 +299,4 @@ splitOnSuffixSeq str inh =
261299
262300hinspect $ hasNoTypeClasses 'splitOnSuffixSeq
263301-- hinspect $ 'splitOnSuffixSeq `hasNoType` ''Step
302+-- hinspect $ 'splitOnSuffixSeq `hasNoType` ''AT.FlattenState