Skip to content

Commit

Permalink
featuregate: clone queriedFeatures only when mutation is needed
Browse files Browse the repository at this point in the history
Avoid allocating memory when cloned set of queried features is not necessary

Kubernetes-commit: 77e84efe31863c690da7f34ead7411cbb81b9870
  • Loading branch information
vrutkovs authored and k8s-publishing-bot committed Jul 19, 2024
1 parent 6f32dbe commit f9413de
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions featuregate/feature_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/naming"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/version"
featuremetrics "k8s.io/component-base/metrics/prometheus/feature"
baseversion "k8s.io/component-base/version"
Expand Down Expand Up @@ -265,7 +266,7 @@ func NewVersionedFeatureGate(emulationVersion *version.Version) *featureGate {
f.enabled.Store(map[Feature]bool{})
f.enabledRaw.Store(map[string]bool{})
f.emulationVersion.Store(emulationVersion)
f.queriedFeatures.Store(map[Feature]struct{}{})
f.queriedFeatures.Store(sets.Set[Feature]{})
klog.V(1).Infof("new feature gate with emulationVersion=%s", f.emulationVersion.Load().String())
return f
}
Expand Down Expand Up @@ -530,7 +531,7 @@ func (f *featureGate) SetEmulationVersion(emulationVersion *version.Version) err
enabled := map[Feature]bool{}
errs := f.unsafeSetFromMap(enabled, enabledRaw, emulationVersion)

queriedFeatures := f.queriedFeatures.Load().(map[Feature]struct{})
queriedFeatures := f.queriedFeatures.Load().(sets.Set[Feature])
known := f.known.Load().(map[Feature]VersionedSpecs)
for feature := range queriedFeatures {
newVal := featureEnabled(feature, enabled, known, emulationVersion)
Expand All @@ -544,7 +545,7 @@ func (f *featureGate) SetEmulationVersion(emulationVersion *version.Version) err
// Persist changes
f.enabled.Store(enabled)
f.emulationVersion.Store(emulationVersion)
f.queriedFeatures.Store(map[Feature]struct{}{})
f.queriedFeatures.Store(sets.Set[Feature]{})
}
return utilerrors.NewAggregate(errs)
}
Expand All @@ -564,15 +565,14 @@ func (f *featureGate) featureSpec(key Feature) (FeatureSpec, error) {
}

func (f *featureGate) unsafeRecordQueried(key Feature) {
queriedFeatures := map[Feature]struct{}{}
for k := range f.queriedFeatures.Load().(map[Feature]struct{}) {
queriedFeatures[k] = struct{}{}
}
queriedFeatures := f.queriedFeatures.Load().(sets.Set[Feature])
if _, ok := queriedFeatures[key]; ok {
return
}
queriedFeatures[key] = struct{}{}
f.queriedFeatures.Store(queriedFeatures)
// Clone items from queriedFeatures before mutating it
newQueriedFeatures := queriedFeatures.Clone()
newQueriedFeatures.Insert(key)
f.queriedFeatures.Store(newQueriedFeatures)
}

func featureEnabled(key Feature, enabled map[Feature]bool, known map[Feature]VersionedSpecs, emulationVersion *version.Version) bool {
Expand Down Expand Up @@ -700,7 +700,7 @@ func (f *featureGate) DeepCopy() MutableVersionedFeatureGate {
fg.known.Store(known)
fg.enabled.Store(enabled)
fg.enabledRaw.Store(enabledRaw)
fg.queriedFeatures.Store(map[Feature]struct{}{})
fg.queriedFeatures.Store(sets.Set[Feature]{})
return fg
}

Expand Down

0 comments on commit f9413de

Please sign in to comment.