Optimize peer ranking paths in prioritized peers and worse connections#1045
Open
Sahil-4555 wants to merge 2 commits into
Open
Optimize peer ranking paths in prioritized peers and worse connections#1045Sahil-4555 wants to merge 2 commits into
Sahil-4555 wants to merge 2 commits into
Conversation
Owner
|
Can you include a benchmark? |
Author
PrioritizedPeers Benchmarkfunc benchmarkPrioritizedPeers(n int, addrForIndex func(int) PeerRemoteAddr) []PeerInfo {
peers := make([]PeerInfo, n)
for i := range peers {
peers[i] = PeerInfo{
Addr: addrForIndex(i),
Trusted: i%7 == 0,
}
}
return peers
}
func BenchmarkPrioritizedPeersAdd(b *testing.B) {
benchmarks := []struct {
name string
addrForIndex func(int) PeerRemoteAddr
}{
{
name: "IPv4IPPortAddr",
addrForIndex: func(i int) PeerRemoteAddr {
return ipPortAddr{
IP: net.IPv4(192, 0, 2, byte((i%250)+1)),
Port: 10000 + i,
}
},
},
{
name: "IPv6IPPortAddr",
addrForIndex: func(i int) PeerRemoteAddr {
return ipPortAddr{
IP: net.ParseIP(fmt.Sprintf("2001:db8::%x", i+1)),
Port: 10000 + i,
}
},
},
{
name: "StringAddr",
addrForIndex: func(i int) PeerRemoteAddr {
return StringAddr(
net.JoinHostPort(
net.IPv4(192, 0, 2, byte((i%250)+1)).String(),
fmt.Sprintf("%d", 10000+i),
),
)
},
},
{
name: "NetipAddrPort",
addrForIndex: func(i int) PeerRemoteAddr {
return netip.AddrPortFrom(
netip.AddrFrom4([4]byte{192, 0, 2, byte((i % 250) + 1)}),
uint16(10000+i),
)
},
},
}
for _, bench := range benchmarks {
b.Run(bench.name, func(b *testing.B) {
peers := benchmarkPrioritizedPeers(512, bench.addrForIndex)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
pp := prioritizedPeers{
om: btree.New(8),
getPrio: func(p PeerInfo) peerPriority {
return peerPriority(len(p.Addr.String()))
},
}
for _, p := range peers {
pp.Add(p)
}
}
})
}
}WorseConn Benchmarkfunc BenchmarkWorseConnLess(b *testing.B) {
b.Run("BadDirection", func(b *testing.B) {
left := worseConnInput{
BadDirection: true,
GetPeerPriority: func() (peerPriority, error) { return 5, nil },
Pointer: 1,
}
right := worseConnInput{
GetPeerPriority: func() (peerPriority, error) { return 7, nil },
Pointer: 2,
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if !left.Less(&right) {
b.Fatal("unexpected ordering")
}
}
})
b.Run("TimestampTieBreak", func(b *testing.B) {
left := worseConnInput{
LastHelpful: time.Unix(100, 0),
GetPeerPriority: func() (peerPriority, error) { return 5, nil },
Pointer: 1,
}
right := worseConnInput{
LastHelpful: time.Unix(101, 0),
GetPeerPriority: func() (peerPriority, error) { return 7, nil },
Pointer: 2,
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if !left.Less(&right) {
b.Fatal("unexpected ordering")
}
}
})
b.Run("PeerPriority", func(b *testing.B) {
left := worseConnInput{
GetPeerPriority: benchmarkMemoizedPeerPriority(func() (peerPriority, error) { return 5, nil }),
Pointer: 1,
}
right := worseConnInput{
GetPeerPriority: benchmarkMemoizedPeerPriority(func() (peerPriority, error) { return 7, nil }),
Pointer: 2,
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if !left.Less(&right) {
b.Fatal("unexpected ordering")
}
}
})
}
func benchmarkMemoizedPeerPriority(f func() (peerPriority, error)) func() (peerPriority, error) {
var once sync.Once
var prio peerPriority
var err error
return func() (peerPriority, error) {
once.Do(func() {
prio, err = f()
})
return prio, err
}
}Result |
Author
Done. Added |
Owner
|
Nice |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This update affects two ranking-heavy paths: prioritized peer insertion and worse-connection selection.