Skip to content

Commit 780e075

Browse files
authored
perf: reuse route rule metadata (envoyproxy#7001)
* perf: reuse route rule metadata * build it once and pass it in the caller * also since EG annotations are rare, make sure to return early and reduce allocations Signed-off-by: Arko Dasgupta <[email protected]>
1 parent ec3ffd2 commit 780e075

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

internal/gatewayapi/route.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
198198
continue
199199
}
200200

201+
routeRuleMetadata := buildResourceMetadata(httpRoute, rule.Name)
202+
201203
// The HTTPRouteRule matches are ORed, a rule is matched if any one of its matches is satisfied,
202204
// so generate a unique Xds IR HTTPRoute per match.
203-
ruleRoutes, err := t.processHTTPRouteRule(httpRoute, ruleIdx, httpFiltersContext, rule)
205+
ruleRoutes, err := t.processHTTPRouteRule(httpRoute, ruleIdx, httpFiltersContext, rule, routeRuleMetadata)
204206
if err != nil {
205207
errs.Add(status.NewRouteStatusError(
206208
fmt.Errorf("failed to process route rule %d: %w", ruleIdx, err),
@@ -251,7 +253,7 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
251253
}
252254
destination := &ir.RouteDestination{
253255
Settings: allDs,
254-
Metadata: buildResourceMetadata(httpRoute, rule.Name),
256+
Metadata: routeRuleMetadata,
255257
}
256258

257259
switch {
@@ -379,15 +381,16 @@ func (t *Translator) processHTTPRouteRule(
379381
ruleIdx int,
380382
httpFiltersContext *HTTPFiltersContext,
381383
rule gwapiv1.HTTPRouteRule,
384+
routeRuleMetadata *ir.ResourceMetadata,
382385
) ([]*ir.HTTPRoute, status.Error) {
383386
var ruleRoutes []*ir.HTTPRoute
384387

385388
// If no matches are specified, the implementation MUST match every HTTP request.
386389
if len(rule.Matches) == 0 {
387390
irRoute := &ir.HTTPRoute{
388-
Name: irRouteName(httpRoute, ruleIdx, -1),
391+
Name: irRouteName(httpRoute, ruleIdx, -1),
392+
Metadata: routeRuleMetadata,
389393
}
390-
irRoute.Metadata = buildResourceMetadata(httpRoute, rule.Name)
391394
processRouteTrafficFeatures(irRoute, rule)
392395
applyHTTPFiltersContextToIRRoute(httpFiltersContext, irRoute)
393396
ruleRoutes = append(ruleRoutes, irRoute)
@@ -453,8 +456,8 @@ func (t *Translator) processHTTPRouteRule(
453456
irRoute := &ir.HTTPRoute{
454457
Name: irRouteName(httpRoute, ruleIdx, matchIdx),
455458
SessionPersistence: sessionPersistence,
459+
Metadata: routeRuleMetadata,
456460
}
457-
irRoute.Metadata = buildResourceMetadata(httpRoute, rule.Name)
458461
processRouteTrafficFeatures(irRoute, rule)
459462

460463
if match.Path != nil {
@@ -896,15 +899,19 @@ func buildResourceMetadata(resource client.Object, sectionName *gwapiv1.SectionN
896899
}
897900

898901
func filterEGPrefix(in map[string]string) map[string]string {
899-
out := map[string]string{}
902+
if len(in) == 0 {
903+
return nil
904+
}
905+
906+
var out map[string]string
900907
for k, v := range in {
901908
if strings.HasPrefix(k, egPrefix) {
909+
if out == nil {
910+
out = make(map[string]string, len(in))
911+
}
902912
out[strings.TrimPrefix(k, egPrefix)] = v
903913
}
904914
}
905-
if len(out) == 0 {
906-
return nil
907-
}
908915
return out
909916
}
910917

0 commit comments

Comments
 (0)