Skip to content

Commit b7f0784

Browse files
committed
optimize thinwaistform so that it can be inlined
1 parent cdacc08 commit b7f0784

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

p2p/host/basic/obsaddr.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package basichost
22

33
import (
44
"context"
5-
"fmt"
5+
"errors"
66
"net"
77
"slices"
88
"sort"
@@ -32,15 +32,14 @@ type thinWaist struct {
3232
Addr, TW, Rest ma.Multiaddr
3333
}
3434

35+
var errTW = errors.New("not a thinwaist address")
36+
3537
func thinWaistForm(a ma.Multiaddr) (thinWaist, error) {
3638
if len(a) < 2 {
37-
return thinWaist{}, fmt.Errorf("not a thinwaist address: %s", a)
39+
return thinWaist{}, errTW
3840
}
39-
// we don't care about link local ipv6 addresses here
40-
hasIP := a[0].Code() == ma.P_IP4 || a[0].Code() == ma.P_IP6
41-
hasProtocol := a[1].Code() == ma.P_TCP || a[1].Code() == ma.P_UDP
42-
if !hasIP || !hasProtocol {
43-
return thinWaist{}, fmt.Errorf("not a thinwaist address: %s", a)
41+
if c0, c1 := a[0].Code(), a[1].Code(); (c0 != ma.P_IP4 && c0 != ma.P_IP6) || (c1 != ma.P_TCP && c1 != ma.P_UDP) {
42+
return thinWaist{}, errTW
4443
}
4544
return thinWaist{Addr: a, TW: a[:2], Rest: a[2:]}, nil
4645
}
@@ -503,14 +502,14 @@ func (o *ObservedAddrsManager) Close() error {
503502
return nil
504503
}
505504

506-
// hasConsistentTransport returns true if the address `a` shares the same
507-
// protocols with `b`
508-
func hasConsistentTransport(a, b ma.Multiaddr) bool {
509-
if len(a) != len(b) {
505+
// hasConsistentTransport returns true if the thin waist address `aTW` shares the same
506+
// protocols with `bTW`
507+
func hasConsistentTransport(aTW, bTW ma.Multiaddr) bool {
508+
if len(aTW) != len(bTW) {
510509
return false
511510
}
512-
for i, ac := range a {
513-
if b[i].Code() != ac.Code() {
511+
for i, a := range aTW {
512+
if bTW[i].Code() != a.Code() {
514513
return false
515514
}
516515
}

0 commit comments

Comments
 (0)