speedup a little

This commit is contained in:
Dmitry Zuikov 2024-01-07 12:26:34 +03:00
parent 00a316b786
commit 4095d0cf0d
4 changed files with 59 additions and 5 deletions

View File

@ -14,7 +14,7 @@ globalOptions = do
<&> maybe mempty (const [AppDebugOpt]) <&> maybe mempty (const [AppDebugOpt])
trace <- optional (flag' True (long "trace" <> help "allow more debug output")) trace <- optional (flag' True (long "trace" <> help "allow more debug output"))
<&> maybe mempty (const [AppDebugOpt]) <&> maybe mempty (const [AppTraceOpt])
replica <- optional (flag' True (long "replica" <> help "replica (slave) mode")) replica <- optional (flag' True (long "replica" <> help "replica (slave) mode"))

View File

@ -75,6 +75,7 @@ common shared-properties
, stm , stm
, text , text
, time , time
, timeit
, transformers , transformers
, typed-process , typed-process
, unordered-containers , unordered-containers

View File

@ -69,6 +69,8 @@ import Codec.Serialise
import Codec.Compression.GZip as GZip import Codec.Compression.GZip as GZip
import System.AtomicWrite.Writer.LazyByteString qualified as AwL import System.AtomicWrite.Writer.LazyByteString qualified as AwL
import System.TimeIt
import Streaming.Prelude qualified as S import Streaming.Prelude qualified as S
@ -422,7 +424,7 @@ updateFileMethod UpdateFileSync rpc fe = do
fnew <- renameFileUniq fpath fnew <- renameFileUniq fpath
info $ "renamed" <+> pretty fpath <+> pretty fnew info $ "renamed" <+> pretty fpath <+> pretty fnew
info $ "create dir" <+> pretty fpath debug $ "create dir" <+> pretty fpath
liftIO $ createDirectoryIfMissing True fpath liftIO $ createDirectoryIfMissing True fpath
let h = view remoteTree fe & fromHashRef let h = view remoteTree fe & fromHashRef
@ -494,11 +496,14 @@ scanState rpc = do
debug $ "refchan value" <+> pretty rv debug $ "refchan value" <+> pretty rv
withState do withState do
scanTx sto rv seen <- selectSeen rv
commitAll unless seen do
scanTx sto rv
commitAll
props <- withState selectProposes props <- withState selectProposes
-- FIXME: cache-somehow
((px,e), meta) <- findGoodNewBlock kr sto props ((px,e), meta) <- findGoodNewBlock kr sto props
>>= orThrowUser "no meta block found" >>= orThrowUser "no meta block found"
@ -512,16 +517,20 @@ scanState rpc = do
for_ rfs $ \rf -> do for_ rfs $ \rf -> do
updateFile rpc rf updateFile rpc rf
withState $ insertSeen rv
pure px pure px
where where
findGoodNewBlock kr sto props = do findGoodNewBlock kr sto props = do
runMaybeT (go props) runMaybeT (go props)
where where
go [] = mzero go [] = mzero
go (p:ps) = do go (p:ps) = do
let btx = fst p let btx = fst p
missed <- lift $ isMissed sto btx missed <- lift $ isMissed sto btx
if missed then if missed then
@ -530,7 +539,9 @@ scanState rpc = do
what <- S.head_ do what <- S.head_ do
walkMerkle (fromHashRef btx) (getBlock sto) $ \case walkMerkle (fromHashRef btx) (getBlock sto) $ \case
Right ( (hx:_) :: [HashRef] ) -> S.yield hx Right ( (hx:_) :: [HashRef] ) -> do
S.yield hx
_ -> pure () _ -> pure ()
hmeta <- toMPlus what hmeta <- toMPlus what
@ -555,6 +566,12 @@ scanState rpc = do
trace $ "got some" <+> pretty (length hs) trace $ "got some" <+> pretty (length hs)
for_ hs $ \htx -> void $ runMaybeT do for_ hs $ \htx -> void $ runMaybeT do
seen <- lift $ lift $ selectSeen htx
-- debug $ "SEEN" <+> pretty seen <+> pretty htx
guard (not seen)
bs <- toMPlus =<< getBlock sto (fromHashRef htx) bs <- toMPlus =<< getBlock sto (fromHashRef htx)
tx <- toMPlus $ deserialiseOrFail @(RefChanUpdate L4Proto) bs tx <- toMPlus $ deserialiseOrFail @(RefChanUpdate L4Proto) bs
@ -576,6 +593,8 @@ scanState rpc = do
trace $ "tx propose" <+> pretty htx <+> pretty mytx trace $ "tx propose" <+> pretty htx <+> pretty mytx
lift $ lift $ insertPropose htx mytx lift $ lift $ insertPropose htx mytx
lift $ lift $ insertSeen htx
dontPost :: AppPerks m => ShareCLI m Bool dontPost :: AppPerks m => ShareCLI m Bool
dontPost = do dontPost = do
opts <- asks ( view appOpts ) opts <- asks ( view appOpts )

View File

@ -54,6 +54,9 @@ instance HasHash HashRef where
newtype HashVal = HashVal { fromHashVal :: HashRef } newtype HashVal = HashVal { fromHashVal :: HashRef }
deriving newtype (IsString) deriving newtype (IsString)
wrapHash :: HasHash hx => hx -> HashVal
wrapHash hx = HashVal (HashRef (toHash hx))
instance ToField GK0Key where instance ToField GK0Key where
toField (GK0Key hs) = toField (show (pretty hs)) toField (GK0Key hs) = toField (show (pretty hs))
@ -127,6 +130,7 @@ populateState = do
|] |]
createRemoteFileTable createRemoteFileTable
createSeenTable
commitAll commitAll
@ -343,3 +347,33 @@ selectRemoteFile px k = do
limit 1 limit 1
|] (HashVal px, k) <&> listToMaybe |] (HashVal px, k) <&> listToMaybe
createSeenTable :: MonadUnliftIO m => DBPipeM m ()
createSeenTable = do
ddl [qc|create table if not exists seen
( hash text not null
, primary key (hash)
)
|]
insertSeen :: (MonadUnliftIO m, HasHash hx)
=> hx
-> DBPipeM m ()
insertSeen hx = do
insert [qc|
insert into seen (hash)
values (?)
on conflict (hash)
do nothing
|] (Only $ wrapHash hx)
selectSeen :: (MonadUnliftIO m, HasHash hx)
=> hx
-> DBPipeM m Bool
selectSeen hx = do
select [qc|
select 1 from seen where hash = ? limit 1
|] (Only $ wrapHash hx)
<&> (maybe False fromOnly . listToMaybe)