Optimize DDAI profile merge and reconcile paths #3853
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #3853"
general
weight: 0.35
tbavelier
<!-- dd-meta {"pullId":"00000000-0000-0000-0000-000000000000","source":"chat","resourceId":"08d9e3d9-7de6-48d3-9a34-6e78de6ba0c9","workflowId":"81140062-eaff-4824-968b-a6110b243dfe","codeChangeId":"81140062-eaff-4824-968b-a6110b243dfe","sourceType":"chat"} -->
What does this PR do?
Speeds up the DatadogAgent reconcile hot paths, mainly DDAI profile merging. The profile merge is intentionally the complex part; the other changes are kept small.
1. DDAI profile merge (
internal/controller/datadogagent/merge.go,profile.go,controller.go)- Calls
TypedValue.Mergedirectly when objects have no populated ManagedFields, skipping the ManagedFields tracking inmanagedfields.FieldManager.Applythat we don't use. Outdated-CRD unknown-field stripping and its logging are unchanged.- Checks the base DDAI against the installed CRD schema once per batch, then merges only the spec subtrees each profile touches and copies the rest directly. The synthetic default profile skips the structured merge entirely.
- Adds a steady-state merge cache on the Reconciler. Entries are checked against the CRD field manager, a fingerprint of the source DDAI and a content hash of the profile. They're evicted when a profile goes away, and callers get DeepCopies. The fingerprint functions document that they must cover every input the merge reads.
- On my machine, 20 profiles take ~4.5 ms cold and ~0.2 ms in steady state (
BenchmarkApplyProfilesToDDAISpec_20Profiles_).2. Profile reconcile (
reconcile_agent.go,profile.go,pkg/agentprofile/agent_profile.go,feature/overlay.go)-
labelNodesWithProfilesreuses the node list that's already fetched instead of fetching each node: ~1.8 ms → ~23 µs for 200 nodes in steady state.- Index-based loops instead of copying Node/Pod/Profile values; overlay registration order is sorted once at registration.
3. Pod mergers (
merger/envvars.go,envvarfrom.go,port.go,volume.go,volume_mount.go): index-based loops, so the merge function gets a pointer into the slice instead of a heap-escaping copy of the loop variable.4.
comparison.GenerateMD5ForSpec: pooled JSON encoding buffer. Hashes are unchanged; ~80% fewer bytes allocated per DatadogAgentSpec hash (1780 → 336 B/op), same speed.5.
utils.IsAboveMinVersion: caches the parsed>= minVersionconstraint. Every caller passes a package constant, so the cache stays small: ~27 µs → ~5.5 µs and 120 → 26 allocs for 6 checks.Motivation
Production eBPF CPU profiling across the datadog-operator fleet showed
ssaMergeCRDat over 50% of profiled operator CPU, also driving GC overhead, with ~20 profile merges per DatadogAgent reconcile. The other changes target the next user-code hotspots in the same profiles.Additional Notes
Validation on nightly clusters
Commit
2159d27bran on 8 operator nightly clusters, starting around 2026-11-26 13:30 UTC, for a ~19h soak.oddish-bstayed onmainas a matched control foroddish-c: before the rollout, their CPU, reconcile latency and allocation rate were within ~2% of each other.| Signal | Before (
main) | After (this PR) | Delta ||--|--|--|--|
| Container CPU,
oddish-cvsoddish-b(19h soak) | 28.11 mCPU | 9.64 mCPU | -65.7% || Container CPU, per updated cluster (19h soak vs each cluster's baseline) | 20.7–30.6 mCPU | 6.9–9.7 mCPU | -61% to -69% |
|
datadogagentreconcile latency,oddish-cvsoddish-b(19h soak) | 296.5 ms | 46.4 ms | -84.4% ||
datadogagentreconcile latency, per updated cluster (initial steady state) | 290–363 ms | 18.6–60.8 ms | -79% to -94% || Fleet eBPF CPU (
ebpf-cpu-time) | 1,577 ms/min | 524 ms/min | -66.8% || ↳
managedfieldsstructuredMergeManager.Apply+Update| 603 ms/min (38%) | not in top stacks | gone || ↳
override.PodTemplateSpec(pod mergers) | ~36.6 ms/min | ~4.7 ms/min | -87% || Go profiler CPU (
empoleon-a) | 1,310 ms/min | 532 ms/min | -59.4% (Reconcile-79.7%) || Go alloc bytes / alloc count (
empoleon-a) | 229 MiB/min / 2.8M/min | 102 MiB/min / 711k/min | -55.7% / -74.7% || Go live heap (
empoleon-a) | 79.3 MiB | 73.1 MiB | -7.8% |<details>
<summary>Per-cluster results (baseline → initial steady state → 19h soak)</summary>
| Cluster | Container CPU (mCPU) |
datadogagentreconcile latency (ms) | Working set (MiB) | 19h restarts ||--|--|--|--|--|
|
oddish-b(control,main) | 31.07 → 27.50 → 28.11 | 286.7 → 287.1 → 296.5 | 155.2 → 158.8 → 157.9 | 0 ||
oddish-c| 30.63 → 11.84 → 9.64 (-68.5%) | 289.7 → 60.8 → 46.4 (-84.0%) | 159.0 → 143.5 → 145.6 | 0 ||
empoleon-a| 23.55 → 8.28 → 9.20 (-60.9%) | 349.0 → 26.4 → 15.2 (-95.6%) | 167.1 → 163.6 → 164.1 | 0 ||
diglet| 20.70 → 7.16 → 6.90 (-66.7%) | 317.1 → 28.6 → 121.0 (19h avg incl. spikes) | 190.5 → 198.6 → 196.6 | 0 ||
lagaffe| 21.62 → 7.37 → 7.94 (-63.3%) | 315.7 → 33.2 → 195.7 (19h avg incl. spikes) | 166.9 → 162.0 → 163.5 | 0 ||
squirtle-a| 22.18 → 8.11 → 7.93 (-64.2%) | 359.3 → 20.6 → 29.6 (-91.8%) | 114.9 → 107.6 → 109.1 | 0 ||
snowver| 20.69 → 8.12 → 8.04 (-61.1%) | 310.1 → 18.6 → 20.2 (-93.5%) | 123.3 → 121.6 → 126.6 | 0 ||
entei| 25.78 → 10.33 → 9.72 (-62.3%) | 362.7 → 41.6 → 37.1 (-89.8%) | 201.5 → 173.5 → 187.8 | 0 |Windows (UTC): baseline 2026-11-26 10:00–13:20, initial steady state 13:44–14:20, soak 2026-11-26 14:00 – 2026-11-27 08:43.
</details>
Metrics (container CPU, reconcile latency, memory):


Profiles (eBPF fleet CPU and Go profiler on
empoleon-a):



Stability over the 19h soak, on every updated cluster:
datadogagent,datadogagentinternalordatadogagentprofilein the initial steady state.Next hotspots:
reconcileProfiles(getNodeList,enforceCreateStrategy, profile status updates), then the DDAI DaemonSet update andannotateConfigMapsChecksum.Minimum Agent Versions
Are there minimum versions of the Datadog Agent and/or Cluster Agent required?
Agent: N/A
Describe your test plan
Checklist
bug,enhancement,refactoring,documentation,tooling, and/ordependenciesqa/skip-qalabel---
PR by Bits - View session in Datadog
🤖 Generated with Claude Code