Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miner: remove contention on worker.currentMu for pending API requests #16497

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ type worker struct {
currentMu sync.Mutex
current *Work

snapshotMu sync.RWMutex
snapshotBlock *types.Block
snapshotState *state.StateDB

uncleMu sync.Mutex
possibleUncles map[common.Hash]*types.Block

Expand Down Expand Up @@ -171,32 +175,28 @@ func (self *worker) setExtra(extra []byte) {
}

func (self *worker) pending() (*types.Block, *state.StateDB) {
self.currentMu.Lock()
defer self.currentMu.Unlock()

if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
), self.current.state.Copy()
// return a snapshot to avoid contention on currentMu mutex
self.snapshotMu.RLock()
defer self.snapshotMu.RUnlock()
return self.snapshotBlock, self.snapshotState.Copy()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: Here we are returning the same snapshotBlock to all consumers, rather than a copy each time. That seemed ok to me in my testing, but we could be more conservative and use types.NewBlock(...) here to make another copy of the snapshotBlock. Same applies for pendingBlock() below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A block is immutable AFAIK, so it should be safe to return the same thing for everyone.

}
return self.current.Block, self.current.state.Copy()
}

func (self *worker) pendingBlock() *types.Block {
self.currentMu.Lock()
defer self.currentMu.Unlock()
return self.current.Block, self.current.state.Copy()
}

func (self *worker) pendingBlock() *types.Block {
if atomic.LoadInt32(&self.mining) == 0 {
return types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
// return a snapshot to avoid contention on currentMu mutex
self.snapshotMu.RLock()
defer self.snapshotMu.RUnlock()
return self.snapshotBlock
}

self.currentMu.Lock()
defer self.currentMu.Unlock()
return self.current.Block
}

Expand Down Expand Up @@ -268,6 +268,7 @@ func (self *worker) update() {
txset := types.NewTransactionsByPriceAndNonce(self.current.signer, txs)

self.current.commitTransactions(self.mux, txset, self.chain, self.coinbase)
self.updateSnapshot()
self.currentMu.Unlock()
} else {
// If we're mining, but nothing is being processed, wake on new transactions
Expand Down Expand Up @@ -489,6 +490,7 @@ func (self *worker) commitNewWork() {
self.unconfirmed.Shift(work.Block.NumberU64() - 1)
}
self.push(work)
self.updateSnapshot()
}

func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
Expand All @@ -506,6 +508,19 @@ func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
return nil
}

func (self *worker) updateSnapshot() {
self.snapshotMu.Lock()
defer self.snapshotMu.Unlock()

self.snapshotBlock = types.NewBlock(
self.current.header,
self.current.txs,
nil,
self.current.receipts,
)
self.snapshotState = self.current.state.Copy()
}

func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, bc *core.BlockChain, coinbase common.Address) {
gp := new(core.GasPool).AddGas(env.header.GasLimit)

Expand Down