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

Improve 'ipSliceDifference' function memory footprint #567

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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