From 96c1d217e60993d3e54a156d1e5bcdbff14464f3 Mon Sep 17 00:00:00 2001 From: Edmund Noble Date: Wed, 19 Jun 2024 16:31:02 -0400 Subject: [PATCH] perf: use module cache output from readFrom All of our time was being spent reading modules from the database, because the new readFrom didn't persist its cache back to the database state. I think originally the reason why it didn't was to avoid state leakage, but this is safe because the module cache is guaranteed correct (it just does JSON parsing). Change-Id: I405069a2ee17cfb4581df20402ff66c39675d90b --- changes/2024-06-19T182645-0400.txt | 2 ++ src/Chainweb/Pact/Backend/RelationalCheckpointer.hs | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 changes/2024-06-19T182645-0400.txt diff --git a/changes/2024-06-19T182645-0400.txt b/changes/2024-06-19T182645-0400.txt new file mode 100644 index 000000000..744eb2950 --- /dev/null +++ b/changes/2024-06-19T182645-0400.txt @@ -0,0 +1,2 @@ +Fix a performance bug in read-only replay which was not using a cache +for module data diff --git a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs index 3dc9ee2c7..e7ffea546 100644 --- a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs +++ b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs @@ -153,11 +153,11 @@ doReadFrom logger v cid sql moduleCacheVar maybeParent doRead = do Nothing -> genesisHeight v cid Just parent -> succ . _blockHeight . _parentHeader $ parent - withMVar moduleCacheVar $ \sharedModuleCache -> do + modifyMVar moduleCacheVar $ \sharedModuleCache -> do bracket (beginSavepoint sql BatchSavepoint) (\_ -> abortSavepoint sql BatchSavepoint) $ \() -> do - getEndTxId "doReadFrom" sql maybeParent >>= traverse \startTxId -> do + h <- getEndTxId "doReadFrom" sql maybeParent >>= traverse \startTxId -> do newDbEnv <- newMVar $ BlockEnv (mkBlockHandlerEnv v cid currentHeight sql DoNotPersistIntraBlockWrites logger) (initBlockState defaultModuleCacheLimit startTxId) @@ -184,7 +184,12 @@ doReadFrom logger v cid sql moduleCacheVar maybeParent doRead = do , _cpLookupProcessedTx = \hs -> runBlockEnv newDbEnv (doLookupSuccessful currentHeight hs) } - doRead curBlockDbEnv + r <- doRead curBlockDbEnv + finalCache <- _bsModuleCache . _benvBlockState <$> readMVar newDbEnv + return (r, finalCache) + case h of + NoHistory -> return (sharedModuleCache, NoHistory) + Historical (r, finalCache) -> return (finalCache, Historical r)