on master help sign in
Add foldMany1 and arraysOf benchmarks
de31277Harendra Kumar
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+27−4
@@ -314,6 +314,10 @@ foldManyChunksOfSum :: Int -> Handle -> IO Int
314foldManyChunksOfSum n inh =
314foldManyChunksOfSum n inh =
315 S.length $ IP.foldMany (FL.takeLE n FL.sum) (S.unfold FH.read inh)
315 S.length $ IP.foldMany (FL.takeLE n FL.sum) (S.unfold FH.read inh)
316
316
317+foldMany1ChunksOfSum :: Int -> Handle -> IO Int
318+foldMany1ChunksOfSum n inh =
319+ S.length $ IP.foldMany1 (FL.takeLE n FL.sum) (S.unfold FH.read inh)
320+
317parseManyChunksOfSum :: Int -> Handle -> IO Int
321parseManyChunksOfSum :: Int -> Handle -> IO Int
318parseManyChunksOfSum n inh =
322parseManyChunksOfSum n inh =
319 S.length
323 S.length
@@ -327,7 +331,6 @@ parseManyChunksOfSum n inh =
327chunksOf :: Int -> Handle -> IO Int
331chunksOf :: Int -> Handle -> IO Int
328chunksOf n inh =
332chunksOf n inh =
329 -- writeNUnsafe gives 2.5x boost here over writeN.
333 -- writeNUnsafe gives 2.5x boost here over writeN.
330− -- XXX replace with S.arraysOf
331 S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.read inh)
334 S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.read inh)
332
335
333#ifdef INSPECTION
336#ifdef INSPECTION
@@ -339,6 +342,10 @@ inspect $ 'chunksOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.concat
339inspect $ 'chunksOf `hasNoType` ''MA.ReadUState -- FH.read/A.read
342inspect $ 'chunksOf `hasNoType` ''MA.ReadUState -- FH.read/A.read
340#endif
343#endif
341
344
345+{-# INLINE arraysOf #-}
346+arraysOf :: Int -> Handle -> IO Int
347+arraysOf n inh = S.length $ IP.arraysOf n (S.unfold FH.read inh)
348+
342o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
349o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
343o_1_space_reduce_read_grouped env =
350o_1_space_reduce_read_grouped env =
344 [ bgroup "reduce/read/chunks"
351 [ bgroup "reduce/read/chunks"
@@ -358,6 +365,14 @@ o_1_space_reduce_read_grouped env =
358 "S.foldMany (FL.take 1 FL.sum)"
365 "S.foldMany (FL.take 1 FL.sum)"
359 env
366 env
360 $ \inh _ -> inline foldManyChunksOfSum 1 inh
367 $ \inh _ -> inline foldManyChunksOfSum 1 inh
368+ , mkBenchSmall
369+ ("S.foldMany1 (FL.take " ++ show (bigSize env) ++ " FL.sum)")
370+ env
371+ $ \inh _ -> noinline foldMany1ChunksOfSum (bigSize env) inh
372+ , mkBench
373+ "S.foldMany1 (FL.take 1 FL.sum)"
374+ env
375+ $ \inh _ -> inline foldMany1ChunksOfSum 1 inh
361 , mkBenchSmall
376 , mkBenchSmall
362 ("S.parseMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
377 ("S.parseMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
363 env
378 env
@@ -368,12 +383,20 @@ o_1_space_reduce_read_grouped env =
368 $ \inh _ -> inline parseManyChunksOfSum 1 inh
383 $ \inh _ -> inline parseManyChunksOfSum 1 inh
369
384
370 -- folding chunks to arrays
385 -- folding chunks to arrays
371− , mkBenchSmall "S.arraysOf 1" env $ \inh _ ->
386+ , mkBenchSmall "S.chunksOf 1" env $ \inh _ ->
372 chunksOf 1 inh
387 chunksOf 1 inh
373− , mkBench "S.arraysOf 10" env $ \inh _ ->
388+ , mkBench "S.chunksOf 10" env $ \inh _ ->
374 chunksOf 10 inh
389 chunksOf 10 inh
375− , mkBench "S.arraysOf 1000" env $ \inh _ ->
390+ , mkBench "S.chunksOf 1000" env $ \inh _ ->
376 chunksOf 1000 inh
391 chunksOf 1000 inh
392+
393+ -- arraysOf may use a different impl than chunksOf
394+ , mkBenchSmall "S.arraysOf 1" env $ \inh _ ->
395+ arraysOf 1 inh
396+ , mkBench "S.arraysOf 10" env $ \inh _ ->
397+ arraysOf 10 inh
398+ , mkBench "S.arraysOf 1000" env $ \inh _ ->
399+ arraysOf 1000 inh
377 ]
400 ]
378 ]
401 ]
379
402
src/Streamly/Internal/Data/Stream/IsStream/Nesting.hs+9
@@ -127,6 +127,7 @@ module Streamly.Internal.Data.Stream.IsStream.Nesting
127 -- ** Folding
127 -- ** Folding
128 -- | Apply folds on a stream.
128 -- | Apply folds on a stream.
129 , foldMany
129 , foldMany
130+ , foldMany1
130 , foldSequence
131 , foldSequence
131 , foldIterate
132 , foldIterate
132
133
@@ -926,6 +927,14 @@ foldMany
926 -> t m b
927 -> t m b
927foldMany f m = D.fromStreamD $ D.foldMany f (D.toStreamD m)
928foldMany f m = D.fromStreamD $ D.foldMany f (D.toStreamD m)
928
929
930+{-# INLINE foldMany1 #-}
931+foldMany1
932+ :: (IsStream t, Monad m)
933+ => Fold m a b
934+ -> t m a
935+ -> t m b
936+foldMany1 f m = D.fromStreamD $ D.foldMany1 f (D.toStreamD m)
937+
929-- | Apply a stream of folds to an input stream and emit the results in the
938-- | Apply a stream of folds to an input stream and emit the results in the
930-- output stream.
939-- output stream.
931--
940--