]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Calculate peer priority lazily for worse conn comparison
authorMatt Joiner <anacrolix@gmail.com>
Sun, 15 Jul 2018 02:57:52 +0000 (12:57 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 15 Jul 2018 02:57:52 +0000 (12:57 +1000)
multiless.go
worst_conns.go

index 4a48393757f9a2f8a218068b9d2e1ddc57e3cfbb..20ba2bd7255323cf09d2125587fd20d03b7be70f 100644 (file)
@@ -24,17 +24,22 @@ func (me *multiLess) FinalOk() (left, ok bool) {
 }
 
 func (me *multiLess) Next(f cmper) {
-       me.StrictNext(f())
-}
-
-func (me *multiLess) StrictNext(same, less bool) {
        if me.ok {
                return
        }
+       same, less := f()
        if same {
                return
        }
-       me.ok, me.less = true, less
+       me.ok = true
+       me.less = less
+}
+
+func (me *multiLess) StrictNext(same, less bool) {
+       if me.ok {
+               return
+       }
+       me.Next(func() (bool, bool) { return same, less })
 }
 
 func (me *multiLess) NextBool(l, r bool) {
index ad87d1b38f9613791a22eb06016a02f4d93f11f5..d67a52f1f4aedbcd3d84e270c5d92e0574ed6be9 100644 (file)
@@ -15,7 +15,9 @@ func worseConn(l, r *connection) bool {
        ml.StrictNext(
                l.completedHandshake.Equal(r.completedHandshake),
                l.completedHandshake.Before(r.completedHandshake))
-       ml.StrictNext(l.peerPriority() == r.peerPriority(), l.peerPriority() < r.peerPriority())
+       ml.Next(func() (bool, bool) {
+               return l.peerPriority() == r.peerPriority(), l.peerPriority() < r.peerPriority()
+       })
        ml.StrictNext(l == r, uintptr(unsafe.Pointer(l)) < uintptr(unsafe.Pointer(r)))
        less, ok := ml.FinalOk()
        if !ok {