Skip to content

Commit

Permalink
Improve 'ipSliceDifference' function memory footprint (vmware#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
Didainius committed Apr 18, 2023
1 parent e9d15fd commit 35dc5da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .changes/v2.20.0/532-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Added `NsxtEdgeGateway.GetUsedIpAddressSlice` method to fetch used IP addresses in a slice
[GH-532]
* Added `NsxtEdgeGateway.GetUnusedExternalIPAddresses` method that can help to find an unused
IP address in an Edge Gateway by given constraints [GH-532]
IP address in an Edge Gateway by given constraints [GH-532,GH-567]
* Added `NsxtEdgeGateway.GetAllUnusedExternalIPAddresses` method that can return all unused IP
addresses in an Edge Gateway [GH-532]
addresses in an Edge Gateway [GH-532,GH-567]
* Added `NsxtEdgeGateway.GetAllocatedIpCount` method that sums up `TotalIPCount` fields in all
subnets [GH-532]
* Added `NsxtEdgeGateway.QuickDeallocateIpCount` and `NsxtEdgeGateway.DeallocateIpCount`
Expand Down
24 changes: 17 additions & 7 deletions govcd/nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ func flattenEdgeGatewayUplinkToIpSlice(uplinks []types.EdgeGatewayUplinks) ([]ne
// Special behavior:
// * Passing nil minuend results in nil
// * Passing nil subtrahend will return minuendSlice
//
// NOTE. This function will mutate minuendSlice to save memory and avoid having a copy of all values
// which can become expensive if there are a lot of items
func ipSliceDifference(minuendSlice, subtrahendSlice []netip.Addr) []netip.Addr {
if minuendSlice == nil {
return nil
Expand All @@ -773,31 +776,38 @@ func ipSliceDifference(minuendSlice, subtrahendSlice []netip.Addr) []netip.Addr
return minuendSlice
}

var difference []netip.Addr
resultIpCount := 0 // count of IPs after removing items from subtrahendSlice

// Loop over minuend IPs
for _, minuendIp := range minuendSlice {

// Check if subtrahend has minuend element listed
var foundSubtrahend bool

for _, subtrahendIp := range subtrahendSlice {
if subtrahendIp == minuendIp {
// IP found in subtrahend, therefore breaking inner loop early
foundSubtrahend = true
break
}

}

// Store the IP in difference when subtrahend does not contain IP of minuend
// Store the IP in `minuendSlice` at `resultIpCount` index and increment the index itself
if !foundSubtrahend {
// Add IP to the resulting difference slice
difference = append(difference, minuendIp)
// Add IP to the 'resultIpCount' index position
minuendSlice[resultIpCount] = minuendIp
resultIpCount++
}
}

return difference
// if all elements are removed - return nil
if resultIpCount == 0 {
return nil
}

// cut off all values, greater than `resultIpCount`
minuendSlice = minuendSlice[:resultIpCount]

return minuendSlice
}

// filterIpSlicesBySubnet accepts 'ipRange' and returns a slice of IPs only that fall into given
Expand Down

0 comments on commit 35dc5da

Please sign in to comment.