on master help sign in
Add benchmarks for Parser.choice
commita322c073c3024877bd0a9b955d5408ef55ddbba2 authorAdithya Kumar <adithya@composewell.com> dateSun 1 Aug 2021, 23:20 (5 years ago) parent32eace2
2 files changed+18−4
benchmark/Streamly/Benchmark/Data/Parser.hs+9−2 benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+9−2
benchmark/Streamly/Benchmark/Data/Parser.hs+9−2
@@ -293,11 +293,17 @@ sequence_ :: MonadCatch m => Int -> SerialT m Int -> m ()
293293sequence_ value =
294294 IP.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
295295
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+
296302{-# INLINE choice #-}
297303choice :: MonadCatch m => Int -> SerialT m Int -> m Int
298304choice value =
299− IP.parse (asum (replicate value (PR.satisfy (< 0)))
300− AP.<|> PR.satisfy (> 0))
305+ IP.parse
306+ (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
301307
302308-------------------------------------------------------------------------------
303309-- Stream transformation
@@ -388,6 +394,7 @@ o_n_heap_serial value =
388394 -- non-linear time complexity (parserD)
389395 , benchIOSink value "split_" $ split_ value
390396 -- XXX why O(n) heap?
397+ , benchIOSink value "choice (asum)" $ choiceAsum value
391398 , benchIOSink value "choice" $ choice value
392399
393400 -- These show non-linear time complexity.
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+9−2
@@ -277,11 +277,17 @@ sequence_ value xs =
277277-- choice using the "Alternative" instance with direct style parser type has
278278-- quadratic performance complexity.
279279--
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+
280286{-# INLINE choice #-}
281287choice :: MonadCatch m => Int -> SerialT m Int -> m Int
282288choice value =
283− IP.parseD (asum (replicate value (PR.satisfy (< 0)))
284− AP.<|> PR.satisfy (> 0))
289+ IP.parseD
290+ (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
285291
286292-------------------------------------------------------------------------------
287293-- Benchmarks
@@ -356,6 +362,7 @@ o_n_space_serial value =
356362 , benchIOSink value "sequenceA_/100" $ sequenceA_ (value `div` 100)
357363 , benchIOSink value "sequence/100" $ sequence (value `div` 100)
358364 , benchIOSink value "sequence_/100" $ sequence_ (value `div` 100)
365+ , benchIOSink value "choice (asum)/100" $ choiceAsum (value `div` 100)
359366 , benchIOSink value "choice/100" $ choice (value `div` 100)
360367 ]
361368