-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not construct intermediate sets when calculating union of multiple…
… sets An AddressGroup can be shared by multiple NetworkPolicies and its span is the union of the NetworkPolicies'. The syncAddressGroup method uses "Union" method to calculate the union set. However, the method always create a new sets and copies the original items when traversing each set. This performs badly when the number of the NetworkPolicies increase and the span is big. This patch optimizes it by using a "Merge" function to avoid above cost. The benchmark impact is as below when there are 1000 NetworkPolicies sharing the AddressGroup and the span is 1000 Nodes: name old time/op new time/op delta SyncAddressGroup-48 417µs ± 4% 326µs ± 4% -21.91% (p=0.008 n=5+5) name old alloc/op new alloc/op delta SyncAddressGroup-48 98.9kB ± 0% 51.1kB ± 1% -48.27% (p=0.008 n=5+5) name old allocs/op new allocs/op delta SyncAddressGroup-48 1.05k ± 0% 0.05k ± 8% -94.78% (p=0.008 n=5+5)
- Loading branch information
Showing
4 changed files
with
152 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sets | ||
|
||
import "k8s.io/apimachinery/pkg/util/sets" | ||
|
||
// Merge merges the src sets into dst and returns dst. | ||
// This assumes that dst is non-nil. | ||
// For example: | ||
// s1 = {a1, a2, a3} | ||
// s2 = {a1, a2, a4, a5} | ||
// Merge(s1, s2) = {a1, a2, a3, a4, a5} | ||
// s1 = {a1, a2, a3, a4, a5} | ||
// | ||
// It supersedes s1.Union(s2) when constructing a new sets is not the intention. | ||
func Merge(dst, src sets.String) sets.String { | ||
for item := range src { | ||
dst.Insert(item) | ||
} | ||
return dst | ||
} |