author | koda |
Thu, 13 Jun 2013 22:50:18 +0200 | |
changeset 9225 | d8d929f92633 |
parent 7751 | 8c7f5c43ea5e |
child 10460 | 8dcea9087d75 |
permissions | -rw-r--r-- |
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
5119
diff
changeset
|
1 |
{-# LANGUAGE BangPatterns, GeneralizedNewtypeDeriving #-} |
4905 | 2 |
module Store( |
3 |
ElemIndex(), |
|
4 |
MStore(), |
|
5 |
IStore(), |
|
6 |
newStore, |
|
7 |
addElem, |
|
8 |
removeElem, |
|
9 |
readElem, |
|
10 |
writeElem, |
|
11 |
modifyElem, |
|
12 |
elemExists, |
|
13 |
firstIndex, |
|
14 |
indicesM, |
|
15 |
withIStore, |
|
16 |
withIStore2, |
|
17 |
(!), |
|
18 |
indices |
|
19 |
) where |
|
20 |
||
21 |
import qualified Data.IntSet as IntSet |
|
7751 | 22 |
import qualified Data.Vector as V |
23 |
import qualified Data.Vector.Mutable as MV |
|
4905 | 24 |
import Data.IORef |
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 | 27 |
|
28 |
||
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) |
7751 | 31 |
newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, MV.IOVector e)) |
32 |
newtype IStore e = IStore (IntSet.IntSet, V.Vector e) |
|
4905 | 33 |
|
34 |
||
35 |
firstIndex :: ElemIndex |
|
36 |
firstIndex = ElemIndex 0 |
|
37 |
||
38 |
-- MStore code |
|
39 |
initialSize :: Int |
|
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
40 |
initialSize = 16 |
4905 | 41 |
|
42 |
||
43 |
growFunc :: Int -> Int |
|
44 |
growFunc a = a * 3 `div` 2 |
|
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 | 50 |
|
51 |
newStore :: IO (MStore e) |
|
52 |
newStore = do |
|
7751 | 53 |
newar <- MV.new initialSize |
4905 | 54 |
new <- newIORef (IntSet.empty, IntSet.fromAscList [0..initialSize - 1], newar) |
55 |
return (MStore new) |
|
56 |
||
57 |
||
58 |
growStore :: MStore e -> IO () |
|
59 |
growStore (MStore ref) = do |
|
60 |
(busyElems, freeElems, arr) <- readIORef ref |
|
7751 | 61 |
let oldSize = MV.length arr |
62 |
let newSize = growFunc oldSize |
|
63 |
newArr <- MV.grow arr (newSize - oldSize) |
|
64 |
writeIORef ref (busyElems, freeElems `IntSet.union` IntSet.fromAscList [oldSize .. newSize-1], newArr) |
|
4905 | 65 |
|
66 |
||
67 |
growIfNeeded :: MStore e -> IO () |
|
68 |
growIfNeeded m@(MStore ref) = do |
|
69 |
(_, freeElems, _) <- readIORef ref |
|
70 |
when (IntSet.null freeElems) $ growStore m |
|
71 |
||
72 |
||
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
73 |
truncateIfNeeded :: MStore e -> IO () |
5119 | 74 |
truncateIfNeeded (MStore ref) = do |
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
75 |
(busyElems, _, arr) <- readIORef ref |
7751 | 76 |
let oldSize = MV.length arr |
77 |
let newSize = truncFunc oldSize |
|
78 |
when (newSize < oldSize && (not $ IntSet.null busyElems) && IntSet.findMax busyElems < newSize) $ do |
|
79 |
writeIORef ref (busyElems, IntSet.fromAscList [0..newSize - 1] `IntSet.difference` busyElems, MV.take newSize arr) |
|
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
80 |
|
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
81 |
|
4905 | 82 |
addElem :: MStore e -> e -> IO ElemIndex |
83 |
addElem m@(MStore ref) element = do |
|
84 |
growIfNeeded m |
|
85 |
(busyElems, freeElems, arr) <- readIORef ref |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
5119
diff
changeset
|
86 |
let (!n, freeElems') = IntSet.deleteFindMin freeElems |
7751 | 87 |
MV.write arr n element |
4905 | 88 |
writeIORef ref (IntSet.insert n busyElems, freeElems', arr) |
89 |
return $ ElemIndex n |
|
90 |
||
91 |
||
92 |
removeElem :: MStore e -> ElemIndex -> IO () |
|
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
93 |
removeElem m@(MStore ref) (ElemIndex n) = do |
4905 | 94 |
(busyElems, freeElems, arr) <- readIORef ref |
7751 | 95 |
MV.write arr n (error $ "Store: no element " ++ show n) |
4905 | 96 |
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
|
97 |
truncateIfNeeded m |
4905 | 98 |
|
99 |
||
100 |
readElem :: MStore e -> ElemIndex -> IO e |
|
7751 | 101 |
readElem (MStore ref) (ElemIndex n) = readIORef ref >>= \(_, _, arr) -> MV.read arr n |
4905 | 102 |
|
103 |
||
104 |
writeElem :: MStore e -> ElemIndex -> e -> IO () |
|
7751 | 105 |
writeElem (MStore ref) (ElemIndex n) el = readIORef ref >>= \(_, _, arr) -> MV.write arr n el |
4905 | 106 |
|
107 |
||
108 |
modifyElem :: MStore e -> (e -> e) -> ElemIndex -> IO () |
|
109 |
modifyElem (MStore ref) f (ElemIndex n) = do |
|
110 |
(_, _, arr) <- readIORef ref |
|
7751 | 111 |
MV.read arr n >>= MV.write arr n . f |
4905 | 112 |
|
113 |
elemExists :: MStore e -> ElemIndex -> IO Bool |
|
114 |
elemExists (MStore ref) (ElemIndex n) = do |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
5119
diff
changeset
|
115 |
(_, !free, _) <- readIORef ref |
4905 | 116 |
return $ n `IntSet.notMember` free |
117 |
||
118 |
indicesM :: MStore e -> IO [ElemIndex] |
|
119 |
indicesM (MStore ref) = do |
|
6805
097289be7200
Add more strictness in hope it will help with space leak
unc0rr
parents:
5119
diff
changeset
|
120 |
(!busy, _, _) <- readIORef ref |
4905 | 121 |
return $ map ElemIndex $ IntSet.toList busy |
122 |
||
123 |
||
124 |
-- A way to see MStore elements in pure code via IStore |
|
125 |
m2i :: MStore e -> IO (IStore e) |
|
126 |
m2i (MStore ref) = do |
|
127 |
(a, _, c') <- readIORef ref |
|
7751 | 128 |
c <- V.unsafeFreeze c' |
4905 | 129 |
return $ IStore (a, c) |
130 |
||
4932 | 131 |
i2m :: MStore e -> IStore e -> IO () |
4905 | 132 |
i2m (MStore ref) (IStore (_, arr)) = do |
133 |
(b, e, _) <- readIORef ref |
|
7751 | 134 |
a <- V.unsafeThaw arr |
4905 | 135 |
writeIORef ref (b, e, a) |
136 |
||
137 |
withIStore :: MStore e -> (IStore e -> a) -> IO a |
|
138 |
withIStore m f = do |
|
139 |
i <- m2i m |
|
140 |
let res = f i |
|
141 |
res `seq` i2m m i |
|
142 |
return res |
|
143 |
||
144 |
||
145 |
withIStore2 :: MStore e1 -> MStore e2 -> (IStore e1 -> IStore e2 -> a) -> IO a |
|
146 |
withIStore2 m1 m2 f = do |
|
147 |
i1 <- m2i m1 |
|
148 |
i2 <- m2i m2 |
|
149 |
let res = f i1 i2 |
|
150 |
res `seq` i2m m1 i1 |
|
151 |
i2m m2 i2 |
|
152 |
return res |
|
153 |
||
154 |
||
155 |
-- IStore code |
|
156 |
(!) :: IStore e -> ElemIndex -> e |
|
7751 | 157 |
(!) (IStore (_, arr)) (ElemIndex i) = (V.!) arr i |
4905 | 158 |
|
159 |
indices :: IStore e -> [ElemIndex] |
|
160 |
indices (IStore (busy, _)) = map ElemIndex $ IntSet.toList busy |