From 1490750bdfe909aff77e0bb12b7a129afec82b45 Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Wed, 17 Sep 2025 18:24:59 -0700 Subject: [PATCH 1/2] 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 --- internal/gatewayapi/route.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index fe918ee975..691c86a73e 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -198,9 +198,11 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe continue } + routeRuleMetadata := buildResourceMetadata(httpRoute, rule.Name) + // The HTTPRouteRule matches are ORed, a rule is matched if any one of its matches is satisfied, // so generate a unique Xds IR HTTPRoute per match. - ruleRoutes, err := t.processHTTPRouteRule(httpRoute, ruleIdx, httpFiltersContext, rule) + ruleRoutes, err := t.processHTTPRouteRule(httpRoute, ruleIdx, httpFiltersContext, rule, routeRuleMetadata) if err != nil { errs.Add(status.NewRouteStatusError( fmt.Errorf("failed to process route rule %d: %w", ruleIdx, err), @@ -251,7 +253,7 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe } destination := &ir.RouteDestination{ Settings: allDs, - Metadata: buildResourceMetadata(httpRoute, rule.Name), + Metadata: routeRuleMetadata, } switch { @@ -379,15 +381,16 @@ func (t *Translator) processHTTPRouteRule( ruleIdx int, httpFiltersContext *HTTPFiltersContext, rule gwapiv1.HTTPRouteRule, + routeRuleMetadata *ir.ResourceMetadata, ) ([]*ir.HTTPRoute, status.Error) { var ruleRoutes []*ir.HTTPRoute // If no matches are specified, the implementation MUST match every HTTP request. if len(rule.Matches) == 0 { irRoute := &ir.HTTPRoute{ - Name: irRouteName(httpRoute, ruleIdx, -1), + Name: irRouteName(httpRoute, ruleIdx, -1), + Metadata: routeRuleMetadata, } - irRoute.Metadata = buildResourceMetadata(httpRoute, rule.Name) processRouteTrafficFeatures(irRoute, rule) applyHTTPFiltersContextToIRRoute(httpFiltersContext, irRoute) ruleRoutes = append(ruleRoutes, irRoute) @@ -453,8 +456,8 @@ func (t *Translator) processHTTPRouteRule( irRoute := &ir.HTTPRoute{ Name: irRouteName(httpRoute, ruleIdx, matchIdx), SessionPersistence: sessionPersistence, + Metadata: routeRuleMetadata, } - irRoute.Metadata = buildResourceMetadata(httpRoute, rule.Name) processRouteTrafficFeatures(irRoute, rule) if match.Path != nil { @@ -896,15 +899,29 @@ func buildResourceMetadata(resource client.Object, sectionName *gwapiv1.SectionN } func filterEGPrefix(in map[string]string) map[string]string { - out := map[string]string{} - for k, v := range in { + if len(in) == 0 { + return nil + } + + // check if any EG-prefixed annotations exist + hasEGAnnotations := false + for k := range in { if strings.HasPrefix(k, egPrefix) { - out[strings.TrimPrefix(k, egPrefix)] = v + hasEGAnnotations = true + break } } - if len(out) == 0 { + + if !hasEGAnnotations { return nil } + + out := make(map[string]string, len(in)) + for k, v := range in { + if strings.HasPrefix(k, egPrefix) { + out[strings.TrimPrefix(k, egPrefix)] = v + } + } return out } From a4f1bc8a74409112f12c9ebb30b022a1c607f59f Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Wed, 17 Sep 2025 18:31:02 -0700 Subject: [PATCH 2/2] improve func Signed-off-by: Arko Dasgupta --- internal/gatewayapi/route.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index 691c86a73e..4addd4c86b 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -903,22 +903,12 @@ func filterEGPrefix(in map[string]string) map[string]string { return nil } - // check if any EG-prefixed annotations exist - hasEGAnnotations := false - for k := range in { - if strings.HasPrefix(k, egPrefix) { - hasEGAnnotations = true - break - } - } - - if !hasEGAnnotations { - return nil - } - - out := make(map[string]string, len(in)) + var out map[string]string for k, v := range in { if strings.HasPrefix(k, egPrefix) { + if out == nil { + out = make(map[string]string, len(in)) + } out[strings.TrimPrefix(k, egPrefix)] = v } }