summaryrefslogtreecommitdiff
path: root/src/Data/Bitfield.hs
blob: ac64779102a641644ba07b3d133da3ab6d5578a2 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
-- |
--   Copyright   :  (c) Sam T. 2013
--   License     :  MIT
--   Maintainer  :  pxqr.sta@gmail.com
--   Stability   :  experimental
--   Portability :  portable
--
--
--   This module provides Bitfield datatype used to represent sets of
--   piece indexes any peer have. All associated operations should be
--   defined here as well.
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
module Data.Bitfield
-- TODO: move to Data.Bitfield
       ( Bitfield(..)

         -- * Construction
       , empty, full
       , toList
       , fromByteString, toByteString

         -- * Query
       , haveCount, completeness
       , findMin, findMax
       , union, intersection, difference, combine
       , frequencies

         -- * Serialization
       , getBitfield, putBitfield
       , bitfieldByteCount, bitfieldBitCount


       , aligned, alignLow, alignedZip
       ) where

import Control.Applicative hiding (empty)
import Data.Array.Unboxed
import Data.Bits
import           Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import Data.List as L hiding (union)
import Data.Maybe
import Data.Serialize
import Data.Word

import Foreign

--import Network.BitTorrent.PeerWire.Block
import Data.Torrent

-- TODO: one good idea is to aggregate frequently used stats in reducer
-- it should give a big boost
newtype Bitfield = MkBitfield {
    bfBits :: ByteString
--  , bfSize :: Int
  } deriving (Show, Eq, Ord)


