Skip to content

perf(xds): memoize the dependency set behind an input generation counter (#539) - #549

Merged
bpalermo merged 1 commit into
mainfrom
perf/539-dependency-set-memo
Jul 19, 2026
Merged

bpalermo merged 1 commit into
mainfrom
perf/539-dependency-set-memo

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Fixes #539

What

dependencySetLocked() rebuilt the node dependency set from scratch on every call — per pod event, per GAMMA/L4 update, per refresh, plus the extra before/after diff calls in the change-signal paths (setPodDependencies, removePodDependencies, SetStaticDependencies, ObserveDependency). This memoizes the build behind an input generation counter (the safer of the two approaches in the issue — no hand-maintained incremental deltas).

How

  • depGen (guarded by depMu) counts input mutations. Rule kept trivially auditable: every write under depMu bumps via bumpDepGenLocked(), even for fields the rebuild does not read today (routeTargetPorts, serviceInboundFilters) — over-invalidation costs one recompute, a missed bump serves a stale set (the demand-scoping bug class).
  • Mutators wired: setPodDependencies, removePodDependencies, ObserveDependency (incl. pure timestamp refresh), PruneObservedDependencies (when entries expired), SetStaticDependencies, SetCaptureTCPServices, SetServiceRoutes, SetImportedServiceRoutes, SetRouteTargetPorts, SetServiceChainFilters, SetImportedServiceChainFilters, SetServiceInboundFilters, SetTCPServiceRoutes, SetTLSServiceRoutes, SetUDPServiceRoutes, plus the declareDeps test helper.
  • TTL edge: observed (ODCDS) dependencies expire by wall clock at read time, without any mutator running — a purely generation-keyed cache would serve expired entries until the next prune. The memo therefore also records the earliest observed-entry expiry (depSetExpiry, exact boundary now <= last+ttl) and rebuilds once that instant passes. PruneObservedDependencies stays the deletion point and bumps like any mutator; when entries expire is unchanged. The observedTTL test hook is part of the memo key too (depSetTTL).
  • Sharing/copies: dependencySetLocked returns the shared memoized map (documented read-only; a rebuild always allocates a fresh map, so the internal before/after diff paths still compare distinct sets). DependencySet() now takes the write lock (a call may store the memo) and returns a copy, preserving the old caller-owns-the-map contract.

Tests

New dependencies_memo_test.go:

  • per-mutator invalidation coverage (pod add/remove, observe, prune, static, capture-TCP, local+imported GAMMA routes incl. backends, local+imported chain filters, TCP/TLS/UDP L4 route backends)
  • TTL expiry with no mutator running (wall-clock invalidation), and a refresh-extends-TTL case (touch bumps the horizon)
  • repeated calls without mutations return equal content; mutating a returned copy does not leak into the memo
  • memo identity: same map served without mutation, new map after a bump

bazel test //agent/... green (23/23); //agent/internal/xds/cache:cache_test also green with --@rules_go//go/config:race=true.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

…ter (#539)

dependencySetLocked() rebuilt the node dependency set from scratch on
every call — per pod event, per GAMMA/L4 update, per refresh, plus the
before/after diff calls in the change-signal paths. Memoize it on an
input generation counter (depGen): every writer of any depMu-guarded
input bumps it via bumpDepGenLocked, and an unchanged generation serves
the cached set.

Observed (ODCDS) dependencies expire by wall clock at read time without
any mutator running, so the memo also records the earliest observed-
entry expiry (depSetExpiry) and rebuilds once that instant passes —
expiry semantics are byte-identical to the from-scratch build; prune
remains a plain mutator that bumps like the rest.

DependencySet() now takes the write lock (a hit may store the memo) and
hands external callers a copy, so the shared memoized map can never be
mutated; the internal before/after diff paths keep distinct sets because
the rebuild after a bump allocates a fresh map.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR
@bpalermo
bpalermo merged commit 3a2236f into main Jul 19, 2026
14 checks passed
@bpalermo
bpalermo deleted the perf/539-dependency-set-memo branch July 19, 2026 14:37
bpalermo added a commit that referenced this pull request Jul 19, 2026
…te time (#540) (#551)

Two per-snapshot recomputations of write-time-stable data in the xDS cache
are now memoized on generation counters (the #549 depGen idiom):

- serviceRoutesSnapshot: the effective (local ∪ imported) GAMMA rules with
  unavailable extension filters stripped were merged + strip-scanned (and
  copied) on EVERY snapshot. Now memoized on depGen; SetServiceRoutes /
  SetImportedServiceRoutes invalidate via their existing bump, and
  SetAuthzSidecar bumps too since authz availability changes the strip
  result.

- routeTargetDomains: the cap_http host-match domain lists were rebuilt via
  fmt.Sprintf in nested loops (base names × ports) per route target on every
  snapshot. Now precomputed into a routeDomains memo keyed on depGen (routes
  + routeTargetPorts) and a new captureAuthGen (captureAuthorities → the
  SA-backed fqdn source); captureVhosts reads the memo through
  serviceRoutesAndDomainsSnapshot, which hands back routes + domains from
  the same generation.

Behavior is byte-identical: the per-target domain algorithm is unchanged
(same domains, same order), the bare-name uniqueness guard counts over the
same set (every effective route target is unconditionally in the dependency
set), and strip semantics are untouched. No ordering semantics changed —
map iteration nondeterminism in vhost emission is pre-existing and
unaffected (lookups only; no ordering is derived from the cached maps).
Cached maps follow the depSet convention: shared, replaced never mutated.

Fixes #540


Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(xds): maintain the dependency set incrementally instead of full rebuild per event

1 participant