Avoid performing expensive computational operations repeatedly when the same result can be achieved through better algorithm design or data structure choices. This includes avoiding sorting on each access, repeated string parsing, or complex comparisons that could be precomputed or cached.
Avoid performing expensive computational operations repeatedly when the same result can be achieved through better algorithm design or data structure choices. This includes avoiding sorting on each access, repeated string parsing, or complex comparisons that could be precomputed or cached.
Key principles:
Example from the codebase:
// Bad: Sorting on each equality check
func WorkloadInstancesEqual(first, second *WorkloadInstance) bool {
firtAddrs := first.Endpoint.SortIstioEndpointByAddresses() // Mutates and sorts every time
// ... comparison logic
}
// Better: Use a more efficient comparison or maintain sorted order
func WorkloadInstancesEqual(first, second *WorkloadInstance) bool {
return IsAddrsEqualIstioEndpoint(first.Endpoint, second.Endpoint) // Efficient comparison without sorting
}
Another example:
// Bad: String concatenation and splitting for indexing
workloadNetworkServiceIndex := krt.NewIndex[string, model.WorkloadInfo](GlobalWorkloads, "network;service", func(o model.WorkloadInfo) []string {
// Later requires: parts := strings.Split(i.Key, ";")
})
// Better: Use a proper struct for composite keys
type NetworkServiceKey struct {
Network string
Service string
}
This approach reduces computational complexity, improves performance, and makes code more maintainable by choosing algorithms and data structures that match the usage patterns.
Enter the URL of a public GitHub repository