Commit 84e3f3f0 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[OPTIM] after profiling, optimize serialisation

parent 5fa1eae2
Pipeline #859 canceled with stage
...@@ -235,6 +235,7 @@ executables: ...@@ -235,6 +235,7 @@ executables:
- -rtsopts - -rtsopts
- -threaded - -threaded
- -with-rtsopts=-N - -with-rtsopts=-N
- -fprof-auto
dependencies: dependencies:
- base - base
- containers - containers
......
...@@ -26,13 +26,14 @@ TODO-SECURITY: Critical ...@@ -26,13 +26,14 @@ TODO-SECURITY: Critical
module Gargantext.API.Admin.Settings module Gargantext.API.Admin.Settings
where where
import Codec.Serialise (Serialise(), serialise)
import Control.Concurrent import Control.Concurrent
import Control.Debounce (mkDebounce, defaultDebounceSettings, debounceFreq, debounceAction) import Control.Debounce (mkDebounce, defaultDebounceSettings, debounceFreq, debounceAction)
import Control.Exception (finally) import Control.Exception (finally)
import Control.Lens import Control.Lens
import Control.Monad.Logger import Control.Monad.Logger
import Control.Monad.Reader import Control.Monad.Reader
import Data.Aeson import Data.Aeson hiding (encode)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Either (either) import Data.Either (either)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
...@@ -187,11 +188,11 @@ repoSnapshot = repoDir <> "/repo.json" ...@@ -187,11 +188,11 @@ repoSnapshot = repoDir <> "/repo.json"
-- | TODO add hard coded file in Settings -- | TODO add hard coded file in Settings
-- This assumes we own the lock on repoSnapshot. -- This assumes we own the lock on repoSnapshot.
repoSaverAction :: ToJSON a => a -> IO () repoSaverAction :: Serialise a => a -> IO ()
repoSaverAction a = do repoSaverAction a = do
withTempFile "repos" "tmp-repo.json" $ \fp h -> do withTempFile "repos" "tmp-repo.json" $ \fp h -> do
-- printDebug "repoSaverAction" fp -- printDebug "repoSaverAction" fp
L.hPut h $ encode a L.hPut h $ serialise a
hClose h hClose h
renameFile fp repoSnapshot renameFile fp repoSnapshot
......
...@@ -96,6 +96,7 @@ module Gargantext.API.Ngrams ...@@ -96,6 +96,7 @@ module Gargantext.API.Ngrams
) )
where where
import Codec.Serialise (Serialise())
import Control.Category ((>>>)) import Control.Category ((>>>))
import Control.Concurrent import Control.Concurrent
import Control.Lens (makeLenses, makePrisms, Getter, Iso', iso, from, (.~), (?=), (#), to, folded, {-withIndex, ifolded,-} view, use, (^.), (^..), (^?), (+~), (%~), (.~), (%=), sumOf, at, _Just, Each(..), itraverse_, both, forOf_, (%%~), (?~), mapped) import Control.Lens (makeLenses, makePrisms, Getter, Iso', iso, from, (.~), (?=), (#), to, folded, {-withIndex, ifolded,-} view, use, (^.), (^..), (^?), (+~), (%~), (.~), (%=), sumOf, at, _Just, Each(..), itraverse_, both, forOf_, (%%~), (?~), mapped)
...@@ -238,6 +239,8 @@ makeLenses ''NgramsRepoElement ...@@ -238,6 +239,8 @@ makeLenses ''NgramsRepoElement
instance ToSchema NgramsRepoElement where instance ToSchema NgramsRepoElement where
declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_nre_") declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_nre_")
instance Serialise (MSet NgramsTerm)
instance Serialise NgramsRepoElement
data NgramsElement = data NgramsElement =
NgramsElement { _ne_ngrams :: NgramsTerm NgramsElement { _ne_ngrams :: NgramsTerm
...@@ -443,6 +446,8 @@ instance ToSchema a => ToSchema (PatchSet a) ...@@ -443,6 +446,8 @@ instance ToSchema a => ToSchema (PatchSet a)
type AddRem = Replace (Maybe ()) type AddRem = Replace (Maybe ())
instance Serialise AddRem
remPatch, addPatch :: AddRem remPatch, addPatch :: AddRem
remPatch = replace (Just ()) Nothing remPatch = replace (Just ()) Nothing
addPatch = replace Nothing (Just ()) addPatch = replace Nothing (Just ())
...@@ -452,6 +457,7 @@ isRem = (== remPatch) ...@@ -452,6 +457,7 @@ isRem = (== remPatch)
type PatchMap = PM.PatchMap type PatchMap = PM.PatchMap
newtype PatchMSet a = PatchMSet (PatchMap a AddRem) newtype PatchMSet a = PatchMSet (PatchMap a AddRem)
deriving (Eq, Show, Generic, Validity, Semigroup, Monoid, deriving (Eq, Show, Generic, Validity, Semigroup, Monoid,
Transformable, Composable) Transformable, Composable)
...@@ -459,6 +465,9 @@ newtype PatchMSet a = PatchMSet (PatchMap a AddRem) ...@@ -459,6 +465,9 @@ newtype PatchMSet a = PatchMSet (PatchMap a AddRem)
type ConflictResolutionPatchMSet a = a -> ConflictResolutionReplace (Maybe ()) type ConflictResolutionPatchMSet a = a -> ConflictResolutionReplace (Maybe ())
type instance ConflictResolution (PatchMSet a) = ConflictResolutionPatchMSet a type instance ConflictResolution (PatchMSet a) = ConflictResolutionPatchMSet a
instance (Serialise a, Ord a) => Serialise (PatchMap a AddRem)
instance (Serialise a, Ord a) => Serialise (PatchMSet a)
-- TODO this breaks module abstraction -- TODO this breaks module abstraction
makePrisms ''PM.PatchMap makePrisms ''PM.PatchMap
...@@ -528,6 +537,10 @@ instance ToSchema NgramsPatch where ...@@ -528,6 +537,10 @@ instance ToSchema NgramsPatch where
instance Arbitrary NgramsPatch where instance Arbitrary NgramsPatch where
arbitrary = NgramsPatch <$> arbitrary <*> (replace <$> arbitrary <*> arbitrary) arbitrary = NgramsPatch <$> arbitrary <*> (replace <$> arbitrary <*> arbitrary)
instance Serialise NgramsPatch
instance Serialise (Replace ListType)
instance Serialise ListType
type NgramsPatchIso = PairPatch (PatchMSet NgramsTerm) (Replace ListType) type NgramsPatchIso = PairPatch (PatchMSet NgramsTerm) (Replace ListType)
_NgramsPatch :: Iso' NgramsPatch NgramsPatchIso _NgramsPatch :: Iso' NgramsPatch NgramsPatchIso
...@@ -578,6 +591,9 @@ instance Action NgramsPatch (Maybe NgramsRepoElement) where ...@@ -578,6 +591,9 @@ instance Action NgramsPatch (Maybe NgramsRepoElement) where
newtype NgramsTablePatch = NgramsTablePatch (PatchMap NgramsTerm NgramsPatch) newtype NgramsTablePatch = NgramsTablePatch (PatchMap NgramsTerm NgramsPatch)
deriving (Eq, Show, Generic, ToJSON, FromJSON, Semigroup, Monoid, Validity, Transformable) deriving (Eq, Show, Generic, ToJSON, FromJSON, Semigroup, Monoid, Validity, Transformable)
instance Serialise NgramsTablePatch
instance Serialise (PatchMap NgramsTerm NgramsPatch)
instance FromField NgramsTablePatch instance FromField NgramsTablePatch
where where
fromField = fromField' fromField = fromField'
...@@ -736,6 +752,8 @@ instance (ToJSON s, ToJSON p) => ToJSON (Repo s p) where ...@@ -736,6 +752,8 @@ instance (ToJSON s, ToJSON p) => ToJSON (Repo s p) where
toJSON = genericToJSON $ unPrefix "_r_" toJSON = genericToJSON $ unPrefix "_r_"
toEncoding = genericToEncoding $ unPrefix "_r_" toEncoding = genericToEncoding $ unPrefix "_r_"
instance (Serialise s, Serialise p) => Serialise (Repo s p)
makeLenses ''Repo makeLenses ''Repo
initRepo :: Monoid s => Repo s p initRepo :: Monoid s => Repo s p
...@@ -745,6 +763,9 @@ type NgramsRepo = Repo NgramsState NgramsStatePatch ...@@ -745,6 +763,9 @@ type NgramsRepo = Repo NgramsState NgramsStatePatch
type NgramsState = Map TableNgrams.NgramsType (Map NodeId NgramsTableMap) type NgramsState = Map TableNgrams.NgramsType (Map NodeId NgramsTableMap)
type NgramsStatePatch = PatchMap TableNgrams.NgramsType (PatchMap NodeId NgramsTablePatch) type NgramsStatePatch = PatchMap TableNgrams.NgramsType (PatchMap NodeId NgramsTablePatch)
instance Serialise (PM.PatchMap NodeId NgramsTablePatch)
instance Serialise NgramsStatePatch
initMockRepo :: NgramsRepo initMockRepo :: NgramsRepo
initMockRepo = Repo 1 s [] initMockRepo = Repo 1 s []
where where
......
...@@ -91,7 +91,7 @@ mkNodeWithParent NodeList (Just i) uId name = ...@@ -91,7 +91,7 @@ mkNodeWithParent NodeList (Just i) uId name =
where where
hd = defaultAnnuaire hd = defaultAnnuaire
mkNodeWithParent NodeGraph (Just i) uId name = mkNodeWithParent NodeGraph (Just i) uId _name =
insertNodesWithParentR (Just i) [node NodeGraph "Graph" hd Nothing uId] insertNodesWithParentR (Just i) [node NodeGraph "Graph" hd Nothing uId]
where where
hd = arbitraryGraph hd = arbitraryGraph
......
...@@ -25,6 +25,7 @@ Portability : POSIX ...@@ -25,6 +25,7 @@ Portability : POSIX
module Gargantext.Database.Admin.Types.Node module Gargantext.Database.Admin.Types.Node
where where
import Codec.Serialise (Serialise())
import Control.Applicative ((<*>)) import Control.Applicative ((<*>))
import Control.Lens hiding (elements, (&)) import Control.Lens hiding (elements, (&))
import Control.Monad (mzero) import Control.Monad (mzero)
...@@ -138,6 +139,8 @@ pgNodeId = O.pgInt4 . id2int ...@@ -138,6 +139,8 @@ pgNodeId = O.pgInt4 . id2int
newtype NodeId = NodeId Int newtype NodeId = NodeId Int
deriving (Show, Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON) deriving (Show, Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON)
instance Serialise NodeId
instance ToField NodeId where instance ToField NodeId where
toField (NodeId n) = toField n toField (NodeId n) = toField n
......
...@@ -27,6 +27,7 @@ Ngrams connection to the Database. ...@@ -27,6 +27,7 @@ Ngrams connection to the Database.
module Gargantext.Database.Schema.Ngrams module Gargantext.Database.Schema.Ngrams
where where
import Codec.Serialise (Serialise())
import Control.Lens (makeLenses, over) import Control.Lens (makeLenses, over)
import Control.Monad (mzero) import Control.Monad (mzero)
import Data.Aeson import Data.Aeson
...@@ -90,6 +91,8 @@ ngramsTable = Table "ngrams" (pNgramsDb NgramsDb { _ngrams_id = optional "id" ...@@ -90,6 +91,8 @@ ngramsTable = Table "ngrams" (pNgramsDb NgramsDb { _ngrams_id = optional "id"
data NgramsType = Authors | Institutes | Sources | NgramsTerms data NgramsType = Authors | Institutes | Sources | NgramsTerms
deriving (Eq, Show, Read, Ord, Enum, Bounded, Generic) deriving (Eq, Show, Read, Ord, Enum, Bounded, Generic)
instance Serialise NgramsType
ngramsTypes :: [NgramsType] ngramsTypes :: [NgramsType]
ngramsTypes = [minBound..] ngramsTypes = [minBound..]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment