Heap-based priority queue causes a lot of garbage collection in Haskell

I am trying to create a heuristic search library in Haskell, and one of the main things that make the algorithm anyway is the data structure; so I declared a class:

class DataStructure ds where  
  add :: Eq a => Node a -> ds a -> ds a
  addList :: Eq a => [Node a] -> ds a -> ds a
  next :: Eq a => ds a -> (ds a, Node a)
  isEmpty :: Eq a => ds a -> Bool

And then instances of this class are used in the general search algorithm, which extends the following nodes:

generalSearch :: (DataStructure ds, Eq a, Hashable a)
  => ProblemSpace a   -- ^ 'ProblemSpace' to be solved
  -> Cost a           -- ^ 'Cost' function to use
  -> Heuristic a      -- ^ 'Heuristic' function to use
  -> ds a             -- ^ 'DataStructure' that manages the node expansion
  -> [Node a]         -- ^ Returns the infinite list of final nodes (solutions)

generalSearch problem g h open
  | isEmpty open                    = []
  | getGoalF problem (getState n)   = n : generalSearch problem g h open'
  | otherwise                       = generalSearch problem g h open''
  where (open', n) = next open
        expanded = expand n g h (getActions problem)
        open'' = addList expanded open'

This last piece of code is quite difficult to understand without the full context of the module, but the important part is the last line: it uses the method DataStructure addListto merge the just extended nodes using the rest of the open list that is executed at runtime.

, - : , ... heap PriorityQueue :

data PriorityQueue a =
  PriorityQueue { priorityQueueToHeap :: MinPrioHeap Double (Node a)
                , valueFunction :: Node a -> Double }

, valueFunction. addList :

addList :: Eq a => [Node a] -> PriorityQueue a -> PriorityQueue a
addList xs (PriorityQueue h f) = PriorityQueue (union h xs') f
  where xs' = fromList $ map (\n -> (f n, n)) xs

- Haskell , , , . , - , , union addList ( , ), ( , ).

COST CENTRE   MODULE             SRC                                     %time %alloc

union         Data.Heap.Internal Data/Heap/Internal.hs:(134,1)-(141,79)   42.8   32.7
visited       Search.General     src/Search/General.hs:77:43-53           14.6    0.0
expand        Search.General     src/Search/General.hs:(70,40)-(76,78)    13.6   14.0
cost          Search.General     src/Search/General.hs:74:70-81            7.8    7.9
generalSearch Search.General     src/Search/General.hs:(52,1)-(58,37)      6.7   13.6
fromList      Data.Heap          Data/Heap.hs:137:1-34                     4.7   11.7
size          Data.Heap.Internal Data/Heap/Internal.hs:(98,1)-(99,23)      3.4    8.3
rank          Data.Heap.Internal Data/Heap/Internal.hs:(93,1)-(94,23)      2.7    8.0
expand        Search.General     src/Search/General.hs:(69,1)-(77,53)      1.4    3.4

, , , , . ? ?

+6

All Articles