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/ItemProcessors.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/ItemProcessors.hs')
-rw-r--r-- | compiler/src/ItemProcessors.hs | 85 |
1 files changed, 34 insertions, 51 deletions
diff --git a/compiler/src/ItemProcessors.hs b/compiler/src/ItemProcessors.hs index 209bc2a..f967954 100644 --- a/compiler/src/ItemProcessors.hs +++ b/compiler/src/ItemProcessors.hs | |||
@@ -19,17 +19,15 @@ | |||
19 | module ItemProcessors | 19 | module ItemProcessors |
20 | ( ItemProcessor | 20 | ( ItemProcessor |
21 | , itemFileProcessor | 21 | , itemFileProcessor |
22 | , ThumbnailProcessor | ||
23 | , thumbnailFileProcessor | 22 | , thumbnailFileProcessor |
24 | ) where | 23 | ) where |
25 | 24 | ||
26 | 25 | ||
27 | import Data.Function ((&)) | ||
28 | import Data.Char (toLower) | 26 | import Data.Char (toLower) |
29 | import System.FilePath (takeExtension) | 27 | import System.FilePath (takeExtension) |
30 | 28 | ||
31 | import Config (Resolution(..)) | 29 | import Config (Resolution(..)) |
32 | import Resource (ItemProcessor, ThumbnailProcessor, Thumbnail(..), GalleryItemProps(..)) | 30 | import Resource (ItemProcessor, Thumbnail(..), GalleryItemProps(..)) |
33 | import Caching (Cache) | 31 | import Caching (Cache) |
34 | import FileProcessors | 32 | import FileProcessors |
35 | import Files | 33 | import Files |
@@ -75,58 +73,43 @@ formatFromPath = | |||
75 | _ -> Unknown | 73 | _ -> Unknown |
76 | 74 | ||
77 | 75 | ||
78 | type ItemFileProcessor = | 76 | type ItemFileProcessor a = |
79 | FileName -- ^ Input base path | 77 | FilePath -- ^ Filesystem input base path |
80 | -> FileName -- ^ Output base path | 78 | -> FilePath -- ^ Filesystem output base path |
81 | -> FileName -- ^ Output class (subdir) | 79 | -> FileName -- ^ Output class (subdir) |
82 | -> ItemProcessor | 80 | -> ItemProcessor a |
83 | 81 | ||
84 | itemFileProcessor :: Maybe Resolution -> Cache -> ItemFileProcessor | ||
85 | itemFileProcessor maxResolution cached inputBase outputBase resClass inputRes = | ||
86 | cached processor inPath outPath | ||
87 | >> resourceAt outPath relOutPath | ||
88 | >>= descriptor outPath | ||
89 | where | ||
90 | relOutPath = resClass /> inputRes | ||
91 | inPath = localPath $ inputBase /> inputRes | ||
92 | outPath = localPath $ outputBase /> relOutPath | ||
93 | (processor, descriptor) = processorFor (formatFromPath inputRes) maxResolution | ||
94 | |||
95 | processorFor :: Format -> Maybe Resolution -> (FileProcessor, ItemDescriber) | ||
96 | processorFor PictureFormat (Just maxRes) = (resizePictureUpTo maxRes, getPictureProps) | ||
97 | processorFor PictureFormat Nothing = (copyFileProcessor, getPictureProps) | ||
98 | processorFor PlainTextFormat _ = (copyFileProcessor, const $ return . PlainText) | ||
99 | processorFor PortableDocumentFormat _ = (copyFileProcessor, const $ return . PDF) | ||
100 | processorFor VideoFormat _ = (copyFileProcessor, const $ return . Video) | ||
101 | processorFor AudioFormat _ = (copyFileProcessor, const $ return . Audio) | ||
102 | -- TODO: handle video reencoding and others? | ||
103 | processorFor Unknown _ = (copyFileProcessor, const $ return . Other) | ||
104 | 82 | ||
83 | callFileProcessor :: (Path -> FileProcessor a) -> Cache a -> ItemFileProcessor a | ||
84 | callFileProcessor processorProvider withCache inputBase outputBase resClass itemPath resPath = | ||
85 | withCache (processorProvider resPath) | ||
86 | itemPath | ||
87 | (resClass /> resPath) | ||
88 | (localPath $ inputBase /> resPath) | ||
89 | (localPath $ outputBase /> (resClass /> resPath)) | ||
105 | 90 | ||
106 | type ThumbnailFileProcessor = | ||
107 | FileName -- ^ Input base path | ||
108 | -> FileName -- ^ Output base path | ||
109 | -> FileName -- ^ Output class (subdir) | ||
110 | -> ThumbnailProcessor | ||
111 | 91 | ||
112 | thumbnailFileProcessor :: Resolution -> Cache -> ThumbnailFileProcessor | 92 | itemFileProcessor :: Maybe Resolution -> Cache GalleryItemProps -> ItemFileProcessor GalleryItemProps |
113 | thumbnailFileProcessor maxRes cached inputBase outputBase resClass inputRes = | 93 | itemFileProcessor maxResolution = |
114 | cached <$> processorFor (formatFromPath inputRes) | 94 | callFileProcessor (flip processorFor maxResolution . formatFromPath) |
115 | & process | ||
116 | where | 95 | where |
117 | relOutPath = resClass /> inputRes | 96 | processorFor :: Format -> Maybe Resolution -> FileProcessor GalleryItemProps |
118 | inPath = localPath $ inputBase /> inputRes | 97 | processorFor PictureFormat (Just maxRes) = |
119 | outPath = localPath $ outputBase /> relOutPath | 98 | transformThenDescribe (resizePictureUpTo maxRes) getPictureProps |
99 | processorFor PictureFormat Nothing = | ||
100 | transformThenDescribe copyFileProcessor getPictureProps | ||
101 | processorFor PlainTextFormat _ = copyResource PlainText | ||
102 | processorFor PortableDocumentFormat _ = copyResource PDF | ||
103 | processorFor VideoFormat _ = copyResource Video | ||
104 | processorFor AudioFormat _ = copyResource Audio | ||
105 | processorFor Unknown _ = copyResource Other | ||
106 | -- TODO: handle video reencoding and others? | ||
120 | 107 | ||
121 | process :: Maybe FileProcessor -> IO (Maybe Thumbnail) | ||
122 | process Nothing = return Nothing | ||
123 | process (Just proc) = | ||
124 | do | ||
125 | proc inPath outPath | ||
126 | resource <- resourceAt outPath relOutPath | ||
127 | resolution <- getImageResolution outPath | ||
128 | return $ Just $ Thumbnail resource resolution | ||
129 | 108 | ||
130 | processorFor :: Format -> Maybe FileProcessor | 109 | thumbnailFileProcessor :: Resolution -> Cache (Maybe Thumbnail) -> ItemFileProcessor (Maybe Thumbnail) |
131 | processorFor PictureFormat = Just $ resizePictureUpTo maxRes | 110 | thumbnailFileProcessor maxRes = |
132 | processorFor _ = Nothing | 111 | callFileProcessor (processorFor . formatFromPath) |
112 | where | ||
113 | processorFor :: Format -> FileProcessor (Maybe Thumbnail) | ||
114 | processorFor PictureFormat = transformThenDescribe (resizePictureUpTo maxRes) getThumbnailProps | ||
115 | processorFor _ = noopProcessor | ||