wgengine/filter/filtertype: make Match.IPProto a view

I noticed we were allocating these every time when they could just
share the same memory. Rather than document ownership, just lock it
down with a view.

I was considering doing all of the fields but decided to just do this
one first as test to see how infectious it became.  Conclusion: not
very.

Updates #cleanup (while working towards tailscale/corp#20514)

Change-Id: I8ce08519de0c9a53f20292adfbecd970fe362de0
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2024-06-18 12:05:34 -07:00
committed by Brad Fitzpatrick
parent bfb775ce62
commit bd93c3067e
8 changed files with 26 additions and 24 deletions

View File

@@ -23,6 +23,8 @@ var defaultProtos = []ipproto.Proto{
ipproto.ICMPv6,
}
var defaultProtosView = views.SliceOf(defaultProtos)
// MatchesFromFilterRules converts tailcfg FilterRules into Matches.
// If an error is returned, the Matches result is still valid,
// containing the rules that were successfully converted.
@@ -41,14 +43,15 @@ func MatchesFromFilterRules(pf []tailcfg.FilterRule) ([]Match, error) {
}
if len(r.IPProto) == 0 {
m.IPProto = append([]ipproto.Proto(nil), defaultProtos...)
m.IPProto = defaultProtosView
} else {
m.IPProto = make([]ipproto.Proto, 0, len(r.IPProto))
filtered := make([]ipproto.Proto, 0, len(r.IPProto))
for _, n := range r.IPProto {
if n >= 0 && n <= 0xff {
m.IPProto = append(m.IPProto, ipproto.Proto(n))
filtered = append(filtered, ipproto.Proto(n))
}
}
m.IPProto = views.SliceOf(filtered)
}
for i, s := range r.SrcIPs {