module Graphics.SvgTree.Memo
( memo
, preRender
) where
import Control.Lens
import Data.IORef
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe
import Data.Typeable
import Graphics.SvgTree.Printer
import Graphics.SvgTree.Types (Tree, preRendered)
import System.IO.Unsafe
{-# NOINLINE intCache #-}
intCache :: IORef (Map Int Tree)
intCache = unsafePerformIO (newIORef Map.empty)
{-# NOINLINE doubleCache #-}
doubleCache :: IORef (Map Double Tree)
doubleCache = unsafePerformIO (newIORef Map.empty)
{-# NOINLINE anyCache #-}
anyCache :: IORef (Map (TypeRep,String) Tree)
anyCache = unsafePerformIO (newIORef Map.empty)
memo :: (Typeable a, Show a) => (a -> Tree) -> (a -> Tree)
memo fn =
case listToMaybe (catMaybes caches) of
Just ret -> ret
Nothing -> memoAny fn
where
caches = [try intCache, try doubleCache]
try cache = cast . memoUsing cache =<< cast fn
memoUsing :: Ord a => IORef (Map a Tree) -> (a -> Tree) -> (a -> Tree)
memoUsing cache fn a = unsafePerformIO $
atomicModifyIORef cache $ \m ->
let newVal = preRender $ fn a
notFound =
(Map.insert a newVal m, newVal) in
case Map.lookup a m of
Nothing -> notFound
Just t -> (m, t)
memoAny :: (Typeable a, Show a) => (a -> Tree) -> (a -> Tree)
memoAny fn a = unsafePerformIO $
atomicModifyIORef anyCache $ \m ->
let newVal = preRender $ fn a
notFound =
(Map.insert (typeOf a, show a) newVal m, newVal) in
case Map.lookup (typeOf a, show a) m of
Nothing -> notFound
Just t -> (m, t)
preRender :: Tree -> Tree
preRender t = t & preRendered .~ Just (ppTree t)