gameServer/Store.hs
changeset 5119 f475e10c4081
parent 5003 db4726bf9205
child 6805 097289be7200
--- a/gameServer/Store.hs	Thu Apr 07 11:30:56 2011 -0400
+++ b/gameServer/Store.hs	Thu Apr 07 21:43:43 2011 +0400
@@ -69,23 +69,15 @@
     when (IntSet.null freeElems) $ growStore m
 
 
-truncateStore :: MStore e -> IO ()
-truncateStore (MStore ref) = do
-    (busyElems, freeElems, arr) <- readIORef ref
-    (_, m') <- IOA.getBounds arr
-    let newM' = truncFunc (m' + 1) - 1
-    newArr <- IOA.newArray_ (0, newM')
-    sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems]
-    writeIORef ref (busyElems, freeElems `IntSet.difference` IntSet.fromAscList [newM'..m'+1], newArr)
-
-
 truncateIfNeeded :: MStore e -> IO ()
-truncateIfNeeded m@(MStore ref) = do
+truncateIfNeeded (MStore ref) = do
     (busyElems, _, arr) <- readIORef ref
     (_, m') <- IOA.getBounds arr
     let newM' = truncFunc (m' + 1) - 1
-    let allLessM = all (< newM') $ IntSet.elems busyElems
-    when (newM' < m' && allLessM) $ truncateStore m
+    when (newM' < m' && (not $ IntSet.null busyElems) && IntSet.findMax busyElems <= newM') $ do
+        newArr <- IOA.newArray_ (0, newM')
+        sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems]
+        writeIORef ref (busyElems, IntSet.fromAscList [0..newM'] `IntSet.difference` busyElems, newArr)
 
 
 addElem :: MStore e -> e -> IO ElemIndex