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 <milan@zededa.com>
  • Loading branch information
milan-zededa authored and eriknordmark committed Nov 25, 2024
1 parent 1f23508 commit a783bfc
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 a783bfc

Please sign in to comment.