Skip to content

Commit 9bd0ab8

Browse files
committed
perf(gateway): replace fmt.Sprintf with strconv/concat in hot paths
- Replace fmt.Sprintf in admin/topology.go (15 calls -> strconv.FormatUint + concat) - Replace fmt.Fprintf in admin/chatbot/rag.go (16 calls -> WriteString + strconv) - Replace fmt.Sprintf in infrastructure/inspector_support.go (6 calls) - Replace fmt.Sprintf in gwapi/http_route_validation.go (2 calls)
1 parent 49e35c3 commit 9bd0ab8

4 files changed

Lines changed: 114 additions & 59 deletions

File tree

internal/admin/chatbot/rag.go

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chatbot
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"strings"
78

89
corev1 "k8s.io/api/core/v1"
@@ -70,16 +71,29 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
7071
b.WriteString("(none)\n\n")
7172
} else {
7273
for _, gw := range managedGateways {
73-
fmt.Fprintf(&b, "- **%s/%s** (class: %s)\n", gw.Namespace, gw.Name, gw.Spec.GatewayClassName)
74+
b.WriteString("- **")
75+
b.WriteString(gw.Namespace)
76+
b.WriteString("/")
77+
b.WriteString(gw.Name)
78+
b.WriteString("** (class: ")
79+
b.WriteString(string(gw.Spec.GatewayClassName))
80+
b.WriteString(")\n")
7481
if len(gw.Spec.Listeners) > 0 {
7582
b.WriteString(" Listeners:\n")
7683
for _, l := range gw.Spec.Listeners {
7784
hostname := "-"
7885
if l.Hostname != nil {
7986
hostname = string(*l.Hostname)
8087
}
81-
fmt.Fprintf(&b, " - `%s`: port=%d proto=%s hostname=%s\n",
82-
l.Name, l.Port, l.Protocol, hostname)
88+
b.WriteString(" - `")
89+
b.WriteString(string(l.Name))
90+
b.WriteString("`: port=")
91+
b.WriteString(strconv.Itoa(int(l.Port)))
92+
b.WriteString(" proto=")
93+
b.WriteString(string(l.Protocol))
94+
b.WriteString(" hostname=")
95+
b.WriteString(hostname)
96+
b.WriteString("\n")
8397
}
8498
}
8599
}
@@ -92,7 +106,11 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
92106
b.WriteString("(none)\n\n")
93107
} else {
94108
for _, r := range httpList.Items {
95-
fmt.Fprintf(&b, "- **%s/%s**", r.Namespace, r.Name)
109+
b.WriteString("- **")
110+
b.WriteString(r.Namespace)
111+
b.WriteString("/")
112+
b.WriteString(r.Name)
113+
b.WriteString("**")
96114
fmtRouteParents(&b, r.Spec.ParentRefs, r.Namespace)
97115
b.WriteString("\n")
98116
for i, rule := range r.Spec.Rules {
@@ -102,10 +120,16 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
102120
if m.Path != nil && m.Path.Value != nil {
103121
path = *m.Path.Value
104122
}
105-
fmt.Fprintf(&b, " - rule[%d] match: path=%s\n", i, path)
123+
b.WriteString(" - rule[")
124+
b.WriteString(strconv.Itoa(i))
125+
b.WriteString("] match: path=")
126+
b.WriteString(path)
127+
b.WriteString("\n")
106128
}
107129
} else {
108-
fmt.Fprintf(&b, " - rule[%d] match: (all)\n", i)
130+
b.WriteString(" - rule[")
131+
b.WriteString(strconv.Itoa(i))
132+
b.WriteString("] match: (all)\n")
109133
}
110134
for _, br := range rule.BackendRefs {
111135
ns := r.Namespace
@@ -116,8 +140,13 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
116140
if br.Port != nil {
117141
port = int32(*br.Port)
118142
}
119-
fmt.Fprintf(&b, " → backend: %s/%s (port=%d)\n",
120-
string(br.Name), ns, port)
143+
b.WriteString(" → backend: ")
144+
b.WriteString(string(br.Name))
145+
b.WriteString("/")
146+
b.WriteString(ns)
147+
b.WriteString(" (port=")
148+
b.WriteString(strconv.Itoa(int(port)))
149+
b.WriteString(")\n")
121150
}
122151
}
123152
}
@@ -130,7 +159,11 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
130159
b.WriteString("(none)\n\n")
131160
} else {
132161
for _, r := range grpcList.Items {
133-
fmt.Fprintf(&b, "- **%s/%s**", r.Namespace, r.Name)
162+
b.WriteString("- **")
163+
b.WriteString(r.Namespace)
164+
b.WriteString("/")
165+
b.WriteString(r.Name)
166+
b.WriteString("**")
134167
fmtRouteParents(&b, r.Spec.ParentRefs, r.Namespace)
135168
b.WriteString("\n")
136169
for i, rule := range r.Spec.Rules {
@@ -144,45 +177,68 @@ func BuildRAGContext(ctx context.Context, cl client.Client, controllerName strin
144177
if m.Method.Method != nil {
145178
method = *m.Method.Method
146179
}
147-
fmt.Fprintf(&b, " - rule[%d] match: service=%s method=%s\n", i, svc, method)
180+
b.WriteString(" - rule[")
181+
b.WriteString(strconv.Itoa(i))
182+
b.WriteString("] match: service=")
183+
b.WriteString(svc)
184+
b.WriteString(" method=")
185+
b.WriteString(method)
186+
b.WriteString("\n")
148187
} else {
149-
fmt.Fprintf(&b, " - rule[%d] match: (all)\n", i)
188+
b.WriteString(" - rule[")
189+
b.WriteString(strconv.Itoa(i))
190+
b.WriteString("] match: (all)\n")
150191
}
151192
}
152193
if len(rule.Matches) == 0 {
153-
fmt.Fprintf(&b, " - rule[%d] match: (all)\n", i)
194+
b.WriteString(" - rule[")
195+
b.WriteString(strconv.Itoa(i))
196+
b.WriteString("] match: (all)\n")
154197
}
155-
for _, br := range rule.BackendRefs {
156-
ns := r.Namespace
157-
if br.Namespace != nil {
158-
ns = string(*br.Namespace)
159-
}
160-
port := int32(0)
161-
if br.Port != nil {
162-
port = int32(*br.Port)
163-
}
164-
fmt.Fprintf(&b, " → backend: %s/%s (port=%d)\n",
165-
string(br.Name), ns, port)
198+
for _, br := range rule.BackendRefs {
199+
ns := r.Namespace
200+
if br.Namespace != nil {
201+
ns = string(*br.Namespace)
166202
}
203+
port := int32(0)
204+
if br.Port != nil {
205+
port = int32(*br.Port)
206+
}
207+
b.WriteString(" → backend: ")
208+
b.WriteString(string(br.Name))
209+
b.WriteString("/")
210+
b.WriteString(ns)
211+
b.WriteString(" (port=")
212+
b.WriteString(strconv.Itoa(int(port)))
213+
b.WriteString(")\n")
167214
}
168215
}
169-
b.WriteString("\n")
170216
}
217+
b.WriteString("\n")
218+
}
171219

