tsweb: remove allocs introduced by earlier change

This removes the ~9 allocs added by #5869, while still keeping struct
fields sorted (the previous commit's tests still pass). And add a test
to lock it in that this shouldn't allocate.

Updates #5778

Change-Id: I4c12b9e2a1334adc1ea5aba1777681cb9fc18fbf
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-10-10 09:56:26 -07:00
committed by Brad Fitzpatrick
parent 529e893f70
commit 718914b697
2 changed files with 76 additions and 28 deletions

View File

@ -14,6 +14,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
@ -726,3 +727,19 @@ func TestPort80Handler(t *testing.T) {
})
}
}
func TestSortedStructAllocs(t *testing.T) {
f := reflect.ValueOf(struct {
Foo int
Bar int
Baz int
}{})
n := testing.AllocsPerRun(1000, func() {
foreachExportedStructField(f, func(fieldOrJSONName, metricType string, rv reflect.Value) {
// Nothing.
})
})
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
}
}