@@ -247,4 +247,31 @@ benchShort = bgroup "ShortByteString"
247247 , bench " unpack and get last element" $ nf (\ x -> last . S. unpack $ x) absurdlong
248248 , bench " unpack and get first 120 elements" $ nf (\ x -> take 120 . S. unpack $ x) absurdlong
249249 ]
250+ , bgroup " ShortByteString unpack/uncons comparison" $
251+ [ bench " unpack and look at first 5 elements" $ nf (unpack5) absurdlong
252+ , bench " uncons consecutively 5 times" $ nf (uncons5) absurdlong
253+ , bench " unconsN 5" $ nf (unconsN) absurdlong
254+ ]
250255 ]
256+
257+
258+ unpack5 :: ShortByteString -> Bool
259+ unpack5 sbs = case S. unpack sbs of
260+ (a: b: c: d: e: _) -> True
261+ _ -> False
262+
263+
264+ uncons5 :: ShortByteString -> Bool
265+ uncons5 sbs
266+ | Just (a, r1) <- S. uncons sbs
267+ , Just (b, r2) <- S. uncons r1
268+ , Just (c, r3) <- S. uncons r2
269+ , Just (d, r4) <- S. uncons r3
270+ , Just (e, xs) <- S. uncons r4 = True
271+ | otherwise = False
272+
273+ unconsN :: ShortByteString -> Bool
274+ unconsN sbs = case S. unconsN 5 sbs of
275+ Just ([a, b, c, d, e], xs) -> True
276+ Just _ -> error " oops"
277+ _ -> False
0 commit comments