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

core: fine grained lock for TxPool.all #197

Merged
merged 2 commits into from
May 30, 2018
Merged

core: fine grained lock for TxPool.all #197

merged 2 commits into from
May 30, 2018

Conversation

jmank88
Copy link
Contributor

@jmank88 jmank88 commented May 29, 2018

@jmank88 jmank88 requested review from treeder, benbjohnson and rkononov and removed request for treeder and benbjohnson May 29, 2018 21:56
return true, old
}

func (l *txList) add(tx *types.Transaction) {
Copy link
Contributor Author

@jmank88 jmank88 May 29, 2018

Choose a reason for hiding this comment

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

enqueueTx() and Add() handle queueing a tx which might replace an existing transaction. Most callers were cases where the tx was being moved from pending, so we know it's not a replacement, and we can just call this simplified add() method directly instead.

@jmank88 jmank88 changed the title core: core: fine grained lock for TxPool.all May 29, 2018
- use a wrapped map w/ `sync.RWMutex` for `TxPool.all` to remove contention in `TxPool.Get`

- refactor TxPool.enqueueTx() callers
core/tx_pool.go Outdated
if queue.Empty() {
delete(pool.queue, addr)
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

Right now this conditional in the defer only executes if pool.queue[addr] doesn't exist and the queue is empty at the end of the function. Should this execute if pool.queue[addr] does exists but the queue is emptied by the end of the function?

e.g.

queue := pool.queue[addr]
if queue == nil {
	queue = newTxList(false)
	pool.queue[addr] = queue
}
defer func() {
	if queue.Empty() {
		delete(pool.queue, addr)
	}
}()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inside of this if pending block, we only add to the queue and never remove, so it should only be empty if we created it and then didn't end up adding any (but pre-emptive creation allows a cleaner queue.add usage down below). That being said, It's still possible to exit this pending conditional and enter the queue one below, which already handles this clean-up case, so there is likely a simpler solution here. Let me take another look and at least document it better if not refactor it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified and documented

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

core/tx_pool.go Outdated
type txLookup struct {
all map[common.Hash]*types.Transaction
lock sync.RWMutex
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should lock be mu for consistency with the other mutexes in gochain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's my usual preference, but I had copy-pasted this type initially.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@benbjohnson
Copy link
Contributor

Looks good overall. Just a couple questions.

@benbjohnson
Copy link
Contributor

lgtm

@jmank88 jmank88 merged commit c52354c into master May 30, 2018
@jmank88 jmank88 deleted the pool-all branch May 30, 2018 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants