Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not construct intermediate sets when calculating union of multiple sets #1938

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

tnqn
Copy link
Member

@tnqn tnqn commented Mar 4, 2021

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)

@codecov-io
Copy link

codecov-io commented Mar 4, 2021

Codecov Report

Merging #1938 (a3b2459) into main (2e78afe) will decrease coverage by 3.87%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1938      +/-   ##
==========================================
- Coverage   62.49%   58.61%   -3.88%     
==========================================
  Files         202      203       +1     
  Lines       17480    17482       +2     
==========================================
- Hits        10924    10247     -677     
- Misses       5388     6120     +732     
+ Partials     1168     1115      -53     
Flag Coverage Δ
e2e-tests 44.38% <100.00%> (?)
kind-e2e-tests ?
unit-tests 40.82% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ntroller/networkpolicy/networkpolicy_controller.go 73.37% <100.00%> (-10.50%) ⬇️
pkg/util/sets/string.go 100.00% <100.00%> (ø)
pkg/apis/controlplane/helper.go 25.00% <0.00%> (-75.00%) ⬇️
pkg/apis/controlplane/v1beta2/helper.go 25.00% <0.00%> (-75.00%) ⬇️
pkg/controller/networkpolicy/mutate.go 0.00% <0.00%> (-71.77%) ⬇️
pkg/controller/networkpolicy/validate.go 0.00% <0.00%> (-71.12%) ⬇️
pkg/agent/util/ethtool/ethtool_linux.go 0.00% <0.00%> (-70.00%) ⬇️
pkg/controller/networkpolicy/store/group.go 4.00% <0.00%> (-68.00%) ⬇️
pkg/k8s/name.go 33.33% <0.00%> (-66.67%) ⬇️
pkg/agent/controller/networkpolicy/packetin.go 10.76% <0.00%> (-55.39%) ⬇️
... and 65 more

@tnqn
Copy link
Member Author

tnqn commented Mar 4, 2021

/test-all

@tnqn
Copy link
Member Author

tnqn commented Mar 4, 2021

/test-integration
/test-networkpolicy

antoninbas
antoninbas previously approved these changes Mar 4, 2021
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/a new sets/a new set

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

Dyanngg
Dyanngg previously approved these changes Mar 4, 2021
Copy link
Contributor

@jianjuns jianjuns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice optimization!

… 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)
@tnqn tnqn dismissed stale reviews from Dyanngg and antoninbas via a3b2459 March 5, 2021 03:07
@tnqn
Copy link
Member Author

tnqn commented Mar 5, 2021

/test-all

@tnqn
Copy link
Member Author

tnqn commented Mar 5, 2021

Merging it as jenkins-windows-networkpolicy succeeded before I fixed the typo.
Thanks for the review @antoninbas @Dyanngg @jianjuns.

@tnqn tnqn merged commit 151f738 into antrea-io:main Mar 5, 2021
@tnqn tnqn deleted the sets-union branch March 5, 2021 13:08
tnqn added a commit to tnqn/antrea that referenced this pull request Mar 17, 2021
… sets (antrea-io#1938)

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants