diff options
author | pacien | 2020-06-15 04:46:11 +0200 |
---|---|---|
committer | pacien | 2020-06-16 18:34:32 +0200 |
commit | 52abb806a3bde6eb69d64564d971efae2cbfda24 (patch) | |
tree | 3649f42ab8bccc348a68e67fbec97f6b4868ef5d /compiler/src/FileProcessors.hs | |
parent | 8905383e2d17e2adb4097e1ce2e7f90ab9ceb5f5 (diff) | |
download | ldgallery-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.hs | 59 |
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 | ||
19 | module FileProcessors | 19 | module 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) | |||
35 | import qualified System.Directory | 41 | import qualified System.Directory |
36 | 42 | ||
37 | import Config (Resolution(..)) | 43 | import Config (Resolution(..)) |
38 | import Resource (Resource(..), GalleryItemProps(..)) | 44 | import Resource (Resource(..), GalleryItemProps(..), Thumbnail(..)) |
39 | import Files | 45 | import Files |
40 | 46 | ||
41 | 47 | ||
42 | data ProcessingException = ProcessingException FilePath String deriving Show | 48 | data ProcessingException = ProcessingException FilePath String deriving Show |
43 | instance Exception ProcessingException | 49 | instance Exception ProcessingException |
44 | 50 | ||
45 | type FileProcessor = | 51 | type 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 | |||
58 | transformThenDescribe :: FileTransformer -> FileDescriber a -> FileProcessor a | ||
59 | transformThenDescribe transformer describer _itemPath resPath fsInPath fsOutPath = | ||
60 | transformer fsInPath fsOutPath >> describer resPath fsOutPath | ||
61 | |||
62 | copyResource :: (Resource -> a) -> FileProcessor a | ||
63 | copyResource resPropConstructor = | ||
64 | transformThenDescribe copyFileProcessor (getResProps resPropConstructor) | ||
65 | |||
66 | noopProcessor :: FileProcessor (Maybe a) | ||
67 | noopProcessor _ _ _ _ = return Nothing | ||
68 | |||
69 | |||
70 | type FileTransformer = | ||
46 | FileName -- ^ Input path | 71 | FileName -- ^ Input path |
47 | -> FileName -- ^ Output path | 72 | -> FileName -- ^ Output path |
48 | -> IO () | 73 | -> IO () |
49 | 74 | ||
50 | copyFileProcessor :: FileProcessor | 75 | copyFileProcessor :: FileTransformer |
51 | copyFileProcessor inputPath outputPath = | 76 | copyFileProcessor 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 | ||
55 | resizePictureUpTo :: Resolution -> FileProcessor | 80 | resizePictureUpTo :: Resolution -> FileTransformer |
56 | resizePictureUpTo maxResolution inputPath outputPath = | 81 | resizePictureUpTo 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 | ||
71 | resourceAt :: FilePath -> Path -> IO Resource | 96 | type FileDescriber a = |
72 | resourceAt fsPath resPath = Resource resPath <$> getModificationTime fsPath | 97 | Path -- ^ Target resource path |
98 | -> FilePath -- ^ Filesystem path | ||
99 | -> IO a | ||
73 | 100 | ||
74 | getImageResolution :: FilePath -> IO Resolution | 101 | getImageResolution :: FilePath -> IO Resolution |
75 | getImageResolution fsPath = | 102 | getImageResolution 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 | ||
115 | resourceAt :: FileDescriber Resource | ||
116 | resourceAt resPath fsPath = Resource resPath <$> getModificationTime fsPath | ||
117 | |||
118 | getResProps :: (Resource -> a) -> FileDescriber a | ||
119 | getResProps resPropsConstructor resPath fsPath = | ||
120 | resPropsConstructor <$> resourceAt resPath fsPath | ||
88 | 121 | ||
89 | type ItemDescriber = | 122 | getPictureProps :: FileDescriber GalleryItemProps |
90 | FilePath | 123 | getPictureProps resPath fsPath = |
91 | -> Resource | 124 | Picture <$> resourceAt resPath fsPath <*> getImageResolution fsPath |
92 | -> IO GalleryItemProps | ||
93 | 125 | ||
94 | getPictureProps :: ItemDescriber | 126 | getThumbnailProps :: FileDescriber (Maybe Thumbnail) |
95 | getPictureProps fsPath resource = Picture resource <$> getImageResolution fsPath | 127 | getThumbnailProps resPath fsPath = |
128 | Just <$> (Thumbnail <$> resourceAt resPath fsPath <*> getImageResolution fsPath) | ||