From: Evgenii Akentev Date: Tue, 17 Sep 2024 16:57:32 +0000 (+0400) Subject: TwoFinger wip X-Git-Url: https://git.ak3n.com/?a=commitdiff_plain;h=9aea392bfbcb58251d540bc42480f292c4f09d3b;p=gcs.git TwoFinger wip --- diff --git a/src/MarkCompact/TwoFinger.hs b/src/MarkCompact/TwoFinger.hs index 1cc174c..9667e13 100644 --- a/src/MarkCompact/TwoFinger.hs +++ b/src/MarkCompact/TwoFinger.hs @@ -5,7 +5,8 @@ module MarkCompact.TwoFinger where import Debug.Trace -import Data.List (sortBy) +import Data.Ord (Down(..)) +import Data.List import Data.Function (on) import Data.Array import Control.Monad.State @@ -112,9 +113,7 @@ allocate v = do pure $ Right $ ObjectPtr p Nothing -> pure $ Left "Out of memory" where - findIndexWithNothing h = foldl step Nothing $ sortBy (flip compare `on` fst) $ assocs h - step _ (i, Nothing) = Just i - step acc _ = acc + findIndexWithNothing h = ((+)1) <$> (elemIndex Nothing $ map snd $ sortBy (flip compare `on` Down . fst) $ assocs h) mark :: [ObjectPtr] -> Heap -> Bitmap -> Bitmap mark [] _ bm = bm @@ -146,7 +145,6 @@ markFromRoots = do collect :: GcM () collect = do - traceShowM "mark!!!" markFromRoots compact @@ -170,17 +168,17 @@ relocate start end = go start end | free < scan = do gc <- get let bm = bitmap gc - let indicesToUnmark = fmap fst $ takeWhile (\(_, marked) -> marked) $ assocs bm + let indicesToUnmark = fmap fst $ takeWhile (\(_, marked) -> marked) $ sortBy (flip compare `on` Down . fst) $ assocs bm free' = if indicesToUnmark == [] then free else maximum indicesToUnmark bm' = bm // [(i, False) | i <- indicesToUnmark] - put $ gc { bitmap = traceShow (free', indicesToUnmark) bm' } + put $ gc { bitmap = traceShow (free', indicesToUnmark) bm', heap = heap gc // [(i, Nothing) | (i, x) <- assocs bm, x == False] } let - findIndexStep marked (ok, idx) - | not marked && idx > free' = (ok, idx - 1) - | otherwise = (True, idx) - scan' = snd $ foldr findIndexStep (False, scan) bm' - + findIndexStep (_, marked) idx + | not marked && idx > free' = idx - 1 + | otherwise = idx + scan' = traceShowId $ foldr findIndexStep scan $ traceShowId $ take scan $ sortBy (flip compare `on` Down . fst) $ assocs bm' + if scan' > free' then do put $ gc { bitmap = bm' // [(scan', False)]} @@ -230,9 +228,12 @@ main = do ptr7 <- new (IntVal 7) ptr8 <- new (IntVal 8) + writeToRoots 5 ptr6 collect - - return [ptr, ptr2] + ptr2' <- new (IntVal 2) + collect + + return [ptr, ptr2, ptr2'] print res