aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/FileProcessors.hs
diff options
context:
space:
mode:
authorpacien2020-06-15 04:46:11 +0200
committerpacien2020-06-16 18:34:32 +0200
commit52abb806a3bde6eb69d64564d971efae2cbfda24 (patch)
tree3649f42ab8bccc348a68e67fbec97f6b4868ef5d /compiler/src/FileProcessors.hs
parent8905383e2d17e2adb4097e1ce2e7f90ab9ceb5f5 (diff)
downloadldgallery-52abb806a3bde6eb69d64564d971efae2cbfda24.tar.gz
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
Diffstat (limited to 'compiler/src/FileProcessors.hs')
-rw-r--r--compiler/src/FileProcessors.hs59
1 files changed, 46 insertions, 13 deletions
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 @@
18 18
19module FileProcessors 19module FileProcessors
20 ( FileProcessor 20 ( FileProcessor
21 , transformThenDescribe
22 , copyResource
23 , noopProcessor
24 , FileTransformer
21 , copyFileProcessor 25 , copyFileProcessor
22 , resizePictureUpTo 26 , resizePictureUpTo
23 , resourceAt 27 , resourceAt
24 , getImageResolution 28 , getImageResolution
25 , ItemDescriber 29 , FileDescriber
30 , getResProps
26 , getPictureProps 31 , getPictureProps
32 , getThumbnailProps
27 ) where 33 ) where
28 34
29 35
@@ -35,24 +41,43 @@ import System.Directory (getModificationTime)
35import qualified System.Directory 41import qualified System.Directory
36 42
37import Config (Resolution(..)) 43import Config (Resolution(..))
38import Resource (Resource(..), GalleryItemProps(..)) 44import Resource (Resource(..), GalleryItemProps(..), Thumbnail(..))
39import Files 45import Files
40 46
41 47
42data ProcessingException = ProcessingException FilePath String deriving Show 48data ProcessingException = ProcessingException FilePath String deriving Show
43instance Exception ProcessingException 49instance Exception ProcessingException
44 50
45type FileProcessor = 51type FileProcessor a =
52 Path -- ^ Item path
53 -> Path -- ^ Target resource path
54 -> FilePath -- ^ Filesystem input path
55 -> FilePath -- ^ Filesystem output path
56 -> IO a
57
58transformThenDescribe :: FileTransformer -> FileDescriber a -> FileProcessor a
59transformThenDescribe transformer describer _itemPath resPath fsInPath fsOutPath =
60 transformer fsInPath fsOutPath >> describer resPath fsOutPath
61
62copyResource :: (Resource -> a) -> FileProcessor a
63copyResource resPropConstructor =
64 transformThenDescribe copyFileProcessor (getResProps resPropConstructor)
65
66noopProcessor :: FileProcessor (Maybe a)
67noopProcessor _ _ _ _ = return Nothing
68
69
70type FileTransformer =
46 FileName -- ^ Input path 71 FileName -- ^ Input path
47 -> FileName -- ^ Output path 72 -> FileName -- ^ Output path
48 -> IO () 73 -> IO ()
49 74
50copyFileProcessor :: FileProcessor 75copyFileProcessor :: FileTransformer
51copyFileProcessor inputPath outputPath = 76copyFileProcessor inputPath outputPath =
52 putStrLn ("Copying:\t" ++ outputPath) 77 putStrLn ("Copying:\t" ++ outputPath)
53 >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath 78 >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath
54 79
55resizePictureUpTo :: Resolution -> FileProcessor 80resizePictureUpTo :: Resolution -> FileTransformer
56resizePictureUpTo maxResolution inputPath outputPath = 81resizePictureUpTo maxResolution inputPath outputPath =
57 putStrLn ("Generating:\t" ++ outputPath) 82 putStrLn ("Generating:\t" ++ outputPath)
58 >> ensureParentDir (flip resize) outputPath inputPath 83 >> ensureParentDir (flip resize) outputPath inputPath
@@ -68,8 +93,10 @@ resizePictureUpTo maxResolution inputPath outputPath =
68 , output ] 93 , output ]
69 94
70 95
71resourceAt :: FilePath -> Path -> IO Resource 96type FileDescriber a =
72resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath 97 Path -- ^ Target resource path
98 -> FilePath -- ^ Filesystem path
99 -> IO a
73 100
74getImageResolution :: FilePath -> IO Resolution 101getImageResolution :: FilePath -> IO Resolution
75getImageResolution fsPath = 102getImageResolution fsPath =
@@ -85,11 +112,17 @@ getImageResolution fsPath =
85 (Just w, Just h) -> return $ Resolution w h 112 (Just w, Just h) -> return $ Resolution w h
86 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution." 113 _ -> throwIO $ ProcessingException fsPath "Unable to read image resolution."
87 114
115resourceAt :: FileDescriber Resource
116resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath
117
118getResProps :: (Resource -> a) -> FileDescriber a
119getResProps resPropsConstructor resPath fsPath =
120 resPropsConstructor <$> resourceAt resPath fsPath
88 121
89type ItemDescriber = 122getPictureProps :: FileDescriber GalleryItemProps
90 FilePath 123getPictureProps resPath fsPath =
91 -> Resource 124 Picture <$> resourceAt resPath fsPath <*> getImageResolution fsPath
92 -> IO GalleryItemProps
93 125
94getPictureProps :: ItemDescriber 126getThumbnailProps :: FileDescriber (Maybe Thumbnail)
95getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath 127getThumbnailProps resPath fsPath =
128 Just <$> (Thumbnail <$> resourceAt resPath fsPath <*> getImageResolution fsPath)