on master help sign in
Add benchmark for takeFramedByEsc_
commitaa6625f449c252dfba7d45dfff7382170526cbe0 authorHarendra Kumar <harendra@composewell.com> dateSun 8 May 2022, 22:32 (4 years ago) parentd15c033
1 file changed+53
benchmark/Streamly/Benchmark/Data/Parser.hs+53
benchmark/Streamly/Benchmark/Data/Parser.hs+53
@@ -58,6 +58,46 @@ sourceUnfoldrM value n = S.unfoldrM step n
5858 then return Nothing
5959 else return (Just (cnt, cnt + 1))
6060
61+-- | Generates something like this: { { \{ \{ } }. The stream consists of
62+-- three parts, the first part is contains a sequence of `{`. The second part
63+-- contains a sequence pf escaped values `\{`. The third part contains a
64+-- sequence of `}`.
65+{-# INLINE sourceEscapedFrames #-}
66+sourceEscapedFrames ::
67+ (S.IsStream t, S.MonadAsync m)
68+ => Int
69+ -> Int
70+ -> t m Char
71+sourceEscapedFrames value n = S.unfoldrM step n
72+ where
73+
74+ bs = '\\'
75+ cbOpen = '{'
76+ cbClose = '}'
77+ value1 = value `div` 4
78+
79+ step cnt
80+ | cnt > 4 * value1 = return Nothing
81+ | cnt <= value1 = return $ Just (cbOpen, cnt + 1)
82+ | cnt > 3 * value1 = return $ Just (cbClose, cnt + 1)
83+ | otherwise =
84+ return
85+ $ Just
86+ $ if (cnt - value1) `mod` 2 == 1
87+ then (bs, cnt + 1)
88+ else (cbOpen, cnt + 1)
89+
90+{-# INLINE benchIOSrc #-}
91+benchIOSrc
92+ :: NFData b
93+ => (Int -> Int -> t IO a)
94+ -> Int
95+ -> String
96+ -> (t IO a -> IO b)
97+ -> Benchmark
98+benchIOSrc src value name f =
99+ bench name $ nfIO $ randomRIO (1,1) >>= f . src value
100+
61101-- | Takes a fold method, and uses it with a default source.
62102{-# INLINE benchIOSink #-}
63103benchIOSink
@@ -99,6 +139,17 @@ takeStartBy value stream = do
99139 let stream2 = value `S.cons` stream1
100140 IP.parse (PR.takeStartBy (== value) FL.drain) stream2
101141
142+takeFramedByEsc_ :: MonadCatch m => Int -> SerialT m Char -> m ()
143+takeFramedByEsc_ _ = IP.parse parser
144+
145+ where
146+
147+ isEsc = (== '\\')
148+ isBegin = (== '{')
149+ isEnd = (== '}')
150+
151+ parser = PR.takeFramedByEsc_ isEsc isBegin isEnd FL.drain
152+
102153{-# INLINE takeWhile #-}
103154takeWhile :: MonadCatch m => Int -> SerialT m Int -> m ()
104155takeWhile value = IP.parse (PR.takeWhile (<= value) FL.drain)
@@ -368,6 +419,8 @@ o_1_space_serial value =
368419 , benchIOSink value "takeP" $ takeP value
369420 , benchIOSink value "dropWhile" $ dropWhile value
370421 , benchIOSink value "takeStartBy" $ takeStartBy value
422+ , benchIOSrc sourceEscapedFrames value "takeFramedByEsc_"
423+ $ takeFramedByEsc_ value
371424 , benchIOSink value "groupBy" $ groupBy
372425 , benchIOSink value "groupByRolling" $ groupByRolling
373426 , benchIOSink value "wordBy" $ wordBy value