From 52abb806a3bde6eb69d64564d971efae2cbfda24 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 15 Jun 2020 04:46:11 +0200 Subject: compiler: reuse derived item properties from last compilation A benchmark on an already bulit gallery with ~600 pictures shows a ~90% speedup: Before: Time (mean ± σ): 2.879 s ± 0.125 s [User: 14.686 s, System: 5.511 s] Range (min … max): 2.774 s … 3.203 s 10 runs After: Time (mean ± σ): 289.5 ms ± 15.1 ms [User: 596.1 ms, System: 359.3 ms] Range (min … max): 272.8 ms … 323.0 ms 10 runs GitHub: closes #97 --- compiler/src/Caching.hs | 52 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'compiler/src/Caching.hs') diff --git a/compiler/src/Caching.hs b/compiler/src/Caching.hs index b2b1ee1..c2b5a43 100644 --- a/compiler/src/Caching.hs +++ b/compiler/src/Caching.hs @@ -18,39 +18,59 @@ module Caching ( Cache - , skipCache - , withCache + , noCache + , ItemCache + , buildItemCache + , useCached ) where import Control.Monad (when) +import qualified Data.Map.Strict as Map import System.Directory (removePathForcibly, doesDirectoryExist, doesFileExist) import FileProcessors (FileProcessor) +import Resource (GalleryItem(..), flattenGalleryTree) import Files -type Cache = FileProcessor -> FileProcessor +type Cache a = FileProcessor a -> FileProcessor a -skipCache :: Cache -skipCache processor inputPath outputPath = - removePathForcibly outputPath - >> processor inputPath outputPath -withCache :: Cache -withCache processor inputPath outputPath = +noCache :: Cache a +noCache processor itemPath resPath inputFsPath outputFsPath = + removePathForcibly outputFsPath + >> processor itemPath resPath inputFsPath outputFsPath + + +type ItemCache = Path -> Maybe GalleryItem + +buildItemCache :: Maybe GalleryItem -> ItemCache +buildItemCache cachedItems = lookupCache + where + withKey item = (webPath $ Resource.path item, item) + cachedItemList = maybe [] flattenGalleryTree cachedItems + cachedMap = Map.fromList (map withKey cachedItemList) + lookupCache path = Map.lookup (webPath path) cachedMap + +useCached :: ItemCache -> (GalleryItem -> a) -> Cache a +useCached cache propGetter processor itemPath resPath inputFsPath outputFsPath = do - isDir <- doesDirectoryExist outputPath - when isDir $ removePathForcibly outputPath + isDir <- doesDirectoryExist outputFsPath + when isDir $ removePathForcibly outputFsPath - fileExists <- doesFileExist outputPath + fileExists <- doesFileExist outputFsPath if fileExists then do - needUpdate <- isOutdated True inputPath outputPath - if needUpdate then update else skip + needUpdate <- isOutdated True inputFsPath outputFsPath + case (needUpdate, cache itemPath) of + (False, Just props) -> fromCache props + _ -> update else update where - update = processor inputPath outputPath - skip = putStrLn $ "Skipping:\t" ++ outputPath + update = processor itemPath resPath inputFsPath outputFsPath + fromCache props = + putStrLn ("From cache:\t" ++ outputFsPath) + >> return (propGetter props) -- cgit v1.2.3