p2p/discover: optimize findnodeByID

This commit is contained in:
cuiweixie 2025-11-16 19:26:18 +08:00
parent 2a4847a7d1
commit 15ada877cf
No known key found for this signature in database
GPG key ID: 16DF64EE15E495A3

View file

@ -254,11 +254,29 @@ func (tab *Table) findnodeByID(target enode.ID, nresults int, preferLive bool) *
// is O(tab.len() * nresults).
nodes := &nodesByDistance{target: target}
liveNodes := &nodesByDistance{target: target}
for _, b := range &tab.buckets {
for _, n := range b.entries {
nodes.push(n.Node, nresults)
if preferLive && n.isValidatedLive {
liveNodes.push(n.Node, nresults)
var liveNodesFound = false
if preferLive {
for _, b := range &tab.buckets {
for _, n := range b.entries {
if n.isValidatedLive {
liveNodesFound = true
break
}
}
}
}
if liveNodesFound {
for _, b := range &tab.buckets {
for _, n := range b.entries {
if n.isValidatedLive {
liveNodes.push(n.Node, nresults)
}
}
}
} else {
for _, b := range &tab.buckets {
for _, n := range b.entries {
nodes.push(n.Node, nresults)
}
}
}