Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/dump-decls-lib/bench/Main.hs b/dump-decls-lib/bench/Main.hs
- index b8e6e03..6066ac4 100644
- --- a/dump-decls-lib/bench/Main.hs
- +++ b/dump-decls-lib/bench/Main.hs
- @@ -9,6 +9,7 @@ import Versioned
- import Json.Version3 ()
- import Json.Version4 ()
- import Json.Version5 ()
- +import Json.Version6 ()
- import Criterion.Main
- import Data.Functor (void, (<&>))
- import Control.Monad ((<=<))
- @@ -22,6 +23,8 @@ import qualified Data.ByteString.Lazy as BSL
- import Data.Maybe (fromMaybe)
- import qualified Data.FileEmbed
- +import Codec.Serialise
- +
- -- TODO: include in this package
- dataFileNameV3 :: FilePath
- @@ -36,6 +39,10 @@ dataFileNameV5 :: FilePath
- dataFileNameV5 =
- "dump-decls-lib/bench/data/all-v5.json"
- +dataFileNameV6 :: FilePath
- +dataFileNameV6 =
- + "dump-decls-lib/bench/data/all-v6.bin"
- +
- fileReadDeclarationMapReadFile
- :: A.FromJSON (Versioned version [Json.DeclarationMapJson T.Text]) => FilePath
- -> IO (Versioned version [Json.DeclarationMapJson T.Text])
- @@ -56,6 +63,9 @@ fileReadDeclarationMapDecodeFileStrict fileName =
- fail
- pure
- +fileReadBinary :: Serialise (Versioned version [Json.DeclarationMapJson T.Text]) => FilePath -> IO (Versioned version [Json.DeclarationMapJson T.Text])
- +fileReadBinary = readFileDeserialise
- +
- main :: IO ()
- main = do
- defaultMain
- @@ -70,5 +80,9 @@ main = do
- , bench "v4" $ nfIO (fileReadDeclarationMapDecodeFileStrict dataFileNameV4 :: IO (Versioned 4 [Json.DeclarationMapJson T.Text]))
- , bench "v5" $ nfIO (fileReadDeclarationMapDecodeFileStrict dataFileNameV5 :: IO (Versioned 5 [Json.DeclarationMapJson T.Text]))
- ]
- + , bgroup "decodeFileBinary"
- + [
- + bench "v6" $ nfIO (fileReadBinary dataFileNameV6 :: IO (Versioned 6 [Json.DeclarationMapJson T.Text]))
- + ]
- ]
- ]
- diff --git a/dump-decls-lib/dump-decls-lib.cabal b/dump-decls-lib/dump-decls-lib.cabal
- index fa53ba7..fe0ae80 100644
- --- a/dump-decls-lib/dump-decls-lib.cabal
- +++ b/dump-decls-lib/dump-decls-lib.cabal
- @@ -17,6 +17,7 @@ library
- , Json.Version3
- , Json.Version4
- , Json.Version5
- + , Json.Version6
- , Types
- , Compat
- , Compat.Aeson
- @@ -32,6 +33,7 @@ library
- , deepseq
- , utf8-string
- , vector
- + , serialise
- hs-source-dirs: src
- default-language: Haskell2010
- ghc-options: -Wall
- @@ -71,6 +73,7 @@ executable benchmark-lib
- , bytestring
- , criterion
- , file-embed
- + , serialise
- default-language: Haskell2010
- ghc-options: -threaded
- -O2
- diff --git a/dump-decls-lib/src/Json/Migrate.hs b/dump-decls-lib/src/Json/Migrate.hs
- index 1be8e10..326231d 100644
- --- a/dump-decls-lib/src/Json/Migrate.hs
- +++ b/dump-decls-lib/src/Json/Migrate.hs
- @@ -3,6 +3,7 @@
- module Json.Migrate
- ( migrateV3ToV4
- , migrateV4ToV5
- +, migrateV5ToV6
- , unversionedJsonFileToV3
- )
- where
- @@ -12,8 +13,10 @@ import Versioned
- import Json.Version3 ()
- import Json.Version4 ()
- import Json.Version5 ()
- +import Json.Version6 ()
- import qualified Data.Text as T
- +
- migrateV3ToV4
- :: FilePath -- old (v3)
- -> FilePath -- new (v4)
- @@ -32,6 +35,12 @@ migrateV4ToV5 fpOld fpNew =
- Left e -> fail $ "migrateV4ToV5: failed to parse old version: " <> e
- Right a -> pure a
- +migrateV5ToV6 :: FilePath -> FilePath -> IO [DeclarationMapJson T.Text]
- +migrateV5ToV6 fpOld fpNew = do
- + versionedJsonMigrateFileToBinary (Version :: Version 5, fpOld) (Version :: Version 6, fpNew) >>= \case
- + Left e -> fail $ "migrateV4ToV5: failed to parse old version: " <> e
- + Right a -> pure a
- +
- unversionedJsonFileToV3
- :: FilePath -- unversioned
- -> FilePath -- v3
- diff --git a/dump-decls-lib/src/Json/Version6.hs b/dump-decls-lib/src/Json/Version6.hs
- new file mode 100644
- index 0000000..6ce5886
- --- /dev/null
- +++ b/dump-decls-lib/src/Json/Version6.hs
- @@ -0,0 +1,37 @@
- +{-# LANGUAGE DataKinds #-}
- +{-# LANGUAGE FlexibleInstances #-}
- +{-# OPTIONS_GHC -Wno-orphans #-}
- +module Json.Version6
- +(
- +)
- +where
- +
- +import Types
- + ( Boxity, FgType, TyConParseError, FgPackage, FgTyCon )
- +import Json
- + ( DeclarationMapJson, ModuleDeclarations, TypeInfo, FunctionType )
- +import Versioned ( Versioned )
- +
- +import Data.Text as T ( Text )
- +
- +import Codec.Serialise
- +
- +instance (Serialise value) => Serialise (FunctionType value)
- +
- +instance (Serialise tycon) => Serialise (TypeInfo tycon)
- +
- +instance (Ord value, Serialise value) => Serialise (ModuleDeclarations value)
- +
- +instance (Ord value, Serialise value) => Serialise (DeclarationMapJson value)
- +
- +instance (Serialise text) => Serialise (FgTyCon text)
- +
- +instance (Serialise text) => Serialise (FgPackage text)
- +
- +instance Serialise TyConParseError
- +
- +instance (Serialise value) => Serialise (FgType value)
- +
- +instance Serialise Boxity
- +
- +instance Serialise (Versioned.Versioned 6 [DeclarationMapJson T.Text])
- diff --git a/dump-decls-lib/src/Versioned.hs b/dump-decls-lib/src/Versioned.hs
- index dd75e0c..1aca197 100644
- --- a/dump-decls-lib/src/Versioned.hs
- +++ b/dump-decls-lib/src/Versioned.hs
- @@ -19,6 +19,7 @@ module Versioned
- , versionedToJSON
- , versionedParseJSON
- , versionedJsonMigrateFile
- +, versionedJsonMigrateFileToBinary
- , unversionedJsonFileToVersioned
- -- * Re-exports
- , Nat, KnownNat
- @@ -33,6 +34,8 @@ import qualified Data.Aeson.Types as A
- import GHC.Generics (Generic)
- import Control.DeepSeq (NFData)
- +import Codec.Serialise
- +
- mkVersioned
- :: a
- -> Versioned version a
- @@ -112,6 +115,27 @@ versionedJsonMigrateFile (_, fpOld) (_, fpNew) = do
- coerce :: Versioned oldVersion a -> Versioned newVersion a
- coerce = mkVersioned . versionedContent
- +versionedJsonMigrateFileToBinary
- + :: forall a oldVersion newVersion.
- + ( A.FromJSON (Versioned oldVersion a)
- + , Serialise (Versioned newVersion a)
- + )
- + => (Version oldVersion, FilePath) -- old version
- + -> (Version newVersion, FilePath) -- new version
- + -> IO (Either String a) -- either a decoding error or the decoded content
- +versionedJsonMigrateFileToBinary (_, fpOld) (_, fpNew) = do
- + A.eitherDecodeFileStrict fpOld >>= \case
- + Left e ->
- + pure $ Left e
- + Right !oldVersion -> do
- + let newVersion = coerce oldVersion
- + writeFileSerialise fpNew newVersion
- + pure $! Right $! versionedContent newVersion
- + where
- + coerce :: Versioned oldVersion a -> Versioned newVersion a
- + coerce = mkVersioned . versionedContent
- +
- +
- unversionedJsonFileToVersioned
- :: forall a version.
- ( A.FromJSON a
Advertisement
Add Comment
Please, Sign In to add comment