Skip to content

Commit cfb1367

Browse files
authored
Unbox the SetM array (#528)
`Data.Graph` uses a mutable array to track which vertices have been visited. This was implemented using an `STArray s Vertex Bool`. This is obviously bad, because the garbage collector will have to trace the array if it's alive during a collection. Using an unboxed array instead should always be better. Indeed, doing so will also dramatically reduce the size of the array, from the number of vertices in the graph to that divided by the word size. Each array operation is slightly more complex, but we'll surely win from memory locality and such anyway.
1 parent 84d6480 commit cfb1367

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Data/Graph.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module Data.Graph(
7272
-- Extensions
7373
#if USE_ST_MONAD
7474
import Control.Monad.ST
75-
import Data.Array.ST (STArray, newArray, readArray, writeArray)
75+
import Data.Array.ST (STUArray, newArray, readArray, writeArray)
7676
#else
7777
import Data.IntSet (IntSet)
7878
import qualified Data.IntSet as Set
@@ -372,7 +372,7 @@ chop (Node v ts : us)
372372

373373
-- Use the ST monad if available, for constant-time primitives.
374374

375-
newtype SetM s a = SetM { runSetM :: STArray s Vertex Bool -> ST s a }
375+
newtype SetM s a = SetM { runSetM :: STUArray s Vertex Bool -> ST s a }
376376

377377
instance Monad (SetM s) where
378378
return = pure

0 commit comments

Comments
 (0)