Switch peer.requestQueue from linked list to slice.
Only two operations are performed with this data structure: adding to the back and removing from the front. Because middle inserts and deletions are never needed, a linked list results in overall worse performance due to an extra allocation for each element's node, worse cache locality, and the runtime cost of boxing/unboxing each item during accesses. On top of the performance gains, a slice is more type safe as it is a true generic data structure making it is impossible to insert or access an element with the wrong type.
This commit is contained in:
parent
11bf021ced
commit
869363a210
2 changed files with 7 additions and 6 deletions
|
@ -930,7 +930,7 @@ func (b *blockManager) handleInvMsg(imsg *invMsg) {
|
||||||
}
|
}
|
||||||
if !haveInv {
|
if !haveInv {
|
||||||
// Add it to the request queue.
|
// Add it to the request queue.
|
||||||
imsg.peer.requestQueue.PushBack(iv)
|
imsg.peer.requestQueue = append(imsg.peer.requestQueue, iv)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -980,9 +980,10 @@ func (b *blockManager) handleInvMsg(imsg *invMsg) {
|
||||||
numRequested := 0
|
numRequested := 0
|
||||||
gdmsg := btcwire.NewMsgGetData()
|
gdmsg := btcwire.NewMsgGetData()
|
||||||
requestQueue := imsg.peer.requestQueue
|
requestQueue := imsg.peer.requestQueue
|
||||||
for e := requestQueue.Front(); e != nil; e = requestQueue.Front() {
|
for len(requestQueue) != 0 {
|
||||||
iv := e.Value.(*btcwire.InvVect)
|
iv := requestQueue[0]
|
||||||
imsg.peer.requestQueue.Remove(e)
|
requestQueue[0] = nil
|
||||||
|
requestQueue = requestQueue[1:]
|
||||||
|
|
||||||
switch iv.Type {
|
switch iv.Type {
|
||||||
case btcwire.InvTypeBlock:
|
case btcwire.InvTypeBlock:
|
||||||
|
@ -1010,6 +1011,7 @@ func (b *blockManager) handleInvMsg(imsg *invMsg) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
imsg.peer.requestQueue = requestQueue
|
||||||
if len(gdmsg.InvList) > 0 {
|
if len(gdmsg.InvList) > 0 {
|
||||||
imsg.peer.QueueMessage(gdmsg, nil)
|
imsg.peer.QueueMessage(gdmsg, nil)
|
||||||
}
|
}
|
||||||
|
|
3
peer.go
3
peer.go
|
@ -162,7 +162,7 @@ type peer struct {
|
||||||
prevGetBlocksStop *btcwire.ShaHash // owned by blockmanager
|
prevGetBlocksStop *btcwire.ShaHash // owned by blockmanager
|
||||||
prevGetHdrsBegin *btcwire.ShaHash // owned by blockmanager
|
prevGetHdrsBegin *btcwire.ShaHash // owned by blockmanager
|
||||||
prevGetHdrsStop *btcwire.ShaHash // owned by blockmanager
|
prevGetHdrsStop *btcwire.ShaHash // owned by blockmanager
|
||||||
requestQueue *list.List
|
requestQueue []*btcwire.InvVect
|
||||||
filter *bloom.Filter
|
filter *bloom.Filter
|
||||||
relayMtx sync.Mutex
|
relayMtx sync.Mutex
|
||||||
disableRelayTx bool
|
disableRelayTx bool
|
||||||
|
@ -1890,7 +1890,6 @@ func newPeerBase(s *server, inbound bool) *peer {
|
||||||
knownInventory: NewMruInventoryMap(maxKnownInventory),
|
knownInventory: NewMruInventoryMap(maxKnownInventory),
|
||||||
requestedTxns: make(map[btcwire.ShaHash]struct{}),
|
requestedTxns: make(map[btcwire.ShaHash]struct{}),
|
||||||
requestedBlocks: make(map[btcwire.ShaHash]struct{}),
|
requestedBlocks: make(map[btcwire.ShaHash]struct{}),
|
||||||
requestQueue: list.New(),
|
|
||||||
filter: bloom.LoadFilter(nil),
|
filter: bloom.LoadFilter(nil),
|
||||||
outputQueue: make(chan outMsg, outputBufferSize),
|
outputQueue: make(chan outMsg, outputBufferSize),
|
||||||
sendQueue: make(chan outMsg, 1), // nonblocking sync
|
sendQueue: make(chan outMsg, 1), // nonblocking sync
|
||||||
|
|
Loading…
Add table
Reference in a new issue