From 6ef734e4933234b4c279d8cd756881a0912d40fb Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 18 Aug 2021 15:06:40 -0700 Subject: [PATCH] wgengine: predict min.Peers length across calls The number of peers we have will be pretty stable across time. Allocate roughly the right slice size. This reduces memory usage when there are many peers. Signed-off-by: Josh Bleecher Snyder --- wgengine/userspace.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wgengine/userspace.go b/wgengine/userspace.go index b301c7b94..bc8b92e66 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -108,6 +108,7 @@ type userspaceEngine struct { wgLock sync.Mutex // serializes all wgdev operations; see lock order comment below lastCfgFull wgcfg.Config + lastNMinPeers int lastRouterSig deephash.Sum // of router.Config lastEngineSigFull deephash.Sum // of full wireguard config lastEngineSigTrim deephash.Sum // of trimmed wireguard config @@ -606,7 +607,7 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[key.Publ // based on the full config. Prune off all the peers // and only add the active ones back. min := full - min.Peers = nil + min.Peers = make([]wgcfg.Peer, 0, e.lastNMinPeers) // We'll only keep a peer around if it's been active in // the past 5 minutes. That's more than WireGuard's key @@ -650,6 +651,7 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[key.Publ trimmedDisco[dk] = true } } + e.lastNMinPeers = len(min.Peers) if !deephash.Update(&e.lastEngineSigTrim, &min, trimmedDisco, trackDisco, trackIPs) { // No changes