util/dirwalk, metrics, portlist: add new package for fast directory walking

This is similar to the golang.org/x/tools/internal/fastwalk I'd
previously written but not recursive and using mem.RO.

The metrics package already had some Linux-specific directory reading
code in it. Move that out to a new general package that can be reused
by portlist too, which helps its scanning of all /proc files:

    name                old time/op    new time/op    delta
    FindProcessNames-8    2.79ms ± 6%    2.45ms ± 7%  -12.11%  (p=0.000 n=10+10)

    name                old alloc/op   new alloc/op   delta
    FindProcessNames-8    62.9kB ± 0%    33.5kB ± 0%  -46.76%  (p=0.000 n=9+10)

    name                old allocs/op  new allocs/op  delta
    FindProcessNames-8     2.25k ± 0%     0.38k ± 0%  -82.98%  (p=0.000 n=9+10)

Change-Id: I75db393032c328f12d95c39f71c9742c375f207a
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-11-05 14:26:29 -07:00
committed by Brad Fitzpatrick
parent 21ef7e5c35
commit db2cc393af
9 changed files with 422 additions and 178 deletions

View File

@@ -136,3 +136,16 @@ func BenchmarkParsePorts(b *testing.B) {
}
}
}
func BenchmarkFindProcessNames(b *testing.B) {
b.ReportAllocs()
li := &linuxImpl{}
need := map[string]*portMeta{
"something-we'll-never-find": new(portMeta),
}
for i := 0; i < b.N; i++ {
if err := li.findProcessNames(need); err != nil {
b.Fatal(err)
}
}
}