172-
// Services
220+
// Services
173221
b.WriteString("### Services\n\n")
174222
if len(svcList.Items) == 0 {
175223
b.WriteString("(none)\n\n")
176224
} else {
177225
for _, svc := range svcList.Items {
178-
fmt.Fprintf(&b, "- **%s/%s** (type=%s)", svc.Namespace, svc.Name, svc.Spec.Type)
226+
b.WriteString("- **")
227+
b.WriteString(svc.Namespace)
228+
b.WriteString("/")
229+
b.WriteString(svc.Name)
230+
b.WriteString("** (type=")
231+
b.WriteString(string(svc.Spec.Type))
232+
b.WriteString(")")
179233
if len(svc.Spec.Ports) > 0 {
180234
b.WriteString(" [")
181235
for j, port := range svc.Spec.Ports {
182236
if j > 0 {
183237
b.WriteString(", ")
184238
}
185-
fmt.Fprintf(&b, "%d/%s", port.Port, port.Protocol)
239+
b.WriteString(strconv.Itoa(int(port.Port)))
240+
b.WriteString("/")
241+
b.WriteString(string(port.Protocol))
186242
}
187243
b.WriteString("]")
188244
}
@@ -203,6 +259,9 @@ func fmtRouteParents(b *strings.Builder, refs []gatewayv1.ParentReference, defau
203259
if ref.Namespace != nil {
204260
ns = string(*ref.Namespace)
205261
}
206-
fmt.Fprintf(b, " → %s/%s", ns, ref.Name)
262+
b.WriteString(" → ")
263+
b.WriteString(ns)
264+
b.WriteString("/")
265+
b.WriteString(string(ref.Name))
207266
}
208267
}

internal/admin/topology.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package admin
33
import (
44
"fmt"
55
"sort"
6+
"strconv"
67
"strings"
78
"time"
89

@@ -108,7 +109,7 @@ func buildTopology(snapshot *ir.Snapshot, nodes []ir.NodeStatus) TopologyRespons
108109
Type: "listener",
109110
Label: listener.Name,
110111
Status: strings.ToLower(listener.Protocol),
111-
Detail: fmt.Sprintf("%s:%d", defaultAddress(listener.Address), listener.Port),
112+
Detail: defaultAddress(listener.Address) + ":" + strconv.FormatUint(uint64(listener.Port), 10),
112113
Metadata: map[string]string{"protocol": listener.Protocol, "hostnames": strings.Join(listener.Hostnames, ", ")},
113114
Namespace: listener.Metadata["nantian.dev/frontend-namespace"],
114115
})
@@ -152,7 +153,7 @@ func buildTopology(snapshot *ir.Snapshot, nodes []ir.NodeStatus) TopologyRespons
152153
}
153154
for _, routeNode := range routeNodes {
154155
response.Edges = append(response.Edges, TopologyEdge{
155-
ID: fmt.Sprintf("edge:%s:%s", sourceID, routeNode.ID),
156+
ID: "edge:" + sourceID + ":" + routeNode.ID,
156157
Source: sourceID,
157158
Target: routeNode.ID,
158159
Type: "attach",
@@ -277,11 +278,11 @@ func topologyBackendEdges(
277278
}
278279
backendID := ensureBackendTopologyNode(namespace, item.ref.Name, item.ref.Port, backendNodeIDs, backends, response)
279280
edges = append(edges, TopologyEdge{
280-
ID: fmt.Sprintf("edge:%s:%s", sourceID, backendID),
281+
ID: "edge:" + sourceID + ":" + backendID,
281282
Source: sourceID,
282283
Target: backendID,
283284
Type: "forward",
284-
Label: fmt.Sprintf("%s:%d", item.ref.Name, item.ref.Port),
285+
Label: item.ref.Name + ":" + strconv.FormatUint(uint64(item.ref.Port), 10),
285286
Weight: item.weight,
286287
Status: topologyBackendEdgeStatus(namespace, item.ref.Name, item.ref.Port, backends),
287288
})
@@ -305,21 +306,21 @@ func ensureBackendTopologyNode(
305306

306307
nodeID := backendNodeID(namespace, name, port)
307308
cluster, ok := findTopologyBackend(backends, namespace, name, port)
308-
detail := fmt.Sprintf("%s:%d", name, port)
309+
detail := name + ":" + strconv.FormatUint(uint64(port), 10)
309310
status := "unknown"
310-
metadata := map[string]string{"service": name, "port": fmt.Sprintf("%d", port)}
311+
metadata := map[string]string{"service": name, "port": strconv.FormatUint(uint64(port), 10)}
311312
if ok {
312313
detail = fmt.Sprintf("%s · %d/%d healthy", cluster.Protocol, healthyEndpoints(cluster), len(cluster.Endpoints))
313314
status = topologyBackendStatus(cluster)
314315
metadata["protocol"] = cluster.Protocol
315-
metadata["healthyEndpoints"] = fmt.Sprintf("%d", healthyEndpoints(cluster))
316-
metadata["totalEndpoints"] = fmt.Sprintf("%d", len(cluster.Endpoints))
316+
metadata["healthyEndpoints"] = strconv.Itoa(healthyEndpoints(cluster))
317+
metadata["totalEndpoints"] = strconv.Itoa(len(cluster.Endpoints))
317318
}
318319

319320
response.Nodes = append(response.Nodes, TopologyNode{
320321
ID: nodeID,
321322
Type: "backend",
322-
Label: fmt.Sprintf("%s:%d", name, port),
323+
Label: name + ":" + strconv.FormatUint(uint64(port), 10),
323324
Namespace: namespace,
324325
Name: name,
325326
Status: status,
@@ -339,7 +340,7 @@ func ensureBackendTopologyNode(
339340
Metadata: metadata,
340341
})
341342
response.Edges = append(response.Edges, TopologyEdge{
342-
ID: fmt.Sprintf("edge:%s:%s", nodeID, endpointNodeID),
343+
ID: "edge:" + nodeID + ":" + endpointNodeID,
343344
Source: nodeID,
344345
Target: endpointNodeID,
345346
Type: "resolve",
@@ -353,7 +354,7 @@ func ensureBackendTopologyNode(
353354
}
354355

355356
func findTopologyBackend(backends []ir.BackendCluster, namespace, service string, port uint32) (ir.BackendCluster, bool) {
356-
exactName := fmt.Sprintf("%s:%d", service, port)
357+
exactName := service + ":" + strconv.FormatUint(uint64(port), 10)
357358
for _, backend := range backends {
358359
if backend.Namespace == namespace && (backend.Name == exactName || backend.Metadata["service"] == service) {
359360
return backend, true
@@ -457,19 +458,19 @@ func listenerNodeID(name string) string {
457458
}
458459

459460
func routeNodeID(kind, namespace, name string) string {
460-
return fmt.Sprintf("route:%s:%s/%s", kind, namespace, name)
461+
return "route:" + kind + ":" + namespace + "/" + name
461462
}
462463

463464
func backendNodeID(namespace, name string, port uint32) string {
464-
return fmt.Sprintf("backend:%s/%s:%d", namespace, name, port)
465+
return "backend:" + namespace + "/" + name + ":" + strconv.FormatUint(uint64(port), 10)
465466
}
466467

467468
func endpointSetNodeID(namespace, name string, port uint32) string {
468-
return fmt.Sprintf("endpoint-set:%s/%s:%d", namespace, name, port)
469+
return "endpoint-set:" + namespace + "/" + name + ":" + strconv.FormatUint(uint64(port), 10)
469470
}
470471

471472
func backendNodeKey(namespace, name string, port uint32) string {
472-
return fmt.Sprintf("%s/%s:%d", namespace, name, port)
473+
return namespace + "/" + name + ":" + strconv.FormatUint(uint64(port), 10)
473474
}
474475

475476
func defaultAddress(address string) string {

internal/gwapi/http_route_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func httpRouteRuleMessage(prefix string, indexes []int, messages []string) strin
174174
return ""
175175
}
176176
if len(indexes) == 1 && len(messages) == 1 {
177-
return fmt.Sprintf("%s rule %d %s", prefix, indexes[0]+1, messages[0])
177+
return prefix + " rule " + strconv.Itoa(indexes[0]+1) + " " + messages[0]
178178
}
179179
if len(messages) == len(indexes) && allStringsEqual(messages) {
180180
return fmt.Sprintf(

internal/infrastructure/inspector_support.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"sort"
7+
"strconv"
78

89
corev1 "k8s.io/api/core/v1"
910
discoveryv1 "k8s.io/api/discovery/v1"
@@ -759,46 +760,40 @@ func finalizeInfrastructureReport(report *InfrastructureReport) {
759760
if report.Summary.MissingCount > 0 {
760761
report.Warnings = append(
761762
report.Warnings,
762-
fmt.Sprintf("%d derived infrastructure resources are missing", report.Summary.MissingCount),
763+
strconv.Itoa(report.Summary.MissingCount)+" derived infrastructure resources are missing",
763764
)
764765
}
765766
if report.Summary.DriftedCount > 0 {
766767
report.Warnings = append(
767768
report.Warnings,
768-
fmt.Sprintf("%d derived infrastructure resources have drifted from desired state", report.Summary.DriftedCount),
769+
strconv.Itoa(report.Summary.DriftedCount)+" derived infrastructure resources have drifted from desired state",
769770
)
770771
}
771772
if report.Summary.OrphanCount > 0 {
772773
report.Warnings = append(
773774
report.Warnings,
774-
fmt.Sprintf("%d managed infrastructure resources are orphaned", report.Summary.OrphanCount),
775+
strconv.Itoa(report.Summary.OrphanCount)+" managed infrastructure resources are orphaned",
775776
)
776777
}
777778
if report.Summary.GatewayConvergence.PendingServiceMetadataCount > 0 {
778779
report.Warnings = append(
779780
report.Warnings,
780-
fmt.Sprintf(
781-
"%d gateways are waiting for derived Service metadata convergence",
782-
report.Summary.GatewayConvergence.PendingServiceMetadataCount,
783-
),
781+
strconv.Itoa(report.Summary.GatewayConvergence.PendingServiceMetadataCount)+
782+
" gateways are waiting for derived Service metadata convergence",
784783
)
785784
}
786785
if report.Summary.GatewayConvergence.PendingFrontendEndpointSliceCount > 0 {
787786
report.Warnings = append(
788787
report.Warnings,
789-
fmt.Sprintf(
790-
"%d gateways are waiting for derived frontend EndpointSlice convergence",
791-
report.Summary.GatewayConvergence.PendingFrontendEndpointSliceCount,
792-
),
788+
strconv.Itoa(report.Summary.GatewayConvergence.PendingFrontendEndpointSliceCount)+
789+
" gateways are waiting for derived frontend EndpointSlice convergence",
793790
)
794791
}
795792
if report.Summary.GatewayConvergence.PendingProgrammedObservedGenerationCount > 0 {
796793
report.Warnings = append(
797794
report.Warnings,
798-
fmt.Sprintf(
799-
"%d gateways are waiting for Programmed observedGeneration convergence",
800-
report.Summary.GatewayConvergence.PendingProgrammedObservedGenerationCount,
801-
),
795+
strconv.Itoa(report.Summary.GatewayConvergence.PendingProgrammedObservedGenerationCount)+
796+
" gateways are waiting for Programmed observedGeneration convergence",
802797
)
803798
}
804799
}

0 commit comments

Comments
 (0)