make linear complexity instead of embedded loops#579
Merged
oleg-kushniriov merged 1 commit intoMay 11, 2026
Merged
Conversation
85f522c to
73748d8
Compare
danbar2
approved these changes
May 7, 2026
73748d8 to
5b185a7
Compare
5b185a7 to
6fbc871
Compare
enoodle
approved these changes
May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
Linearizes nine O(N×M) loops in the operator's reconcile path by replacing per-element
slices.Contains/slices.IndexFunc/lo.Filter-with-inner-Containslookups withmap[string]struct{}(ormap[string]T) sets/lookups built once per sync context. Same shape across allnine sites; no behavior change.
Affected call sites (each was O(M·E), is now O(M+E)):
controller/podcliqueset/components/podgang/syncflow.goinitializeAssignedAndUnassignedPodsForPCScontroller/podclique/components/pod/syncflow.gocheckAndRemovePodSchedulingGatescontroller/podclique/components/pod/rollingupdate.gogetPodNamesPendingUpdatecontroller/podcliquescalinggroup/components/podclique/sync.gocreateExpectedPCLQs,createOrUpdatePCLQscontroller/podcliqueset/components/podclique/podclique.gocreateOrUpdatePCLQscontroller/podcliqueset/components/podcliquescalinggroup/podcliquescalinggroup.goSync(existence check + excess filter)Affected call sites (each was O(M·E), is now O(M+E)):
controller/podcliqueset/components/podgang/syncflow.goinitializeAssignedAndUnassignedPodsForPCScontroller/podclique/components/pod/syncflow.gocheckAndRemovePodSchedulingGatescontroller/podclique/components/pod/rollingupdate.gogetPodNamesPendingUpdatecontroller/podcliquescalinggroup/components/podclique/sync.gocreateExpectedPCLQs,createOrUpdatePCLQscontroller/podcliqueset/components/podclique/podclique.gocreateOrUpdatePCLQscontroller/podcliqueset/components/podcliquescalinggroup/podcliquescalinggroup.goSync(existence check + excess filter)controller/podcliqueset/components/podgang/syncflow.gogetPodsForPodCliquesPendingCreationcontroller/podcliqueset/components/podgang/syncflow.gogetPodGangNamesPendingCreation,getExcessPodGangNamescontroller/podcliqueset/components/podgang/syncflow.gogetPodCliques,determinePodCliqueReplicas,determinePCSGReplicasIntroduces a small generic utility at
internal/controller/common/component/utils/:sets.go—Set[T]/NewSet/NewSetBy/Hasfor membership-only lookups;MapByreturningmap[K]Vfor value-retrieval lookups.lookups.go— typed convenience helpers (PodCliqueByName,PodCliqueNameSet,PCSGByName,PodGangByName) over Grove's common resource slices, since these views are needed in multiple controllers.The fix matters at modest scale, not just at the upper bound: at ~1 000 Pods (well below Grove's ~10 000-Pod target) the PCLQ-heavy shape produces low-millions of comparisons per reconcile plus per-element slice allocations on every hot path. CPU and GC pressure don't show in
dev/CI because dev clusters typically run at N≈10. At N=10 000 these patterns produce 10⁸-class per-reconcile work — the regime where reconcile latencies climb to seconds and watch-event storms cascade.
Which issue(s) this PR fixes:
Fixes #577
Special notes for your reviewer:
map[string]struct{}(ormap[string]T) out of the inner loop, do a single map lookup per element. No semantic changes.*ByName/*NameSetfields onsyncContextare populated up front inprepareSyncFlow(orprepareSyncContext). I deliberately did not add lazy-init getters — they would either be dead code (callers always go throughprepareSyncFlow) or a latent data race (ifsyncContextis ever shared across goroutines). Twosyncflow_test.gofixtures were updated to populate the new fields explicitly using the hoisted helpers — this is exactly the kind of mismatch the eager-only design is meant tosurface.
Set[T]keeps.Has,map[K]Vdoesn't get a wrapper.Set[T]uses a sentinelstruct{}value, so theHasmethod is genuinely useful. Formap[K]Vlookups, the natural_, ok := m[k]idiom is used everywhere — noLookup[K, V]alias.initializeAssignedAndUnassignedPodsForPCSrelies onexpectedPodGangs []*podGangInfobeing a slice of pointers — theexpectedPodGangByNamemap values alias the same structs, sopgi.refreshAssociatedPCLQPods(...)mutates back into the slice. There's aninline comment documenting this; a future refactor that switches to value-typed entries would silently break it.
Test_ScaleTest_1000_PodHeavy(1 PCS × 1 PCLQ × 1 000 pods) added toe2e/tests/scale/. Companion to the existingTest_ScaleTest_1000(500 PCLQs × 2 pods, PCLQ-heavy). The two together cover both hotspot axes: PCLQ-heavy hits hotspots 1, 4, 5, 7, 8,9; Pod-heavy hits 2, 3. Run with
make run-scale-test TEST_PATTERN=Test_ScaleTest_1000_PodHeavyagainstmake scale-cluster-up. Diffscale-test-results.jsonand the steady-state-reconcile pprof captures between baseline and this branch for the perf evidence.sets_test.gocoversNewSet(empty/populated/dups),NewSetBy(keyFunc + dedup),Set.Has(incl. zero-value),MapBy(empty/populated/last-write-wins). All existing controller tests pass. Full unit-test suite +go vet(default +-tags=e2e) +make lintclean.
internal/controller/common/component/utils/sets.go,lookups.go(the new abstraction), andcontroller/podcliqueset/components/podgang/syncflow.go(largest delta — 4 hotspots fixed in one file, syncContext shape changed). The rest are 5–10-linemechanical applications of the same pattern.
Does this PR introduce a API change?