Skip to content

Commit

Permalink
PEVM-opt: Remove deepcopy and useless fixup at merge phase (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny2022da authored Sep 13, 2024
1 parent 5d7dd63 commit d3ccc6f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/parallel_state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *ParallelStateProcessor) init() {

p.parallelDBManager = state.NewParallelDBManager(20000, state.NewEmptySlotDB)

quickMergeNum := 2 // p.parallelNum / 2
quickMergeNum := 0 // p.parallelNum / 2
for i := 0; i < p.parallelNum-quickMergeNum; i++ {
p.slotState[i] = &SlotState{
primaryWakeUpChan: make(chan struct{}, 1),
Expand Down
6 changes: 3 additions & 3 deletions core/state/parallel_statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,10 +1726,10 @@ func (s *ParallelStateDB) FinaliseForParallel(deleteEmptyObjects bool, mainDB *S
// obj.finalise(true) will clear its dirtyStorage, will make prefetch broken.
if !s.isParallel || !s.parallel.isSlotDB {
obj.finalise(true) // Prefetch slots in the background
} else {
} /* else {
// don't do finalise() here as to keep dirtyObjects unchanged in dirtyStorages, which avoid contention issue.
obj.fixUpOriginAndResetPendingStorage()
}
// obj.fixUpOriginAndResetPendingStorage()
}*/
}

if obj.created {
Expand Down
11 changes: 11 additions & 0 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
return object
}

// rebase is similar with deepCopy, instead of do copy, it just rebase the db of the Object.
// it is used for the case that the original state object in the mainDB is obsoleted or does not exist,
// so that we can reuse the one generated in slot execution.
func (object *stateObject) rebase(db *StateDB) *stateObject {
object.db = db.getBaseStateDB()
object.dbItf = db
return object
}

func (s *stateObject) MergeSlotObject(db Database, dirtyObjs *stateObject, keys StateKeys) {
for key := range keys {
// In parallel mode, always GetState by StateDB, not by StateObject directly,
Expand All @@ -810,10 +819,12 @@ func (s *stateObject) MergeSlotObject(db Database, dirtyObjs *stateObject, keys
dirtyObjs.originStorage.Range(func(keyItf, valueItf interface{}) bool {
key := keyItf.(common.Hash)
value := valueItf.(common.Hash)
s.storageRecordsLock.Lock()
// Skip noop changes, persist actual changes
if _, ok := s.originStorage.GetValue(key); !ok {
s.originStorage.StoreValue(key, value)
}
s.storageRecordsLock.Unlock()
return true
})
}
Expand Down
21 changes: 18 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
mainObj, exist := s.loadStateObj(addr)
if !exist || mainObj.deleted {
// addr not exist on main DB, the object is created in the merging tx.
mainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
mainObj = dirtyObj.rebase(s)
} else {
mainObj = dirtyObj.deepCopy(s)
}
if !dirtyObj.deleted {
mainObj.finalise(true)
}
Expand Down Expand Up @@ -2554,7 +2559,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
// may not empty until block validation. so the pendingStorage filled by the execution of previous txs
// in same block may get overwritten by deepCopy here, which causes issue in root calculation.
if _, created := s.parallel.createdObjectRecord[addr]; created {
newMainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
newMainObj = dirtyObj.rebase(s)
} else {
newMainObj = dirtyObj.deepCopy(s)
}
} else {
// Merge the dirtyObject with mainObject
if _, balanced := slotDb.parallel.balanceChangesInSlot[addr]; balanced {
Expand All @@ -2579,7 +2589,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
}
} else {
// The object is deleted in the TX.
newMainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
newMainObj = dirtyObj.rebase(s)
} else {
newMainObj = dirtyObj.deepCopy(s)
}
}

// All cases with addrStateChange set to true/false can be deleted. so handle it here.
Expand Down

0 comments on commit d3ccc6f

Please sign in to comment.