on master help sign in
Add benchmarks for Parser.choice
a322c07Adithya Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+9−2
@@ -293,11 +293,17 @@ sequence_ :: MonadCatch m => Int -> SerialT m Int -> m ()
293sequence_ value =
293sequence_ value =
294 IP.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
294 IP.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
295
295
296+{-# INLINE choiceAsum #-}
297+choiceAsum :: MonadCatch m => Int -> SerialT m Int -> m Int
298+choiceAsum value =
299+ IP.parse (asum (replicate value (PR.satisfy (< 0)))
300+ AP.<|> PR.satisfy (> 0))
301+
296{-# INLINE choice #-}
302{-# INLINE choice #-}
297choice :: MonadCatch m => Int -> SerialT m Int -> m Int
303choice :: MonadCatch m => Int -> SerialT m Int -> m Int
298choice value =
304choice value =
305+ IP.parse
299− IP.parse (asum (replicate value (PR.satisfy (< 0)))
306+ (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
300− AP.<|> PR.satisfy (> 0))
301
307
302-------------------------------------------------------------------------------
308-------------------------------------------------------------------------------
303-- Stream transformation
309-- Stream transformation
@@ -388,6 +394,7 @@ o_n_heap_serial value =
388 -- non-linear time complexity (parserD)
394 -- non-linear time complexity (parserD)
389 , benchIOSink value "split_" $ split_ value
395 , benchIOSink value "split_" $ split_ value
390 -- XXX why O(n) heap?
396 -- XXX why O(n) heap?
397+ , benchIOSink value "choice (asum)" $ choiceAsum value
391 , benchIOSink value "choice" $ choice value
398 , benchIOSink value "choice" $ choice value
392
399
393 -- These show non-linear time complexity.
400 -- These show non-linear time complexity.
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+9−2
@@ -277,11 +277,17 @@ sequence_ value xs =
277-- choice using the "Alternative" instance with direct style parser type has
277-- choice using the "Alternative" instance with direct style parser type has
278-- quadratic performance complexity.
278-- quadratic performance complexity.
279--
279--
280+{-# INLINE choiceAsum #-}
281+choiceAsum :: MonadCatch m => Int -> SerialT m Int -> m Int
282+choiceAsum value =
283+ IP.parseD (asum (replicate value (PR.satisfy (< 0)))
284+ AP.<|> PR.satisfy (> 0))
285+
280{-# INLINE choice #-}
286{-# INLINE choice #-}
281choice :: MonadCatch m => Int -> SerialT m Int -> m Int
287choice :: MonadCatch m => Int -> SerialT m Int -> m Int
282choice value =
288choice value =
289+ IP.parseD
283− IP.parseD (asum (replicate value (PR.satisfy (< 0)))
290+ (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
284− AP.<|> PR.satisfy (> 0))
285
291
286-------------------------------------------------------------------------------
292-------------------------------------------------------------------------------
287-- Benchmarks
293-- Benchmarks
@@ -356,6 +362,7 @@ o_n_space_serial value =
356 , benchIOSink value "sequenceA_/100" $ sequenceA_ (value `div` 100)
362 , benchIOSink value "sequenceA_/100" $ sequenceA_ (value `div` 100)
357 , benchIOSink value "sequence/100" $ sequence (value `div` 100)
363 , benchIOSink value "sequence/100" $ sequence (value `div` 100)
358 , benchIOSink value "sequence_/100" $ sequence_ (value `div` 100)
364 , benchIOSink value "sequence_/100" $ sequence_ (value `div` 100)
365+ , benchIOSink value "choice (asum)/100" $ choiceAsum (value `div` 100)
359 , benchIOSink value "choice/100" $ choice (value `div` 100)
366 , benchIOSink value "choice/100" $ choice (value `div` 100)
360 ]
367 ]
361
368