gameServer/Store.hs
author sheepluva
Wed, 18 Jun 2014 19:57:25 +0200
changeset 7380 8bf3ad5a6f13
parent 6805 097289be7200
child 7751 8c7f5c43ea5e
permissions -rw-r--r--
closing wosteffs ancient debug commit head. default branch has been a dual-headed monster for long enough IMO
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
     1
{-# LANGUAGE BangPatterns, GeneralizedNewtypeDeriving #-}
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     2
module Store(
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     3
    ElemIndex(),
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     4
    MStore(),
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     5
    IStore(),
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     6
    newStore,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     7
    addElem,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     8
    removeElem,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
     9
    readElem,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    10
    writeElem,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    11
    modifyElem,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    12
    elemExists,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    13
    firstIndex,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    14
    indicesM,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    15
    withIStore,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    16
    withIStore2,
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    17
    (!),
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    18
    indices
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    19
    ) where
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    20
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    21
import qualified Data.Array.IArray as IA
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    22
import qualified Data.Array.IO as IOA
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    23
import qualified Data.IntSet as IntSet
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    24
import Data.IORef
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    25
import Control.Monad
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
    26
import Control.DeepSeq
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    27
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    28
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    29
newtype ElemIndex = ElemIndex Int
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
    30
    deriving (Eq, Show, Read, Ord, NFData)
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    31
newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, IOA.IOArray Int e))
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    32
newtype IStore e = IStore (IntSet.IntSet, IA.Array Int e)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    33
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    34
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    35
firstIndex :: ElemIndex
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    36
firstIndex = ElemIndex 0
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    37
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    38
-- MStore code
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    39
initialSize :: Int
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    40
initialSize = 16
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    41
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    42
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    43
growFunc :: Int -> Int
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    44
growFunc a = a * 3 `div` 2
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    45
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    46
truncFunc :: Int -> Int
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    47
truncFunc a | a > growFunc initialSize = (a `div` 2)
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    48
            | otherwise = a
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    49
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    50
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    51
newStore :: IO (MStore e)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    52
newStore = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    53
    newar <- IOA.newArray_ (0, initialSize - 1)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    54
    new <- newIORef (IntSet.empty, IntSet.fromAscList [0..initialSize - 1], newar)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    55
    return (MStore new)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    56
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    57
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    58
growStore :: MStore e -> IO ()
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    59
growStore (MStore ref) = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    60
    (busyElems, freeElems, arr) <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    61
    (_, m') <- IOA.getBounds arr
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    62
    let newM' = growFunc (m' + 1) - 1
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    63
    newArr <- IOA.newArray_ (0, newM')
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    64
    sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- [0..m']]
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4905
diff changeset
    65
    writeIORef ref (busyElems, freeElems `IntSet.union` IntSet.fromAscList [m'+1..newM'], newArr)
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    66
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    67
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    68
growIfNeeded :: MStore e -> IO ()
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    69
growIfNeeded m@(MStore ref) = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    70
    (_, freeElems, _) <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    71
    when (IntSet.null freeElems) $ growStore m
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    72
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    73
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    74
truncateIfNeeded :: MStore e -> IO ()
5119
f475e10c4081 Fix crash in server (accessing deleted room)
unc0rr
parents: 5003
diff changeset
    75
truncateIfNeeded (MStore ref) = do
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    76
    (busyElems, _, arr) <- readIORef ref
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    77
    (_, m') <- IOA.getBounds arr
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    78
    let newM' = truncFunc (m' + 1) - 1
5119
f475e10c4081 Fix crash in server (accessing deleted room)
unc0rr
parents: 5003
diff changeset
    79
    when (newM' < m' && (not $ IntSet.null busyElems) && IntSet.findMax busyElems <= newM') $ do
f475e10c4081 Fix crash in server (accessing deleted room)
unc0rr
parents: 5003
diff changeset
    80
        newArr <- IOA.newArray_ (0, newM')
f475e10c4081 Fix crash in server (accessing deleted room)
unc0rr
parents: 5003
diff changeset
    81
        sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems]
f475e10c4081 Fix crash in server (accessing deleted room)
unc0rr
parents: 5003
diff changeset
    82
        writeIORef ref (busyElems, IntSet.fromAscList [0..newM'] `IntSet.difference` busyElems, newArr)
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    83
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    84
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    85
addElem :: MStore e -> e -> IO ElemIndex
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    86
addElem m@(MStore ref) element = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    87
    growIfNeeded m
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    88
    (busyElems, freeElems, arr) <- readIORef ref
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
    89
    let (!n, freeElems') = IntSet.deleteFindMin freeElems
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    90
    IOA.writeArray arr n element
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    91
    writeIORef ref (IntSet.insert n busyElems, freeElems', arr)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    92
    return $ ElemIndex n
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    93
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    94
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    95
removeElem :: MStore e -> ElemIndex -> IO ()
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
    96
removeElem m@(MStore ref) (ElemIndex n) = do
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    97
    (busyElems, freeElems, arr) <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    98
    IOA.writeArray arr n (error $ "Store: no element " ++ show n)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
    99
    writeIORef ref (IntSet.delete n busyElems, IntSet.insert n freeElems, arr)
5003
db4726bf9205 Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents: 4932
diff changeset
   100
    truncateIfNeeded m
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   101
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   102
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   103
readElem :: MStore e -> ElemIndex -> IO e
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   104
readElem (MStore ref) (ElemIndex n) = readIORef ref >>= \(_, _, arr) -> IOA.readArray arr n
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   105
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   106
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   107
writeElem :: MStore e -> ElemIndex -> e -> IO ()
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   108
writeElem (MStore ref) (ElemIndex n) el = readIORef ref >>= \(_, _, arr) -> IOA.writeArray arr n el
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   109
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   110
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   111
modifyElem :: MStore e -> (e -> e) -> ElemIndex -> IO ()
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   112
modifyElem (MStore ref) f (ElemIndex n) = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   113
    (_, _, arr) <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   114
    IOA.readArray arr n >>= IOA.writeArray arr n . f
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   115
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   116
elemExists :: MStore e -> ElemIndex -> IO Bool
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   117
elemExists (MStore ref) (ElemIndex n) = do
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
   118
    (_, !free, _) <- readIORef ref
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   119
    return $ n `IntSet.notMember` free
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   120
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   121
indicesM :: MStore e -> IO [ElemIndex]
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   122
indicesM (MStore ref) = do
6805
097289be7200 Add more strictness in hope it will help with space leak
unc0rr
parents: 5119
diff changeset
   123
    (!busy, _, _) <- readIORef ref
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   124
    return $ map ElemIndex $ IntSet.toList busy
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   125
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   126
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   127
-- A way to see MStore elements in pure code via IStore
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   128
m2i :: MStore e -> IO (IStore e)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   129
m2i (MStore ref) = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   130
    (a, _, c') <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   131
    c <- IOA.unsafeFreeze c'
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   132
    return $ IStore (a, c)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   133
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4905
diff changeset
   134
i2m :: MStore e -> IStore e -> IO ()
4905
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   135
i2m (MStore ref) (IStore (_, arr)) = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   136
    (b, e, _) <- readIORef ref
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   137
    a <- IOA.unsafeThaw arr
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   138
    writeIORef ref (b, e, a)
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   139
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   140
withIStore :: MStore e -> (IStore e -> a) -> IO a
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   141
withIStore m f = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   142
    i <- m2i m
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   143
    let res = f i
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   144
    res `seq` i2m m i
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   145
    return res
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   146
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   147
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   148
withIStore2 :: MStore e1 -> MStore e2 -> (IStore e1 -> IStore e2 -> a) -> IO a
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   149
withIStore2 m1 m2 f = do
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   150
    i1 <- m2i m1
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   151
    i2 <- m2i m2
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   152
    let res = f i1 i2
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   153
    res `seq` i2m m1 i1
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   154
    i2m m2 i2
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   155
    return res
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   156
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   157
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   158
-- IStore code
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   159
(!) :: IStore e -> ElemIndex -> e
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   160
(!) (IStore (_, arr)) (ElemIndex i) = (IA.!) arr i
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   161
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   162
indices :: IStore e -> [ElemIndex]
7842d085acf4 Fix merge :D
unc0rr
parents:
diff changeset
   163
indices (IStore (busy, _)) = map ElemIndex $ IntSet.toList busy