Skip to content

Commit

Permalink
FAB-16687 reduce mutex contention in validator
Browse files Browse the repository at this point in the history
There is a mutex that guards the construction of []sync.Once, and the
level of contention is currently O(n^2), which can be reduced to O(n)
by trivial changes.

Change-Id: I9522eeebf6998ca7403c4033bac5932298ce8067
Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
  • Loading branch information
guoger committed Dec 2, 2019
1 parent 151ea02 commit 6044822
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions core/common/validation/statebased/validator_keylevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,6 @@ func NewKeyLevelValidator(evaluator RWSetPolicyEvaluatorFactory, vpmgr KeyLevelV
}
}

func (klv *KeyLevelValidator) invokeOnce(block *common.Block, txnum uint64) *sync.Once {
klv.blockDep.mutex.Lock()
defer klv.blockDep.mutex.Unlock()

if klv.blockDep.blockNum != block.Header.Number {
klv.blockDep.blockNum = block.Header.Number
klv.blockDep.txDepOnce = make([]sync.Once, len(block.Data.Data))
}

return &klv.blockDep.txDepOnce[txnum]
}

func (klv *KeyLevelValidator) extractDependenciesForTx(blockNum, txNum uint64, envelopeBytes []byte) {
env, err := protoutil.GetEnvelopeFromBlock(envelopeBytes)
if err != nil {
Expand Down Expand Up @@ -235,10 +223,17 @@ func (klv *KeyLevelValidator) extractDependenciesForTx(blockNum, txNum uint64, e

// PreValidate implements the function of the StateBasedValidator interface
func (klv *KeyLevelValidator) PreValidate(txNum uint64, block *common.Block) {
klv.blockDep.mutex.Lock()
if klv.blockDep.blockNum != block.Header.Number {
klv.blockDep.blockNum = block.Header.Number
klv.blockDep.txDepOnce = make([]sync.Once, len(block.Data.Data))
}
klv.blockDep.mutex.Unlock()

for i := int64(txNum); i >= 0; i-- {
txPosition := uint64(i)

klv.invokeOnce(block, txPosition).Do(
klv.blockDep.txDepOnce[i].Do(
func() {
klv.extractDependenciesForTx(block.Header.Number, txPosition, block.Data.Data[txPosition])
})
Expand Down

0 comments on commit 6044822

Please sign in to comment.