Skip to content

Commit 8ffc436

Browse files
authored
perf: preallocate slices in route processing (#7041)
* perf: preallocate slices in route processing Signed-off-by: Arko Dasgupta <arko@tetrate.io> * use index Signed-off-by: Arko Dasgupta <arko@tetrate.io>
1 parent 099695a commit 8ffc436

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

internal/gatewayapi/contexts.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ type ListenerContext struct {
7878
}
7979

8080
func (l *ListenerContext) SetSupportedKinds(kinds ...gwapiv1.RouteGroupKind) {
81-
l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds = make([]gwapiv1.RouteGroupKind, 0, len(kinds))
82-
l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds = append(l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds, kinds...)
81+
if len(kinds) > 0 {
82+
l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds = make([]gwapiv1.RouteGroupKind, len(kinds))
83+
copy(l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds, kinds)
84+
} else {
85+
l.gateway.Status.Listeners[l.listenerStatusIdx].SupportedKinds = []gwapiv1.RouteGroupKind{}
86+
}
8387
}
8488

8589
func (l *ListenerContext) IncrementAttachedRoutes() {

internal/gatewayapi/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,9 @@ func getPolicyTargetRefs[T client.Object](policy egv1a1.PolicyTargetReferences,
591591
slices.SortFunc(selectorsList, func(i, j targetRefWithTimestamp) int {
592592
return i.CreationTimestamp.Compare(j.CreationTimestamp.Time)
593593
})
594-
ret := []gwapiv1a2.LocalPolicyTargetReferenceWithSectionName{}
595-
for _, v := range selectorsList {
596-
ret = append(ret, v.LocalPolicyTargetReferenceWithSectionName)
594+
ret := make([]gwapiv1a2.LocalPolicyTargetReferenceWithSectionName, len(selectorsList))
595+
for i, v := range selectorsList {
596+
ret[i] = v.LocalPolicyTargetReferenceWithSectionName
597597
}
598598
// Plain targetRefs in the policy don't have an associated creation timestamp, but can still refer
599599
// to targets that were already found via the selectors. Only add them to the returned list if

internal/gatewayapi/route.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
212212
}
213213

214214
destName := irRouteDestinationName(httpRoute, ruleIdx)
215-
allDs := []*ir.DestinationSetting{}
215+
allDs := make([]*ir.DestinationSetting, 0, len(rule.BackendRefs))
216216
failedProcessDestination := false
217217
hasDynamicResolver := false
218218
backendRefNames := make([]string, len(rule.BackendRefs))
219-
backendCustomRefs := []*ir.UnstructuredRef{}
219+
backendCustomRefs := make([]*ir.UnstructuredRef, 0, len(rule.BackendRefs))
220220
// process each backendRef, and calculate the destination settings for this rule
221221
for i := range rule.BackendRefs {
222222
settingName := irDestinationSettingName(destName, i)
@@ -664,7 +664,7 @@ func (t *Translator) processGRPCRouteRules(grpcRoute *GRPCRouteContext, parentRe
664664
}
665665

666666
destName := irRouteDestinationName(grpcRoute, ruleIdx)
667-
allDs := []*ir.DestinationSetting{}
667+
allDs := make([]*ir.DestinationSetting, 0, len(rule.BackendRefs))
668668
failedProcessDestination := false
669669

670670
backendRefNames := make([]string, len(rule.BackendRefs))

internal/gatewayapi/translator.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,26 @@ func (t *Translator) Translate(resources *resource.Resources) (*TranslateResult,
202202
// Process ClientTrafficPolicies
203203
clientTrafficPolicies := t.ProcessClientTrafficPolicies(resources, acceptedGateways, xdsIR, infraIR)
204204

205-
// Process BackendTrafficPolicies
206-
routes := []RouteContext{}
207-
for _, h := range httpRoutes {
208-
routes = append(routes, h)
205+
routes := make([]RouteContext, len(httpRoutes)+len(grpcRoutes)+len(tlsRoutes)+len(tcpRoutes)+len(udpRoutes))
206+
offset := 0
207+
for i := range httpRoutes {
208+
routes[offset+i] = httpRoutes[i]
209209
}
210-
for _, g := range grpcRoutes {
211-
routes = append(routes, g)
210+
offset += len(httpRoutes)
211+
for i := range grpcRoutes {
212+
routes[offset+i] = grpcRoutes[i]
212213
}
213-
for _, t := range tlsRoutes {
214-
routes = append(routes, t)
214+
offset += len(grpcRoutes)
215+
for i := range tlsRoutes {
216+
routes[offset+i] = tlsRoutes[i]
215217
}
216-
for _, t := range tcpRoutes {
217-
routes = append(routes, t)
218+
offset += len(tlsRoutes)
219+
for i := range tcpRoutes {
220+
routes[offset+i] = tcpRoutes[i]
218221
}
219-
for _, u := range udpRoutes {
220-
routes = append(routes, u)
222+
offset += len(tcpRoutes)
223+
for i := range udpRoutes {
224+
routes[offset+i] = udpRoutes[i]
221225
}
222226

223227
// Process BackendTrafficPolicies

0 commit comments

Comments
 (0)