wgengine/filter: preallocate some hot slices in MatchesFromFilterRules (#4672)

Profiling identified this as a fairly hot path for growing a slice.

Given this is only used in control & when a new packet filter is received, this shouldnt be hot in the client.
This commit is contained in:
Tom
2022-05-13 13:56:53 -07:00
committed by GitHub
parent c48513b2be
commit 9343967317
2 changed files with 15 additions and 3 deletions

View File

@@ -28,7 +28,14 @@ func MatchesFromFilterRules(pf []tailcfg.FilterRule) ([]Match, error) {
var erracc error
for _, r := range pf {
m := Match{}
// Profiling determined that this function was spending a lot
// of time in runtime.growslice. As such, we attempt to
// pre-allocate some slices. Multipliers were chosen arbitrarily.
m := Match{
Srcs: make([]netaddr.IPPrefix, 0, len(r.SrcIPs)),
Dsts: make([]NetPortRange, 0, 2*len(r.DstPorts)),
Caps: make([]CapMatch, 0, 3*len(r.CapGrant)),
}
if len(r.IPProto) == 0 {
m.IPProto = append([]ipproto.Proto(nil), defaultProtos...)