hash frames (#157)

* cache frames via hashing

* use reanimate-svg-0.10.3.0

* NFC: whitespace

* Add hashable-1.3.0.0 to extra deps in lts-13,14

* allow-newer in lts-13

Co-authored-by: David Himmelstrup <lemmih@gmail.com>
This commit is contained in:
shaurya gupta 2020-09-08 16:28:32 +05:30 committed by GitHub
commit e9f893b6e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -118,7 +118,7 @@ library
fsnotify >=0.3.0.1,
geojson >=3.0.4,
ghcid >=0.7,
hashable >=1.2.7.0,
hashable >=1.3.0.0,
hgeometry >=0.11.0.0,
hgeometry-combinatorial >=0.11.0.0,
lens >=4.16.1,
@ -137,6 +137,7 @@ library
temporary >=1.3,
text >=1.2.3.0,
time >=1.8.0.0,
unordered-containers,
vector >=0.12.0.0,
vector-space >=0.13,
websockets >=0.12.7.0,

View file

@ -28,6 +28,7 @@ import Control.Exception
import Control.Monad (forM_, forever, unless, void, when)
import Data.Either
import Data.Function
import qualified Data.HashMap.Strict as M
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Time
@ -63,15 +64,20 @@ renderSvgs :: FilePath -> Int -> Bool -> Animation -> IO ()
renderSvgs folder offset _prettyPrint ani = do
print frameCount
lock <- newMVar ()
hashedFrames <- newMVar M.empty
handle errHandler $ concurrentForM_ (frameOrder rate frameCount) $ \nth' -> do
let nth = (nth'+offset) `mod` frameCount
now = (duration ani / (fromIntegral frameCount - 1)) * fromIntegral nth
frame = frameAt (if frameCount <= 1 then 0 else now) ani
svg = renderSvg Nothing Nothing frame
path = folder </> show nth <.> "svg"
idempotentFile path $ writeFile path svg
cachedFrame <- M.lookup frame <$> readMVar hashedFrames
idempotentFile path $
case cachedFrame of
Nothing -> do
let svg = renderSvg Nothing Nothing frame
writeFile path svg
modifyMVar_ hashedFrames $ \hm -> pure $ M.insert frame path hm
Just frameFile -> copyFile frameFile path
withMVar lock $ \_ -> do
print nth
hFlush stdout