Skip to content

Commit f6b1cbb

Browse files
committed
perf: replace fmt.Sprintf with direct concat in hot paths
Replace fmt.Sprintf("%s/%s") with direct string concatenation in reconciliation hot paths to reduce heap allocations. - translator_listeners: namespace/ref.Name key construction - backend_tls: ClientCertificateRef construction - backend_tls_policy: backendClusterKey with strconv.Itoa - mesh/frontends: servicePortKey with strconv.Itoa - status/reconciler_listener_sets: listener set key lookup Removes fmt import from 4 files. Adds strconv to 1 file.
1 parent 6b5598f commit f6b1cbb

5 files changed

Lines changed: 6 additions & 9 deletions

File tree

internal/mesh/frontends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func serviceKey(namespace string, name string) string {
198198
}
199199

200200
func servicePortKey(namespace string, name string, port int32) string {
201-
return fmt.Sprintf("%s/%s/%d", namespace, name, port)
201+
return namespace + "/" + name + "/" + strconv.Itoa(int(port))
202202
}
203203

204204
func namespaceOrDefault(namespace *gatewayv1.Namespace, defaultNamespace string) string {

internal/status/reconciler_listener_sets.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package status
22

33
import (
44
"context"
5-
"fmt"
65

76
apiequality "k8s.io/apimachinery/pkg/api/equality"
87
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -15,7 +14,7 @@ import (
1514
func (r *Reconciler) reconcileListenerSetStatuses(ctx context.Context, lses []gatewayv1.ListenerSet, evals map[string]listenerSetEvaluation) error {
1615
for i := range lses {
1716
key := client.ObjectKeyFromObject(&lses[i])
18-
eval, ok := evals[fmt.Sprintf("%s/%s", lses[i].Namespace, lses[i].Name)]
17+
eval, ok := evals[lses[i].Namespace+"/"+lses[i].Name]
1918
if !ok {
2019
continue
2120
}

internal/translator/backend_tls.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package translator
22

33
import (
44
"crypto/tls"
5-
"fmt"
65

76
corev1 "k8s.io/api/core/v1"
87
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -69,7 +68,7 @@ func backendTLSForGatewayWithIndexes(
6968
}
7069

7170
return &ir.BackendTLSConfig{
72-
ClientCertificateRef: fmt.Sprintf("%s/%s", targetNamespace, ref.Name),
71+
ClientCertificateRef: targetNamespace + "/" + string(ref.Name),
7372
}
7473
}
7574

internal/translator/backend_tls_policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package translator
22

33
import (
44
"crypto/x509"
5-
"fmt"
65
"sort"
6+
"strconv"
77

88
corev1 "k8s.io/api/core/v1"
99
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -383,7 +383,7 @@ func serviceImportBackendKeys(
383383
}
384384

385385
func backendClusterKey(namespace, name string, port int32) string {
386-
return fmt.Sprintf("%s/%s:%d", namespace, name, port)
386+
return namespace + "/" + name + ":" + strconv.Itoa(int(port))
387387
}
388388

389389
func compactStrings(items []string) []string {

internal/translator/translator_listeners.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package translator
22

33
import (
44
"crypto/tls"
5-
"fmt"
65
"net"
76
"sort"
87
"strings"
@@ -141,7 +140,7 @@ func listenerCertificateSecretRefsWithIndexes(
141140
continue
142141
}
143142

144-
key := fmt.Sprintf("%s/%s", targetNamespace, ref.Name)
143+
key := targetNamespace + "/" + string(ref.Name)
145144
if _, exists := seen[key]; exists {
146145
continue
147146
}

0 commit comments

Comments
 (0)