author | nemo |
Mon, 25 Jun 2012 09:40:56 -0400 | |
changeset 7290 | 390d76b29ed0 |
parent 6805 | 097289be7200 |
child 7751 | 8c7f5c43ea5e |
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.Array.IArray as IA |
|
22 |
import qualified Data.Array.IO as IOA |
|
23 |
import qualified Data.IntSet as IntSet |
|
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) |
4905 | 31 |
newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, IOA.IOArray Int e)) |
32 |
newtype IStore e = IStore (IntSet.IntSet, IA.Array Int e) |
|
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 |
|
53 |
newar <- IOA.newArray_ (0, initialSize - 1) |
|
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 |
|
61 |
(_, m') <- IOA.getBounds arr |
|
62 |
let newM' = growFunc (m' + 1) - 1 |
|
63 |
newArr <- IOA.newArray_ (0, newM') |
|
64 |
sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- [0..m']] |
|
4932 | 65 |
writeIORef ref (busyElems, freeElems `IntSet.union` IntSet.fromAscList [m'+1..newM'], newArr) |
4905 | 66 |
|
67 |
||
68 |
growIfNeeded :: MStore e -> IO () |
|
69 |
growIfNeeded m@(MStore ref) = do |
|
70 |
(_, freeElems, _) <- readIORef ref |
|
71 |
when (IntSet.null freeElems) $ growStore m |
|
72 |
||
73 |
||
5003
db4726bf9205
Implement Store truncating, so the memory even gets freed sometimes
unc0rr
parents:
4932
diff
changeset
|
74 |
truncateIfNeeded :: MStore e -> IO () |
5119 | 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 | 79 |
when (newM' < m' && (not $ IntSet.null busyElems) && IntSet.findMax busyElems <= newM') $ do |
80 |
newArr <- IOA.newArray_ (0, newM') |
|
81 |
sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems] |
|
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 | 85 |
addElem :: MStore e -> e -> IO ElemIndex |
86 |
addElem m@(MStore ref) element = do |
|
87 |
growIfNeeded m |
|
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 | 90 |
IOA.writeArray arr n element |
91 |
writeIORef ref (IntSet.insert n busyElems, freeElems', arr) |
|
92 |
return $ ElemIndex n |
|
93 |
||
94 |
||
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 | 97 |
(busyElems, freeElems, arr) <- readIORef ref |
98 |
IOA.writeArray arr n (error $ "Store: no element " ++ show n) |
|
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 | 101 |
|
102 |
||
103 |
readElem :: MStore e -> ElemIndex -> IO e |
|
104 |
readElem (MStore ref) (ElemIndex n) = readIORef ref >>= \(_, _, arr) -> IOA.readArray arr n |
|
105 |
||
106 |
||
107 |
writeElem :: MStore e -> ElemIndex -> e -> IO () |
|
108 |
writeElem (MStore ref) (ElemIndex n) el = readIORef ref >>= \(_, _, arr) -> IOA.writeArray arr n el |
|
109 |
||
110 |
||
111 |
modifyElem :: MStore e -> (e -> e) -> ElemIndex -> IO () |
|
112 |
modifyElem (MStore ref) f (ElemIndex n) = do |
|
113 |
(_, _, arr) <- readIORef ref |
|
114 |
IOA.readArray arr n >>= IOA.writeArray arr n . f |
|
115 |
||
116 |
elemExists :: MStore e -> ElemIndex -> IO Bool |
|
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 | 119 |
return $ n `IntSet.notMember` free |
120 |
||
121 |
indicesM :: MStore e -> IO [ElemIndex] |
|
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 | 124 |
return $ map ElemIndex $ IntSet.toList busy |
125 |
||
126 |
||
127 |
-- A way to see MStore elements in pure code via IStore |
|
128 |
m2i :: MStore e -> IO (IStore e) |
|
129 |
m2i (MStore ref) = do |
|
130 |
(a, _, c') <- readIORef ref |
|
131 |
c <- IOA.unsafeFreeze c' |
|
132 |
return $ IStore (a, c) |
|
133 |
||
4932 | 134 |
i2m :: MStore e -> IStore e -> IO () |
4905 | 135 |
i2m (MStore ref) (IStore (_, arr)) = do |
136 |
(b, e, _) <- readIORef ref |
|
137 |
a <- IOA.unsafeThaw arr |
|
138 |
writeIORef ref (b, e, a) |
|
139 |
||
140 |
withIStore :: MStore e -> (IStore e -> a) -> IO a |
|
141 |
withIStore m f = do |
|
142 |
i <- m2i m |
|
143 |
let res = f i |
|
144 |
res `seq` i2m m i |
|
145 |
return res |
|
146 |
||
147 |
||
148 |
withIStore2 :: MStore e1 -> MStore e2 -> (IStore e1 -> IStore e2 -> a) -> IO a |
|
149 |
withIStore2 m1 m2 f = do |
|
150 |
i1 <- m2i m1 |
|
151 |
i2 <- m2i m2 |
|
152 |
let res = f i1 i2 |
|
153 |
res `seq` i2m m1 i1 |
|
154 |
i2m m2 i2 |
|
155 |
return res |
|
156 |
||
157 |
||
158 |
-- IStore code |
|
159 |
(!) :: IStore e -> ElemIndex -> e |
|
160 |
(!) (IStore (_, arr)) (ElemIndex i) = (IA.!) arr i |
|
161 |
||
162 |
indices :: IStore e -> [ElemIndex] |
|
163 |
indices (IStore (busy, _)) = map ElemIndex $ IntSet.toList busy |