@@ -25,11 +25,13 @@ module Streamly.Benchmark.FileIO.Stream
2525 , countBytes
2626 , countLines
2727 , countLinesU
28+ , countWords
2829 , sumBytes
2930 , cat
3031 , catStreamWrite
3132 , copy
3233 , linesUnlinesCopy
34+ , wordsUnwordsCopyWord8
3335 , wordsUnwordsCopy
3436 , copyCodecChar8
3537 , copyCodecUtf8
@@ -105,6 +107,21 @@ inspect $ 'countLines `hasNoType` ''AT.FlattenState
105107inspect $ 'countLines `hasNoType` ''D.ConcatMapUState
106108#endif
107109
110+-- | Count the number of words in a file.
111+{-# INLINE countWords #-}
112+countWords :: Handle -> IO Int
113+countWords =
114+ S.length
115+ . SS.foldWords FL.drain
116+ . SS.decodeChar8
117+ . FH.read
118+
119+#ifdef INSPECTION
120+inspect $ hasNoTypeClasses 'countWords
121+-- inspect $ 'countWords `hasNoType` ''Step
122+-- inspect $ 'countWords `hasNoType` ''D.ConcatMapUState
123+#endif
124+
108125-- | Count the number of lines in a file.
109126{-# INLINE countLinesU #-}
110127countLinesU :: Handle -> IO Int
@@ -253,14 +270,67 @@ inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
253270-- inspect $ 'linesUnlinesCopy `hasNoType` ''D.ConcatMapUState
254271#endif
255272
273+foreign import ccall unsafe "u_iswspace"
274+ iswspace :: Int -> Int
275+
276+-- Code copied from base/Data.Char to INLINE it
277+{-# INLINE isSpace #-}
278+isSpace :: Char -> Bool
279+-- isSpace includes non-breaking space
280+-- The magic 0x377 isn't really that magical. As of 2014, all the codepoints
281+-- at or below 0x377 have been assigned, so we shouldn't have to worry about
282+-- any new spaces appearing below there. It would probably be best to
283+-- use branchless ||, but currently the eqLit transformation will undo that,
284+-- so we'll do it like this until there's a way around that.
285+isSpace c
286+ | uc <= 0x377 = uc == 32 || uc - 0x9 <= 4 || uc == 0xa0
287+ | otherwise = iswspace (ord c) /= 0
288+ where
289+ uc = fromIntegral (ord c) :: Word
290+
291+{-# INLINE isSp #-}
292+isSp :: Word8 -> Bool
293+isSp = isSpace . chr . fromIntegral
294+
295+-- | Word, unwords and copy
296+{-# INLINE wordsUnwordsCopyWord8 #-}
297+wordsUnwordsCopyWord8 :: Handle -> Handle -> IO ()
298+wordsUnwordsCopyWord8 inh outh =
299+ S.runFold (FH.write outh)
300+ $ Internal.concatMapU Internal.fromList
301+ $ S.intersperse [32]
302+ $ S.wordsBy isSp FL.toList
303+ $ FH.read inh
304+
305+#ifdef INSPECTION
306+inspect $ hasNoTypeClasses 'wordsUnwordsCopyWord8
307+-- inspect $ 'wordsUnwordsCopyWord8 `hasNoType` ''Step
308+-- inspect $ 'wordsUnwordsCopyWord8 `hasNoType` ''D.ConcatMapUState
309+#endif
310+
256311-- | Word, unwords and copy
257312{-# INLINE wordsUnwordsCopy #-}
258313wordsUnwordsCopy :: Handle -> Handle -> IO ()
259314wordsUnwordsCopy inh outh =
260315 S.runFold (FH.write outh)
261316 $ SS.encodeChar8
262− $ SS.unwords
263− $ SS.words
317+ $ Internal.concatMapU Internal.fromList
318+ $ S.intersperse " "
319+ -- Array allocation is too expensive for such small strings. So just use
320+ -- lists instead.
321+ --
322+ -- -- $ SS.unwords
323+ -- -- $ SS.words
324+ --
325+ -- XXX This pipeline does not fuse with wordsBy but fuses with splitOn
326+ -- with -funfolding-use-threshold=300. With wordsBy it does not fuse
327+ -- even with high limits for inlining and spec-constr ghc options. With
328+ -- -funfolding-use-threshold=400 it performs pretty well and there
329+ -- is no evidence in the core that a join point involving Step
330+ -- constructors is not getting inlined. Not being able to fuse at all in
331+ -- this case could be an unknown issue, need more investigation.
332+ $ S.wordsBy isSpace FL.toList
333+ -- -- $ S.splitOn isSpace FL.toList
264334 $ SS.decodeChar8
265335 $ FH.read inh
266336
@@ -277,27 +347,6 @@ lf = fromIntegral (ord '\n')
277347toarr :: String -> A.Array Word8
278348toarr = A.fromList . map (fromIntegral . ord)
279349
280−foreign import ccall unsafe "u_iswspace"
281− iswspace :: Int -> Int
282−
283−-- Code copied from base/Data.Char to INLINE it
284−{-# INLINE isSpace #-}
285−isSpace :: Char -> Bool
286−-- isSpace includes non-breaking space
287−-- The magic 0x377 isn't really that magical. As of 2014, all the codepoints
288−-- at or below 0x377 have been assigned, so we shouldn't have to worry about
289−-- any new spaces appearing below there. It would probably be best to
290−-- use branchless ||, but currently the eqLit transformation will undo that,
291−-- so we'll do it like this until there's a way around that.
292−isSpace c
293− | uc <= 0x377 = uc == 32 || uc - 0x9 <= 4 || uc == 0xa0
294− | otherwise = iswspace (ord c) /= 0
295− where
296− uc = fromIntegral (ord c) :: Word
297−
298−isSp :: Word8 -> Bool
299−isSp = isSpace . chr . fromIntegral
300−
301350-- | Split on line feed.
302351{-# INLINE splitOn #-}
303352splitOn :: Handle -> IO Int