Skip to content

Commit e61630a

Browse files
committed
perf(translator): pre-allocate map capacity in hot paths
Add capacity hints to maps where input size is known: - effectiveBackendServices: shadowByName + shadowByOriginal with len(services) - loadPartialBackends: loadedKeys with len(serviceKeys) Reduces map rehashing and heap allocations during translation.
1 parent f6b1cbb commit e61630a

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

internal/translator/mesh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func translateMeshServiceListeners(frontends []mesh.ServiceFrontendPort) []ir.Li
7272
}
7373

7474
func effectiveBackendServices(services []corev1.Service) []backendService {
75-
shadowByName := make(map[string]corev1.Service)
76-
shadowByOriginal := make(map[string]corev1.Service)
75+
shadowByName := make(map[string]corev1.Service, len(services))
76+
shadowByOriginal := make(map[string]corev1.Service, len(services))
7777
for _, service := range services {
7878
if service.Labels[mesh.ShadowServiceRoleLabel] != mesh.ShadowServiceRoleValue {
7979
continue

internal/translator/partial_backends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func expandMeshShadowBackendKeys(
225225

226226
expandedServiceKeys := cloneObjectKeyMap(serviceKeys)
227227
expandedReplacementKeys := cloneObjectKeyMap(replacementKeys)
228-
loadedKeys := make(map[string]struct{})
228+
loadedKeys := make(map[string]struct{}, len(serviceKeys))
229229
pendingKeys := cloneObjectKeyMap(serviceKeys)
230230

231231
for len(pendingKeys) != 0 {

0 commit comments

Comments
 (0)