avoid rewindStateData/state_transition in fast-path (#919)
* avoid rewindStateData/state_transition totally in fast-pathed updateStateData calls * document motivation behind BlockPool.getStateDataCached(...) and clarify naming
This commit is contained in:
parent
6729d3c032
commit
7e94482409
|
@ -715,6 +715,28 @@ proc rewindState(pool: BlockPool, state: var StateData, bs: BlockSlot):
|
|||
|
||||
ancestors
|
||||
|
||||
proc getStateDataCached(pool: BlockPool, state: var StateData, bs: BlockSlot): bool =
|
||||
# This pointedly does not run rewindState or state_transition, but otherwise
|
||||
# mostly matches updateStateData(...), because it's too expensive to run the
|
||||
# rewindState(...)/skipAndUpdateState(...)/state_transition(...) procs, when
|
||||
# each hash_tree_root(...) consumes a nontrivial fraction of a second.
|
||||
for db in [pool.db, pool.cachedStates[0], pool.cachedStates[1]]:
|
||||
if (let tmp = db.getStateRoot(bs.blck.root, bs.slot); tmp.isSome()):
|
||||
if not db.containsState(tmp.get):
|
||||
continue
|
||||
|
||||
let
|
||||
root = tmp.get()
|
||||
ancestorState = db.getState(root)
|
||||
|
||||
doAssert ancestorState.isSome()
|
||||
state.data.data = ancestorState.get()
|
||||
state.data.root = root
|
||||
state.blck = pool.get(bs.blck).refs
|
||||
return true
|
||||
|
||||
false
|
||||
|
||||
proc updateStateData*(pool: BlockPool, state: var StateData, bs: BlockSlot) =
|
||||
## Rewind or advance state such that it matches the given block and slot -
|
||||
## this may include replaying from an earlier snapshot if blck is on a
|
||||
|
@ -732,6 +754,9 @@ proc updateStateData*(pool: BlockPool, state: var StateData, bs: BlockSlot) =
|
|||
|
||||
return # State already at the right spot
|
||||
|
||||
if pool.getStateDataCached(state, bs):
|
||||
return
|
||||
|
||||
let ancestors = rewindState(pool, state, bs)
|
||||
|
||||
# If we come this far, we found the state root. The last block on the stack
|
||||
|
|
Loading…
Reference in New Issue