Speedup of factor 4 by switching to the Data.Binary library for serialization.

This commit is contained in:
Jasper Van der Jeugt 2010-01-20 22:08:15 +01:00
parent 98d712fc3a
commit cf789c7ac6
3 changed files with 15 additions and 9 deletions

View file

@ -34,7 +34,8 @@ library
mtl >= 1.1, mtl >= 1.1,
old-locale >= 1, old-locale >= 1,
time >= 1, time >= 1,
parallel >= 2 parallel >= 2,
binary >= 0.5
exposed-modules: Text.Hakyll exposed-modules: Text.Hakyll
Text.Hakyll.Hakyll Text.Hakyll.Hakyll
Text.Hakyll.Render Text.Hakyll.Render

View file

@ -6,19 +6,17 @@ module Text.Hakyll.Internal.Cache
import Control.Monad.Reader (liftIO) import Control.Monad.Reader (liftIO)
import Text.Hakyll.Hakyll (Hakyll) import Text.Hakyll.Hakyll (Hakyll)
import Text.Hakyll.File import Text.Hakyll.File
import Data.Binary
storeInCache :: (Show a) => a -> FilePath -> Hakyll () storeInCache :: (Binary a) => a -> FilePath -> Hakyll ()
storeInCache value path = do storeInCache value path = do
cachePath <- toCache path cachePath <- toCache path
makeDirectories cachePath makeDirectories cachePath
liftIO $ writeFile cachePath (show value) liftIO $ encodeFile cachePath value
getFromCache :: (Read a) => FilePath -> Hakyll (Maybe a) getFromCache :: (Binary a) => FilePath -> Hakyll (Maybe a)
getFromCache path = do getFromCache path = do
cachePath <- toCache path cachePath <- toCache path
valid <- isMoreRecent cachePath [path] valid <- isMoreRecent cachePath [path]
if valid then liftIO (getFromCache' cachePath) >>= return . Just if valid then liftIO (decodeFile cachePath) >>= return . Just
else return Nothing else return Nothing
where
getFromCache' cachePath = do c <- readFile cachePath
return (read c)

View file

@ -11,12 +11,14 @@ import qualified Data.Map as M
import Data.List (isPrefixOf) import Data.List (isPrefixOf)
import Data.Char (isSpace) import Data.Char (isSpace)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Control.Parallel.Strategies (rdeepseq, ($|)) import Control.Monad (liftM)
import Control.Monad.Reader (liftIO) import Control.Monad.Reader (liftIO)
import Control.Parallel.Strategies (rdeepseq, ($|))
import System.FilePath (takeExtension) import System.FilePath (takeExtension)
import System.IO import System.IO
import Text.Pandoc import Text.Pandoc
import Data.Binary
import Text.Hakyll.Internal.Cache import Text.Hakyll.Internal.Cache
import Text.Hakyll.Hakyll (Hakyll) import Text.Hakyll.Hakyll (Hakyll)
@ -148,3 +150,8 @@ instance Renderable Page where
getDependencies = (:[]) . getPagePath getDependencies = (:[]) . getPagePath
getURL = getPageURL getURL = getPageURL
toContext (Page page) = return page toContext (Page page) = return page
-- Make pages serializable.
instance Binary Page where
put (Page context) = put $ M.toList context
get = liftM (Page . M.fromList) get