diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-02 05:01:46 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-02 05:01:46 +0400 |
commit | 01f51b51af8a67516238bc7264079601a7e2ece5 (patch) | |
tree | 13b346ad0ba930e7d964a3de9988365f6cc6ba4f /bench/Main.hs | |
parent | 5e92eec501e0a1ca6d09a01e078cf54ff3277273 (diff) |
~ Use IntSet instead of ByteString for bitfields.
There are several reasons for this:
* IntSet is stored in ordinary heap, while ByteStrings in pinned memory;
* Our IntSet's should be much faster 90% time. (in typical BT client)
Hovewer in worst case IntSet is slower, but difference should is not
so big. (We should measure this although)
* It's pure, tested, error-free and much more convenient.
Moreover we have kill a lot of ugly code!
Diffstat (limited to 'bench/Main.hs')
-rw-r--r-- | bench/Main.hs | 69 |
1 files changed, 2 insertions, 67 deletions
diff --git a/bench/Main.hs b/bench/Main.hs index 6e8a0ce3..120f5b04 100644 --- a/bench/Main.hs +++ b/bench/Main.hs | |||
@@ -22,8 +22,7 @@ instance NFData BlockIx where | |||
22 | instance NFData Block where | 22 | instance NFData Block where |
23 | rnf (Block a b c) = a `deepseq` b `deepseq` rnf c | 23 | rnf (Block a b c) = a `deepseq` b `deepseq` rnf c |
24 | 24 | ||
25 | instance NFData Bitfield where | 25 | instance NFData Bitfield |
26 | rnf = rnf . bfBits | ||
27 | 26 | ||
28 | instance NFData Message where | 27 | instance NFData Message where |
29 | rnf (Have i) = rnf i | 28 | rnf (Have i) = rnf i |
@@ -40,69 +39,5 @@ encodeMessages xs = runPut (mapM_ put xs) | |||
40 | decodeMessages :: ByteString -> Either String [Message] | 39 | decodeMessages :: ByteString -> Either String [Message] |
41 | decodeMessages = runGet (many get) | 40 | decodeMessages = runGet (many get) |
42 | 41 | ||
43 | bitfieldMin :: Int -> Maybe Int | ||
44 | bitfieldMin n = findMin (BT.empty n) | ||
45 | |||
46 | bitfieldMax :: Int -> Maybe Int | ||
47 | bitfieldMax n = findMax (BT.empty n) | ||
48 | |||
49 | bitfieldDiff :: Int -> Bitfield | ||
50 | bitfieldDiff n = BT.empty n `difference` BT.empty n | ||
51 | |||
52 | bitfieldInter :: Int -> Bitfield | ||
53 | bitfieldInter n = BT.empty n `intersection` BT.empty n | ||
54 | |||
55 | bitfieldUnion :: Int -> Bitfield | ||
56 | bitfieldUnion n = BT.empty n `union` BT.empty n | ||
57 | |||
58 | bitfieldHaveCount :: Int -> Int | ||
59 | bitfieldHaveCount n = haveCount (BT.full n) | ||
60 | |||
61 | selectionStrictFirst :: Int -> Maybe Int | ||
62 | selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) [] | ||
63 | |||
64 | selectionStrictLast :: Int -> Maybe Int | ||
65 | selectionStrictLast n = strictLast (BT.empty n) (BT.empty n) [] | ||
66 | |||
67 | selectionRarestFirst :: Int -> Maybe Int | ||
68 | selectionRarestFirst n = rarestFirst (BT.empty n) (BT.empty n) | ||
69 | (replicate 10 (BT.empty n)) | ||
70 | |||
71 | selectionEndGame :: Int -> Maybe Int | ||
72 | selectionEndGame n = endGame (BT.empty n) (BT.empty n) [] | ||
73 | |||
74 | main :: IO () | 42 | main :: IO () |
75 | main = do | 43 | main = defaultMain [] |
76 | let blockixs = replicate 5000 (Request (BlockIx 0 0 0)) | ||
77 | let bitfields = replicate 5000 (Bitfield (MkBitfield (B.replicate 1000 0))) | ||
78 | let chokes = replicate 5000 Choke | ||
79 | let havenones = replicate 5000 HaveNone | ||
80 | |||
81 | let m = 1024 * 1024 | ||
82 | |||
83 | defaultMain $ | ||
84 | concatMap (uncurry mkMsgBench) | ||
85 | [ ("blockIx", blockixs) | ||
86 | , ("bitfield", bitfields) | ||
87 | , ("choke", chokes) | ||
88 | , ("havenone", havenones) | ||
89 | ] | ||
90 | ++ -- 256KiB * 10M = 2.5TiB bitfield for 10 ms | ||
91 | [ bench "bitfield/min" $ nf bitfieldMin (10 * m) | ||
92 | , bench "bitfield/max" $ nf bitfieldMax (10 * m) | ||
93 | , bench "bitfield/difference" $ nf bitfieldDiff (10 * m) | ||
94 | , bench "bitfield/intersection" $ nf bitfieldInter (10 * m) | ||
95 | , bench "bitfield/union" $ nf bitfieldUnion (10 * m) | ||
96 | , bench "bitfield/haveCount" $ nf bitfieldHaveCount (10 * m) | ||
97 | |||
98 | , bench "selection/strictFirst" $ nf selectionStrictFirst (10 * m) | ||
99 | , bench "selection/strictLast" $ nf selectionStrictLast (10 * m) | ||
100 | , bench "selection/rarestFirst" $ nf selectionRarestFirst (10 * m) | ||
101 | , bench "selection/endGame" $ nf selectionEndGame (10 * m) | ||
102 | ] | ||
103 | where | ||
104 | mkMsgBench name msgs = | ||
105 | [ msgs `deepseq` bench ("message/" ++ name ++ "/encode") $ nf encodeMessages msgs | ||
106 | , let binary = encodeMessages msgs in | ||
107 | binary `deepseq` bench ("message/" ++ name ++ "/decode") $ nf decodeMessages binary | ||
108 | ] | ||