Răsfoiți Sursa

util/deephash: avoid variadic argument for Update (#5372)

Hashing []any is slow since hashing of interfaces is slow.
Hashing of interfaces is slow since we pessimistically assume
that cycles can occur through them and start cycle tracking.

Drop the variadic signature of Update and fix callers to pass in
an anonymous struct so that we are hashing concrete types
near the root of the value tree.

Signed-off-by: Joe Tsai <[email protected]>

Signed-off-by: Joe Tsai <[email protected]>
Joe Tsai 3 ani în urmă
părinte
comite
32a1a3d1c0
3 a modificat fișierele cu 24 adăugiri și 10 ștergeri
  1. 9 1
      ipn/ipnlocal/local.go
  2. 5 6
      util/deephash/deephash.go
  3. 10 3
      wgengine/userspace.go

+ 9 - 1
ipn/ipnlocal/local.go

@@ -1180,7 +1180,15 @@ func (b *LocalBackend) updateFilterLocked(netMap *netmap.NetworkMap, prefs *ipn.
 		sshPol = *netMap.SSHPolicy
 	}
 
-	changed := deephash.Update(&b.filterHash, haveNetmap, addrs, packetFilter, localNets.Ranges(), logNets.Ranges(), shieldsUp, sshPol)
+	changed := deephash.Update(&b.filterHash, &struct {
+		HaveNetmap  bool
+		Addrs       []netip.Prefix
+		FilterMatch []filter.Match
+		LocalNets   []netipx.IPRange
+		LogNets     []netipx.IPRange
+		ShieldsUp   bool
+		SSHPolicy   tailcfg.SSHPolicy
+	}{haveNetmap, addrs, packetFilter, localNets.Ranges(), logNets.Ranges(), shieldsUp, sshPol})
 	if !changed {
 		return
 	}

+ 5 - 6
util/deephash/deephash.go

@@ -188,14 +188,13 @@ func HasherForType[T any]() func(T) Sum {
 }
 
 // Update sets last to the hash of v and reports whether its value changed.
-func Update(last *Sum, v ...any) (changed bool) {
+func Update(last *Sum, v any) (changed bool) {
 	sum := Hash(v)
-	if sum == *last {
-		// unchanged.
-		return false
+	changed = sum != *last
+	if changed {
+		*last = sum
 	}
-	*last = sum
-	return true
+	return changed
 }
 
 var appenderToType = reflect.TypeOf((*appenderTo)(nil)).Elem()

+ 10 - 3
wgengine/userspace.go

@@ -704,8 +704,12 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[key.Node
 	}
 	e.lastNMinPeers = len(min.Peers)
 
-	if !deephash.Update(&e.lastEngineSigTrim, &min, trimmedNodes, trackNodes, trackIPs) {
-		// No changes
+	if changed := deephash.Update(&e.lastEngineSigTrim, &struct {
+		WGConfig     *wgcfg.Config
+		TrimmedNodes map[key.NodePublic]bool
+		TrackNodes   []key.NodePublic
+		TrackIPs     []netip.Addr
+	}{&min, trimmedNodes, trackNodes, trackIPs}); !changed {
 		return nil
 	}
 
@@ -856,7 +860,10 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config,
 	isSubnetRouterChanged := isSubnetRouter != e.lastIsSubnetRouter
 
 	engineChanged := deephash.Update(&e.lastEngineSigFull, cfg)
-	routerChanged := deephash.Update(&e.lastRouterSig, routerCfg, dnsCfg)
+	routerChanged := deephash.Update(&e.lastRouterSig, &struct {
+		RouterConfig *router.Config
+		DNSConfig    *dns.Config
+	}{routerCfg, dnsCfg})
 	if !engineChanged && !routerChanged && listenPort == e.magicConn.LocalPort() && !isSubnetRouterChanged {
 		return ErrNoChanges
 	}