Skip to content

Commit

Permalink
Performance optimization in DFS algorithms: remove extra traversals f…
Browse files Browse the repository at this point in the history
…or ~10us perf gain.
  • Loading branch information
kquick authored and travitch committed Mar 28, 2024
1 parent 98314ca commit 4313789
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Data/Graph/Haggle/Algorithms/DFS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Data.Graph.Haggle.Algorithms.DFS (
hasCycle
) where

import Control.Monad ( filterM, foldM, liftM )
import Control.Monad ( foldM )
import Control.Monad.ST
import qualified Data.Foldable as F
import Data.Monoid
Expand Down Expand Up @@ -81,11 +81,9 @@ xdfsWith g nextVerts f roots
True -> return acc
False -> do
setBitUnsafe bs (vertexId v)
nxt <- filterM (notVisited bs) (nextVerts v)
let nxt = nextVerts v
foldM (go bs) (f v : acc) nxt

notVisited :: BitSet s -> Vertex -> ST s Bool
notVisited bs v = liftM not (testBitUnsafe bs (vertexId v))

-- | Forward parameterized DFS
dfsWith :: (Graph g)
Expand Down Expand Up @@ -147,7 +145,7 @@ xdffWith g nextVerts f roots
True -> return acc
False -> do
setBitUnsafe bs (vertexId v)
nxt <- filterM (notVisited bs) (nextVerts v)
let nxt = nextVerts v
ts <- foldM (go bs) [] nxt
return $ T.Node (f v) (reverse ts) : acc

Expand Down

0 comments on commit 4313789

Please sign in to comment.