Skip to content

Commit

Permalink
⚡ Use strong memo table
Browse files Browse the repository at this point in the history
  • Loading branch information
lsrcz committed Sep 7, 2024
1 parent 064ce80 commit 20b5c53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Grisette/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,11 @@ module Grisette.Core
stableMemo3,
stableMup,
stableMemoFix,
weakStableMemo,
weakStableMemo2,
weakStableMemo3,
weakStableMup,
weakStableMemoFix,
htmemo,
htmemo2,
htmemo3,
Expand Down Expand Up @@ -1779,6 +1784,11 @@ import Grisette.Internal.Core.Data.MemoUtils
stableMemo3,
stableMemoFix,
stableMup,
weakStableMemo,
weakStableMemo2,
weakStableMemo3,
weakStableMemoFix,
weakStableMup,
)
import Grisette.Internal.Core.Data.Symbol
( Identifier (..),
Expand Down
44 changes: 34 additions & 10 deletions src/Grisette/Internal/Core/Data/MemoUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ module Grisette.Internal.Core.Data.MemoUtils
stableMemo3,
stableMup,
stableMemoFix,
weakStableMemo,
weakStableMemo2,
weakStableMemo3,
weakStableMup,
weakStableMemoFix,
htmemo,
htmemo2,
htmemo3,
Expand Down Expand Up @@ -56,20 +61,22 @@ class Ref ref where
mkRef :: a -> b -> IO () -> IO (ref b)
deRef :: ref a -> IO (Maybe a)
finalize :: ref a -> IO ()
tableFinalizer :: MemoTable ref f g -> IO ()
tableFinalizer = HashTable.mapM_ $ finalize . unO . snd

Check warning on line 65 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L65

Added line #L65 was not covered by tests

instance Ref Weak where
mkRef x y = Weak.mkWeak x y . Just
deRef = Weak.deRefWeak
finalize = Weak.finalize

Check warning on line 70 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L67-L70

Added lines #L67 - L70 were not covered by tests

data Strong a = Strong a !(Weak a)
newtype Strong a = Strong a

instance Ref Strong where
mkRef _ y final = do
weak <- Weak.mkWeakPtr y $ Just final
return $ Strong y weak
deRef (Strong x _) = return $ Just x
finalize (Strong _ weak) = Weak.finalize weak
mkRef _ y _ = do
return $ Strong y
deRef (Strong x) = return $ Just x
finalize (Strong _) = return ()

Check warning on line 78 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L77-L78

Added lines #L77 - L78 were not covered by tests
tableFinalizer _ = return ()

finalizer ::
StableName (f Any) -> RLock.RLock -> Weak (MemoTable ref f g) -> IO ()
Expand Down Expand Up @@ -119,9 +126,6 @@ memo' _ f tbl lock weakTbl !x = unsafePerformIO $ do
RLock.release lock
return y

tableFinalizer :: (Ref ref) => MemoTable ref f g -> IO ()
tableFinalizer = HashTable.mapM_ $ finalize . unO . snd

{-# NOINLINE memo0 #-}
memo0 ::
(Ref ref) =>
Expand All @@ -139,7 +143,7 @@ memo0 p f =

-- | Memoize a unary function.
stableMemo :: (a -> b) -> (a -> b)
stableMemo f = getConst . memo0 (Proxy :: Proxy Weak) (Const . f . getConst) . Const
stableMemo f = getConst . memo0 (Proxy :: Proxy Strong) (Const . f . getConst) . Const

-- | Lift a memoizer to work with one more argument.
stableMup :: (b -> c) -> (a -> b) -> (a -> c)
Expand All @@ -157,6 +161,26 @@ stableMemo3 = stableMup stableMemo2
stableMemoFix :: ((a -> b) -> (a -> b)) -> a -> b
stableMemoFix h = fix (stableMemo . h)

Check warning on line 162 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L162

Added line #L162 was not covered by tests

-- | Memoize a unary function.
weakStableMemo :: (a -> b) -> (a -> b)
weakStableMemo f = getConst . memo0 (Proxy :: Proxy Weak) (Const . f . getConst) . Const

Check warning on line 166 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L166

Added line #L166 was not covered by tests

-- | Lift a memoizer to work with one more argument.
weakStableMup :: (b -> c) -> (a -> b) -> (a -> c)
weakStableMup mem f = weakStableMemo (mem . f)

Check warning on line 170 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L170

Added line #L170 was not covered by tests

-- | Curried memoization to share partial evaluation
weakStableMemo2 :: (a -> b -> c) -> (a -> b -> c)
weakStableMemo2 = weakStableMup weakStableMemo

Check warning on line 174 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L174

Added line #L174 was not covered by tests

-- | Curried memoization to share partial evaluation
weakStableMemo3 :: (a -> b -> c -> d) -> (a -> b -> c -> d)
weakStableMemo3 = weakStableMup weakStableMemo2

Check warning on line 178 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L178

Added line #L178 was not covered by tests

-- | Memoizing recursion. Use like 'fix'.
weakStableMemoFix :: ((a -> b) -> (a -> b)) -> a -> b
weakStableMemoFix h = fix (weakStableMemo . h)

Check warning on line 182 in src/Grisette/Internal/Core/Data/MemoUtils.hs

View check run for this annotation

Codecov / codecov/patch

src/Grisette/Internal/Core/Data/MemoUtils.hs#L182

Added line #L182 was not covered by tests

type HashTable k v = H.BasicHashTable k v

-- | Function memoizer with mutable hash table.
Expand Down

0 comments on commit 20b5c53

Please sign in to comment.