empty :: Int -> Bitfield
empty n = MkBitfield $ B.replicate (sizeInBase n 8) 0
{-# INLINE empty #-}

full :: Int -> Bitfield
full n = MkBitfield $ B.replicate (sizeInBase n 8)  (complement 0)
{-# INLINE full #-}

toList :: Bitfield -> [Bool]
toList (MkBitfield bs) = concatMap unpkg (B.unpack bs)
  where
    unpkg :: Word8 -> [Bool]
    unpkg byte = L.map (testBit byte) [0..bitSize (undefined :: Word8) - 1]
{-# INLINE toList #-}

fromByteString :: ByteString -> Bitfield
fromByteString = MkBitfield
{-# INLINE fromByteString #-}

toByteString :: Bitfield -> ByteString
toByteString = bfBits
{-# INLINE toByteString #-}

getBitfield :: Int -> Get Bitfield
getBitfield n = MkBitfield <$> getBytes n
{-# INLINE getBitfield #-}

putBitfield :: Bitfield -> Put
putBitfield = putByteString . bfBits
{-# INLINE putBitfield #-}

bitfieldByteCount :: Bitfield -> Int
bitfieldByteCount = B.length . bfBits
{-# INLINE bitfieldByteCount #-}

-- WARN
-- TODO
bitfieldBitCount :: Bitfield -> Int
bitfieldBitCount bf = bitSize (undefined :: Word8) * bitfieldByteCount bf
{-# INLINE bitfieldBitCount #-}

align :: Storable a => Ptr a -> (Ptr a, Int)
align p = tie (alignPtr p) undefined
  where
    tie :: Storable a => (Int -> Ptr a) -> a -> (Ptr a, Int)
    tie f a = (f (alignment a), (alignment a))

alignLow :: Ptr Word8 -> Ptr Word
alignLow ptr =
  let alg  = alignment (undefined :: Word)
      aptr = alignPtr (castPtr ptr) alg :: Ptr Word
  in
    if ptr == castPtr aptr
    then aptr
    else castPtr ((castPtr aptr :: Ptr Word8) `advancePtr` negate alg)

isAlignedBy :: Storable a => Ptr a -> Int -> Bool
isAlignedBy ptr alg = alignPtr ptr alg == ptr

type Mem a    = (Ptr a, Int)

aligned :: Storable a => Mem Word8 -> (Mem Word8, Mem a, Mem Word8)
aligned (ptr, len) =
  let lowPtr  = ptr
      lowLen  = midPtr `minusPtr` ptr
      midOff  = lowLen
      (midPtr, alg) = align (castPtr ptr)
      midLen  = alg * div (len - midOff) alg
      midLenA = midLen `div` alg
      hghOff  = midOff + midLen
      hghPtr  = ptr `advancePtr` hghOff
      hghLen  = len - hghOff
   in
     ((lowPtr, lowLen), (midPtr, midLenA), (hghPtr, hghLen))
  where
{-# INLINE aligned #-}

type Mem3 a = (Ptr a, Ptr a, Ptr a, Int)

emptyMem3 :: Mem3 a
emptyMem3 = (nullPtr, nullPtr, nullPtr, 0)

-- assume resulting memory is aligned
alignedZip :: Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> Int
              -> (Mem3 Word, Mem3 Word8)
alignedZip aptr bptr cptr size =
  let alg = alignment (undefined :: Word) in
  if (aptr `isAlignedBy` alg) && (bptr `isAlignedBy` alg)
  then
    let asize = alignLow (nullPtr `plusPtr` size) `minusPtr` nullPtr
    in
       ( (castPtr aptr, castPtr bptr, castPtr cptr, asize `div` alg)
       , ( aptr `advancePtr` asize
         , bptr `advancePtr` asize
         , cptr `advancePtr` asize
         , (size - asize)
         )
       )
  else (emptyMem3, (aptr, bptr, cptr, size))

-- force specialization
zipWithBS :: (Word -> Word -> Word)
          -> (Word8 -> Word8 -> Word8)
          -> ByteString -> ByteString -> ByteString
zipWithBS f g a b =
    let (afptr, aoff, asize) = B.toForeignPtr a
        (bfptr, boff, bsize) = B.toForeignPtr b
        size                = min asize bsize in
    B.unsafeCreate size $ \rptr -> do
      withForeignPtr afptr $ \_aptr -> do
        withForeignPtr bfptr $ \_bptr -> do
          let aptr = _aptr `advancePtr` aoff
          let bptr = _bptr `advancePtr` boff

          let (mid, hgh) = alignedZip aptr bptr rptr size
          zipWords mid
          zipBytes hgh
  where
    zipBytes :: (Ptr Word8, Ptr Word8, Ptr Word8, Int) -> IO ()
    zipBytes (aptr, bptr, rptr, n) = go 0
      where
        go :: Int -> IO ()
        go i |   i < n   = do -- TODO unfold
               av <- peekElemOff aptr i
               bv <- peekElemOff bptr i
               pokeElemOff rptr i (g av bv)
               go (succ i)
             | otherwise = return ()

    zipWords :: (Ptr Word, Ptr Word, Ptr Word, Int) -> IO ()
    zipWords (aptr, bptr, rptr, n) = go 0
      where
        go :: Int -> IO ()
        go i |   i < n   = do -- TODO unfold
               av <- peekElemOff aptr i
               bv <- peekElemOff bptr i
               pokeElemOff rptr i (f av bv)
               go (succ i)
             | otherwise = return ()



zipWithBF :: (forall a. Bits a => a -> a -> a) -> Bitfield -> Bitfield -> Bitfield
zipWithBF f a b = MkBitfield $ zipWithBS f f (bfBits a) (bfBits b)
{-# INLINE zipWithBF #-}

findSet :: ByteString -> Maybe Int
findSet b =
    let (fptr, off, len) = B.toForeignPtr b in
    B.inlinePerformIO $ withForeignPtr fptr $ \_ptr -> do
      let ptr = _ptr `advancePtr` off

      let (low, mid, hgh) = aligned (ptr, len)
      let lowOff = fst low `minusPtr` ptr
      let midOff = fst mid `minusPtr` ptr
      let hghOff = fst hgh `minusPtr` ptr

      let resL = (lowOff +) <$> goFind  low
      let resM = (midOff +) <$> goFind (mid :: Mem Word) -- tune size here
                                       --  TODO: with Word8
                                       -- bytestring findIndex works 2
                                       -- times faster.
      let resH = (hghOff +) <$> goFind  hgh

      let res  = resL <|> resM <|> resH

      -- computation of res should not escape withForeignPtr
      case res of
        Nothing -> return ()
        Just _  -> return ()

      return res

  where
    goFind :: (Storable a, Eq a, Num a) => Mem a -> Maybe Int
    goFind (ptr, n) = go 0
      where
        go :: Int -> Maybe Int
        go i |   i < n   =
               let v = B.inlinePerformIO (peekElemOff ptr i) in
               if v /= 0
                 then Just i
                 else go (succ i)
             | otherwise = Nothing

foldBS :: (Word8 -> Int -> Int) -> (Word -> Int -> Int) -> Int -> ByteString -> Int
foldBS f g acc b =
    let (fptr, off, len) = B.toForeignPtr b in
    B.inlinePerformIO $ withForeignPtr fptr $ \_ptr -> do
      let ptr = _ptr `advancePtr` off

      let (low, mid, hgh) = aligned (ptr, len)
      let resL = goFold   low acc
      let resM = goFoldW (mid :: Mem Word) resL
      let resH = goFold   hgh resM

      -- computation of res should not escape withForeignPtr
      case resH of
        0 -> return ()
        _ -> return ()

      return resH

  where
    goFold :: Mem Word8 -> Int -> Int
    goFold (ptr, n) = go 0
      where
        go :: Int -> Int -> Int
        go i !a
          |   i < n   =
            let v = B.inlinePerformIO (peekElemOff ptr i)
            in go (succ i) (f v a)
          | otherwise = a

    goFoldW :: Mem Word -> Int -> Int
    goFoldW (ptr, n) = go 0
      where
        go :: Int -> Int -> Int
        go i !a
          |   i < n   =
            let v = B.inlinePerformIO (peekElemOff ptr i)
            in go (succ i) (g v a)
          | otherwise = a

union :: Bitfield -> Bitfield -> Bitfield
union = zipWithBF (.|.)
{-# INLINE union #-}

intersection :: Bitfield -> Bitfield -> Bitfield
intersection = zipWithBF (.&.)
{-# INLINE intersection #-}

difference :: Bitfield -> Bitfield -> Bitfield
difference = zipWithBF diffWord8
  where
    diffWord8 :: Bits a => a -> a -> a
    diffWord8 a b = a .&. (a `xor` b)
    {-# INLINE diffWord8 #-}
{-# INLINE difference #-}

combine :: [Bitfield] -> Maybe Bitfield
combine [] = Nothing
combine as = return $ foldr1 intersection as

haveCount :: Bitfield -> Int
haveCount (MkBitfield b) = foldBS f f 0 b
  where
    f byte count = popCount byte + count

completeness :: Bitfield -> (Int, Int)
completeness bf = (haveCount bf, bitfieldBitCount bf)

-- | Get min index of piece that the peer have.
findMin :: Bitfield -> Maybe Int
findMin (MkBitfield b) = do
    byteIx <- findSet b
    bitIx  <- findMinWord8 (B.index b byteIx)
    return $ byteIx * bitSize (undefined :: Word8) + bitIx
  where
    -- TODO: bit tricks
    findMinWord8 :: Word8 -> Maybe Int
    findMinWord8 byte = L.find (testBit byte) [0..bitSize (undefined :: Word8) - 1]
    {-# INLINE findMinWord8 #-}
{-# INLINE findMin #-}


findMax :: Bitfield -> Maybe Int
findMax (MkBitfield b) = do
    -- TODO avoid reverse
    byteIx <- (pred (B.length b) -) <$> findSet (B.reverse b)
    bitIx  <- findMaxWord8 (B.index b byteIx)
    return $ byteIx * bitSize (undefined :: Word8) + bitIx
  where
    -- TODO: bit tricks
    findMaxWord8 :: Word8 -> Maybe Int
    findMaxWord8 byte = L.find (testBit byte)
                             (reverse [0 :: Int ..
                                            bitSize (undefined :: Word8) - 1])

{-# INLINE findMax #-}

frequencies :: [Bitfield] -> [Int]
frequencies xs = foldr1 (zipWith (+)) $ map (map fromEnum . toList) xs