fix(xanthous): Make floodFill faster
Speed up the floodFill algorithm by sprinkling in some strictness and specializing it to the only type it's currently called at anyway. Change-Id: I4557fc51b1c1036c127bfd5bee50748d8692ae74 Reviewed-on: https://cl.tvl.fyi/c/depot/+/555 Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
		
							parent
							
								
									fbbb284444
								
							
						
					
					
						commit
						0367e8c303
					
				
					 1 changed files with 4 additions and 1 deletions
				
			
		| 
						 | 
					@ -163,9 +163,11 @@ floodFill = go mempty
 | 
				
			||||||
                   (if y == maxBound then y else succ y)]
 | 
					                   (if y == maxBound then y else succ y)]
 | 
				
			||||||
        in foldl' (\r idx' ->
 | 
					        in foldl' (\r idx' ->
 | 
				
			||||||
                     if arr ! idx'
 | 
					                     if arr ! idx'
 | 
				
			||||||
                     then r <> go (r & contains idx' .~ True) arr idx'
 | 
					                     then r <> (let r' = r & contains idx' .~ True
 | 
				
			||||||
 | 
					                               in r' `seq` go r' arr idx')
 | 
				
			||||||
                     else r)
 | 
					                     else r)
 | 
				
			||||||
           (res & contains idx .~ True) neighbors
 | 
					           (res & contains idx .~ True) neighbors
 | 
				
			||||||
 | 
					{-# SPECIALIZE floodFill :: UArray (Word, Word) Bool -> (Word, Word) -> Set (Word, Word) #-}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- | Gives a list of all the disconnected regions in a cell array, represented
 | 
					-- | Gives a list of all the disconnected regions in a cell array, represented
 | 
				
			||||||
-- each as lists of points
 | 
					-- each as lists of points
 | 
				
			||||||
| 
						 | 
					@ -188,6 +190,7 @@ regions arr
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
    findFirstPoint :: a (i, j) Bool -> Maybe (i, j)
 | 
					    findFirstPoint :: a (i, j) Bool -> Maybe (i, j)
 | 
				
			||||||
    findFirstPoint = fmap fst . headMay . filter snd . assocs
 | 
					    findFirstPoint = fmap fst . headMay . filter snd . assocs
 | 
				
			||||||
 | 
					{-# SPECIALIZE regions :: UArray (Word, Word) Bool -> [Set (Word, Word)] #-}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fillAll :: (IArray a Bool, Ix i, Foldable f) => f i -> a i Bool -> a i Bool
 | 
					fillAll :: (IArray a Bool, Ix i, Foldable f) => f i -> a i Bool -> a i Bool
 | 
				
			||||||
fillAll ixes a = accum (const fst) a $ (, (False, ())) <$> toList ixes
 | 
					fillAll ixes a = accum (const fst) a $ (, (False, ())) <$> toList ixes
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue