From ca455ac84bf4dc96077e8910e7e97f2899bd5e46 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 28 May 2021 20:13:01 -0700 Subject: [PATCH] net/tsaddr: simplify TailscaleServiceIP MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit netaddr allocated at the time this was written. No longer. name old time/op new time/op delta TailscaleServiceAddr-4 5.46ns ± 4% 1.83ns ± 3% -66.52% (p=0.008 n=5+5) A bunch of the others can probably be simplified too, but this was the only one with just an IP and not an IPPrefix. Signed-off-by: Brad Fitzpatrick --- net/tsaddr/tsaddr.go | 18 +----------------- net/tsaddr/tsaddr_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/net/tsaddr/tsaddr.go b/net/tsaddr/tsaddr.go index adb73c607..78543f13d 100644 --- a/net/tsaddr/tsaddr.go +++ b/net/tsaddr/tsaddr.go @@ -41,12 +41,9 @@ var ( // TailscaleServiceIP returns the listen address of services // provided by Tailscale itself such as the MagicDNS proxy. func TailscaleServiceIP() netaddr.IP { - serviceIP.Do(func() { mustIP(&serviceIP.v, "100.100.100.100") }) - return serviceIP.v + return netaddr.IPv4(100, 100, 100, 100) // "100.100.100.100" for those grepping } -var serviceIP onceIP - // IsTailscaleIP reports whether ip is an IP address in a range that // Tailscale assigns from. func IsTailscaleIP(ip netaddr.IP) bool { @@ -126,19 +123,6 @@ type oncePrefix struct { v netaddr.IPPrefix } -func mustIP(v *netaddr.IP, ip string) { - var err error - *v, err = netaddr.ParseIP(ip) - if err != nil { - panic(err) - } -} - -type onceIP struct { - sync.Once - v netaddr.IP -} - // NewContainsIPFunc returns a func that reports whether ip is in addrs. // // It's optimized for the cases of addrs being empty and addrs diff --git a/net/tsaddr/tsaddr_test.go b/net/tsaddr/tsaddr_test.go index eebd61445..2d27d0265 100644 --- a/net/tsaddr/tsaddr_test.go +++ b/net/tsaddr/tsaddr_test.go @@ -93,3 +93,11 @@ func TestNewContainsIPFunc(t *testing.T) { t.Fatal("bad") } } + +var sinkIP netaddr.IP + +func BenchmarkTailscaleServiceAddr(b *testing.B) { + for i := 0; i < b.N; i++ { + sinkIP = TailscaleServiceIP() + } +}