Guest User

dump-decls-lib binary patch

a guest
Dec 24th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. diff --git a/dump-decls-lib/bench/Main.hs b/dump-decls-lib/bench/Main.hs
  2. index b8e6e03..6066ac4 100644
  3. --- a/dump-decls-lib/bench/Main.hs
  4. +++ b/dump-decls-lib/bench/Main.hs
  5. @@ -9,6 +9,7 @@ import Versioned
  6. import Json.Version3 ()
  7. import Json.Version4 ()
  8. import Json.Version5 ()
  9. +import Json.Version6 ()
  10. import Criterion.Main
  11. import Data.Functor (void, (<&>))
  12. import Control.Monad ((<=<))
  13. @@ -22,6 +23,8 @@ import qualified Data.ByteString.Lazy as BSL
  14. import Data.Maybe (fromMaybe)
  15. import qualified Data.FileEmbed
  16.  
  17. +import Codec.Serialise
  18. +
  19. -- TODO: include in this package
  20.  
  21. dataFileNameV3 :: FilePath
  22. @@ -36,6 +39,10 @@ dataFileNameV5 :: FilePath
  23. dataFileNameV5 =
  24. "dump-decls-lib/bench/data/all-v5.json"
  25.  
  26. +dataFileNameV6 :: FilePath
  27. +dataFileNameV6 =
  28. + "dump-decls-lib/bench/data/all-v6.bin"
  29. +
  30. fileReadDeclarationMapReadFile
  31. :: A.FromJSON (Versioned version [Json.DeclarationMapJson T.Text]) => FilePath
  32. -> IO (Versioned version [Json.DeclarationMapJson T.Text])
  33. @@ -56,6 +63,9 @@ fileReadDeclarationMapDecodeFileStrict fileName =
  34. fail
  35. pure
  36.  
  37. +fileReadBinary :: Serialise (Versioned version [Json.DeclarationMapJson T.Text]) => FilePath -> IO (Versioned version [Json.DeclarationMapJson T.Text])
  38. +fileReadBinary = readFileDeserialise
  39. +
  40. main :: IO ()
  41. main = do
  42. defaultMain
  43. @@ -70,5 +80,9 @@ main = do
  44. , bench "v4" $ nfIO (fileReadDeclarationMapDecodeFileStrict dataFileNameV4 :: IO (Versioned 4 [Json.DeclarationMapJson T.Text]))
  45. , bench "v5" $ nfIO (fileReadDeclarationMapDecodeFileStrict dataFileNameV5 :: IO (Versioned 5 [Json.DeclarationMapJson T.Text]))
  46. ]
  47. + , bgroup "decodeFileBinary"
  48. + [
  49. + bench "v6" $ nfIO (fileReadBinary dataFileNameV6 :: IO (Versioned 6 [Json.DeclarationMapJson T.Text]))
  50. + ]
  51. ]
  52. ]
  53. diff --git a/dump-decls-lib/dump-decls-lib.cabal b/dump-decls-lib/dump-decls-lib.cabal
  54. index fa53ba7..fe0ae80 100644
  55. --- a/dump-decls-lib/dump-decls-lib.cabal
  56. +++ b/dump-decls-lib/dump-decls-lib.cabal
  57. @@ -17,6 +17,7 @@ library
  58. , Json.Version3
  59. , Json.Version4
  60. , Json.Version5
  61. + , Json.Version6
  62. , Types
  63. , Compat
  64. , Compat.Aeson
  65. @@ -32,6 +33,7 @@ library
  66. , deepseq
  67. , utf8-string
  68. , vector
  69. + , serialise
  70. hs-source-dirs: src
  71. default-language: Haskell2010
  72. ghc-options: -Wall
  73. @@ -71,6 +73,7 @@ executable benchmark-lib
  74. , bytestring
  75. , criterion
  76. , file-embed
  77. + , serialise
  78. default-language: Haskell2010
  79. ghc-options: -threaded
  80. -O2
  81. diff --git a/dump-decls-lib/src/Json/Migrate.hs b/dump-decls-lib/src/Json/Migrate.hs
  82. index 1be8e10..326231d 100644
  83. --- a/dump-decls-lib/src/Json/Migrate.hs
  84. +++ b/dump-decls-lib/src/Json/Migrate.hs
  85. @@ -3,6 +3,7 @@
  86. module Json.Migrate
  87. ( migrateV3ToV4
  88. , migrateV4ToV5
  89. +, migrateV5ToV6
  90. , unversionedJsonFileToV3
  91. )
  92. where
  93. @@ -12,8 +13,10 @@ import Versioned
  94. import Json.Version3 ()
  95. import Json.Version4 ()
  96. import Json.Version5 ()
  97. +import Json.Version6 ()
  98. import qualified Data.Text as T
  99.  
  100. +
  101. migrateV3ToV4
  102. :: FilePath -- old (v3)
  103. -> FilePath -- new (v4)
  104. @@ -32,6 +35,12 @@ migrateV4ToV5 fpOld fpNew =
  105. Left e -> fail $ "migrateV4ToV5: failed to parse old version: " <> e
  106. Right a -> pure a
  107.  
  108. +migrateV5ToV6 :: FilePath -> FilePath -> IO [DeclarationMapJson T.Text]
  109. +migrateV5ToV6 fpOld fpNew = do
  110. + versionedJsonMigrateFileToBinary (Version :: Version 5, fpOld) (Version :: Version 6, fpNew) >>= \case
  111. + Left e -> fail $ "migrateV4ToV5: failed to parse old version: " <> e
  112. + Right a -> pure a
  113. +
  114. unversionedJsonFileToV3
  115. :: FilePath -- unversioned
  116. -> FilePath -- v3
  117. diff --git a/dump-decls-lib/src/Json/Version6.hs b/dump-decls-lib/src/Json/Version6.hs
  118. new file mode 100644
  119. index 0000000..6ce5886
  120. --- /dev/null
  121. +++ b/dump-decls-lib/src/Json/Version6.hs
  122. @@ -0,0 +1,37 @@
  123. +{-# LANGUAGE DataKinds #-}
  124. +{-# LANGUAGE FlexibleInstances #-}
  125. +{-# OPTIONS_GHC -Wno-orphans #-}
  126. +module Json.Version6
  127. +(
  128. +)
  129. +where
  130. +
  131. +import Types
  132. + ( Boxity, FgType, TyConParseError, FgPackage, FgTyCon )
  133. +import Json
  134. + ( DeclarationMapJson, ModuleDeclarations, TypeInfo, FunctionType )
  135. +import Versioned ( Versioned )
  136. +
  137. +import Data.Text as T ( Text )
  138. +
  139. +import Codec.Serialise
  140. +
  141. +instance (Serialise value) => Serialise (FunctionType value)
  142. +
  143. +instance (Serialise tycon) => Serialise (TypeInfo tycon)
  144. +
  145. +instance (Ord value, Serialise value) => Serialise (ModuleDeclarations value)
  146. +
  147. +instance (Ord value, Serialise value) => Serialise (DeclarationMapJson value)
  148. +
  149. +instance (Serialise text) => Serialise (FgTyCon text)
  150. +
  151. +instance (Serialise text) => Serialise (FgPackage text)
  152. +
  153. +instance Serialise TyConParseError
  154. +
  155. +instance (Serialise value) => Serialise (FgType value)
  156. +
  157. +instance Serialise Boxity
  158. +
  159. +instance Serialise (Versioned.Versioned 6 [DeclarationMapJson T.Text])
  160. diff --git a/dump-decls-lib/src/Versioned.hs b/dump-decls-lib/src/Versioned.hs
  161. index dd75e0c..1aca197 100644
  162. --- a/dump-decls-lib/src/Versioned.hs
  163. +++ b/dump-decls-lib/src/Versioned.hs
  164. @@ -19,6 +19,7 @@ module Versioned
  165. , versionedToJSON
  166. , versionedParseJSON
  167. , versionedJsonMigrateFile
  168. +, versionedJsonMigrateFileToBinary
  169. , unversionedJsonFileToVersioned
  170. -- * Re-exports
  171. , Nat, KnownNat
  172. @@ -33,6 +34,8 @@ import qualified Data.Aeson.Types as A
  173. import GHC.Generics (Generic)
  174. import Control.DeepSeq (NFData)
  175.  
  176. +import Codec.Serialise
  177. +
  178. mkVersioned
  179. :: a
  180. -> Versioned version a
  181. @@ -112,6 +115,27 @@ versionedJsonMigrateFile (_, fpOld) (_, fpNew) = do
  182. coerce :: Versioned oldVersion a -> Versioned newVersion a
  183. coerce = mkVersioned . versionedContent
  184.  
  185. +versionedJsonMigrateFileToBinary
  186. + :: forall a oldVersion newVersion.
  187. + ( A.FromJSON (Versioned oldVersion a)
  188. + , Serialise (Versioned newVersion a)
  189. + )
  190. + => (Version oldVersion, FilePath) -- old version
  191. + -> (Version newVersion, FilePath) -- new version
  192. + -> IO (Either String a) -- either a decoding error or the decoded content
  193. +versionedJsonMigrateFileToBinary (_, fpOld) (_, fpNew) = do
  194. + A.eitherDecodeFileStrict fpOld >>= \case
  195. + Left e ->
  196. + pure $ Left e
  197. + Right !oldVersion -> do
  198. + let newVersion = coerce oldVersion
  199. + writeFileSerialise fpNew newVersion
  200. + pure $! Right $! versionedContent newVersion
  201. + where
  202. + coerce :: Versioned oldVersion a -> Versioned newVersion a
  203. + coerce = mkVersioned . versionedContent
  204. +
  205. +
  206. unversionedJsonFileToVersioned
  207. :: forall a version.
  208. ( A.FromJSON a
  209.  
Advertisement
Add Comment
Please, Sign In to add comment