File: Vector.hs

package info (click to toggle)
haskell-serialise 0.2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: haskell: 6,809; makefile: 6
file content (83 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{-# LANGUAGE ScopedTypeVariables #-}

module Instances.Vector
  ( benchmarks -- :: [Benchmark]
  ) where

import           Data.Proxy
import           Criterion.Main
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector as V
import qualified Data.ByteString.Lazy as BSL
import           Codec.Serialise
import           Control.DeepSeq (force)

benchmarks :: [Benchmark]
benchmarks =
  [ bgroup "unboxed"
      [ bgroup "encode"
          [ vectorEncode unboxed (42 :: Int) 500
          , vectorEncode unboxed (42 :: Int) 5000
          , vectorEncode unboxed (42 :: Int) 50000
          , vectorEncode unboxed (42 :: Int) 500000
          ]
      , bgroup "decode"
          [ vectorDecode unboxed (42 :: Int) 500
          , vectorDecode unboxed (42 :: Int) 5000
          , vectorDecode unboxed (42 :: Int) 50000
          , vectorDecode unboxed (42 :: Int) 500000
          ]
      ]
  , bgroup "boxed"
      [ bgroup "encode"
          [ vectorEncode boxed (42 :: Int) 500
          , vectorEncode boxed (42 :: Int) 5000
          , vectorEncode boxed (42 :: Int) 50000
          , vectorEncode boxed (42 :: Int) 500000
          ]
      , bgroup "decode"
          [ vectorDecode boxed (42 :: Int) 500
          , vectorDecode boxed (42 :: Int) 5000
          , vectorDecode boxed (42 :: Int) 50000
          , vectorDecode boxed (42 :: Int) 500000
          ]
      ]
  ]
  where
    unboxed = Proxy :: Proxy VU.Vector
    boxed   = Proxy :: Proxy V.Vector

--------------------------------------------------------------------------------
-- Encoding

vectorEncode :: forall v a. (Serialise (v a), VG.Vector v a)
  => Proxy v
  -> a
  -> Int
  -> Benchmark
vectorEncode Proxy x len
  = bench (show len) $ benchEncode (asVector $ VG.replicate len x)
  where
    asVector v = v :: v a

    benchEncode :: Serialise s => s -> Benchmarkable
    benchEncode = whnf (BSL.length . serialise)
    {-# INLINE benchEncode #-}

--------------------------------------------------------------------------------
-- Decoding

vectorDecode :: forall v a. (Serialise (v a), VG.Vector v a, Num a)
  => Proxy v
  -> a
  -> Int
  -> Benchmark
vectorDecode Proxy x len
  = bench (show len) $ benchDecode (force $ serialise $ asVector $ VG.replicate len x)
  where
    asVector v = v :: v a

    benchDecode :: BSL.ByteString -> Benchmarkable
    benchDecode = whnf (VG.sum . asVector . deserialise)
    {-# INLINE benchDecode #-}