Awesome Reviewers

When a change affects a scheduler/kubelet hot path, avoid quadratic or repeated work: do expensive filtering/KeepOnly/sorting only in the specific variant that needs it, and precompute indexes/sorted structures once outside inner loops. If the faster algorithm depends on input sizes, make iteration strategy adaptive to prevent worst-case slowdowns, and add/adjust benchmarks to confirm throughput or latency improvements.

Do:

Example (gating expensive work to a resize-only path):

func (a *acc) take(cpus cpuset.CPUSet) {
    a.result = a.result.Union(cpus)

    // Only do topology-wide/KeepOnly work when this accumulator is in resize mode.
    if a.resultDetails != nil {
        for _, cpu := range cpus.UnsortedList() {
            if info, ok := a.topo.CPUDetails[cpu]; ok {
                a.resultDetails[cpu] = info
            }
        }
        // KeepOnly should be applied only in resize variants.
        a.resultDetails = a.topo.CPUDetails.KeepOnly(a.result)
    }
}

Example (remove repeated sorts by sorting once during state construction):

Outcome: lower CPU usage in inner loops, less allocation churn, and fewer performance regressions across different real-world distributions.