From f22be3892f439ba7e6e73461e8e6be63c416ae63 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 15 Jul 2018 12:57:52 +1000 Subject: [PATCH] Calculate peer priority lazily for worse conn comparison --- multiless.go | 15 ++++++++++----- worst_conns.go | 4 +++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/multiless.go b/multiless.go index 4a483937..20ba2bd7 100644 --- a/multiless.go +++ b/multiless.go @@ -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) { diff --git a/worst_conns.go b/worst_conns.go index ad87d1b3..d67a52f1 100644 --- a/worst_conns.go +++ b/worst_conns.go @@ -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 { -- 2.48.1