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/FileProcessors.hs | 59 ++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'compiler/src/FileProcessors.hs') diff --git a/compiler/src/FileProcessors.hs b/compiler/src/FileProcessors.hs index 8ea04d1..5c4e1c8 100644 --- a/compiler/src/FileProcessors.hs +++ b/compiler/src/FileProcessors.hs @@ -18,12 +18,18 @@ module FileProcessors ( FileProcessor + , transformThenDescribe + , copyResource + , noopProcessor + , FileTransformer , copyFileProcessor , resizePictureUpTo , resourceAt , getImageResolution - , ItemDescriber + , FileDescriber + , getResProps , getPictureProps + , getThumbnailProps ) where @@ -35,24 +41,43 @@ import System.Directory (getModificationTime) import qualified System.Directory import Config (Resolution(..)) -import Resource (Resource(..), GalleryItemProps(..)) +import Resource (Resource(..), GalleryItemProps(..), Thumbnail(..)) import Files data ProcessingException = ProcessingException FilePath String deriving Show instance Exception ProcessingException -type FileProcessor = +type FileProcessor a = + Path -- ^ Item path + -> Path -- ^ Target resource path + -> FilePath -- ^ Filesystem input path + -> FilePath -- ^ Filesystem output path + -> IO a + +transformThenDescribe :: FileTransformer -> FileDescriber a -> FileProcessor a +transformThenDescribe transformer describer _itemPath resPath fsInPath fsOutPath = + transformer fsInPath fsOutPath >> describer resPath fsOutPath + +copyResource :: (Resource -> a) -> FileProcessor a +copyResource resPropConstructor = + transformThenDescribe copyFileProcessor (getResProps resPropConstructor) + +noopProcessor :: FileProcessor (Maybe a) +noopProcessor _ _ _ _ = return Nothing + + +type FileTransformer = FileName -- ^ Input path -> FileName -- ^ Output path -> IO () -copyFileProcessor :: FileProcessor +copyFileProcessor :: FileTransformer copyFileProcessor inputPath outputPath = putStrLn ("Copying:\t" ++ outputPath) >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath -resizePictureUpTo :: Resolution -> FileProcessor +resizePictureUpTo :: Resolution -> FileTransformer resizePictureUpTo maxResolution inputPath outputPath = putStrLn ("Generating:\t" ++ outputPath) >> ensureParentDir (flip resize) outputPath inputPath @@ -68,8 +93,10 @@ resizePictureUpTo maxResolution inputPath outputPath = , output ] -resourceAt :: FilePath -> Path -> IO Resource -resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath +type FileDescriber a = + Path -- ^ Target resource path + -> FilePath -- ^ Filesystem path + -> IO a getImageResolution :: FilePath -> IO Resolution getImageResolution fsPath = @@ -85,11 +112,17 @@ getImageResolution fsPath = (Just w, Just h) -> return $ Resolution w h _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution." +resourceAt :: FileDescriber Resource +resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath + +getResProps :: (Resource -> a) -> FileDescriber a +getResProps resPropsConstructor resPath fsPath = + resPropsConstructor <$> resourceAt resPath fsPath -type ItemDescriber = - FilePath - -> Resource - -> IO GalleryItemProps +getPictureProps :: FileDescriber GalleryItemProps +getPictureProps resPath fsPath = + Picture <$> resourceAt resPath fsPath <*> getImageResolution fsPath -getPictureProps :: ItemDescriber -getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath +getThumbnailProps :: FileDescriber (Maybe Thumbnail) +getThumbnailProps resPath fsPath = + Just <$> (Thumbnail <$> resourceAt resPath fsPath <*> getImageResolution fsPath) -- cgit v1.2.3