Skip to content

Commit

Permalink
Avoid using expensive reflect for the Bond.Equal method
Browse files Browse the repository at this point in the history
reflect.DeepEqual is quite expensive so let's compare Bond
fields manually and using more efficient methods from the generics
package.

Signed-off-by: Milan Lenco <[email protected]>
  • Loading branch information
milan-zededa committed Nov 23, 2024
1 parent ef7b03c commit 2a295e8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/pillar/dpcreconciler/linuxitems/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ package linuxitems
import (
"context"
"fmt"
"reflect"

"github.com/vishvananda/netlink"

"github.com/lf-edge/eve-libs/depgraph"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/dpcreconciler/genericitems"
"github.com/lf-edge/eve/pkg/pillar/netmonitor"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/lf-edge/eve/pkg/pillar/utils/generics"
)

// Bond : Bond interface.
Expand Down Expand Up @@ -52,8 +51,12 @@ func (b Bond) Type() string {
// Equal is a comparison method for two equally-named Bond instances.
func (b Bond) Equal(other depgraph.Item) bool {
b2 := other.(Bond)
return reflect.DeepEqual(b.BondConfig, b2.BondConfig) &&
reflect.DeepEqual(b.AggregatedIfNames, b2.AggregatedIfNames) &&
return b.LacpRate == b2.LacpRate &&
b.MIIMonitor == b2.MIIMonitor &&
b.Mode == b2.Mode &&
b.ARPMonitor.Equal(b2.ARPMonitor) &&
generics.EqualSets(b.AggregatedIfNames, b2.AggregatedIfNames) &&
b.Usage == b2.Usage &&
b.MTU == b2.MTU
}

Expand Down Expand Up @@ -320,7 +323,10 @@ func (c *BondConfigurator) Delete(ctx context.Context, item depgraph.Item) error
func (c *BondConfigurator) NeedsRecreate(oldItem, newItem depgraph.Item) (recreate bool) {
oldBondCfg := oldItem.(Bond)
newBondCfg := newItem.(Bond)
if !reflect.DeepEqual(oldBondCfg.BondConfig, newBondCfg.BondConfig) {
if oldBondCfg.LacpRate != newBondCfg.LacpRate ||
oldBondCfg.MIIMonitor != newBondCfg.MIIMonitor ||
oldBondCfg.Mode != newBondCfg.Mode ||
!oldBondCfg.ARPMonitor.Equal(newBondCfg.ARPMonitor) {
return true
}
if oldBondCfg.Usage != newBondCfg.Usage {
Expand Down

0 comments on commit 2a295e8

Please sign in to comment.