Skip to content

perf: reduce linear scans #10306

Closed
Closed

Description

@guydc

Description:

At the moment, some linear scans are performed per route/destination, e.g.:

Double scan of BTLSPs per destination with expensive deepEquals check in backendTLSTargetMatched:

if target.SectionName != nil {
for _, policy := range policies {
if backendTLSTargetMatched(policy, target, backendNamespace, true) {
// prefer policies that target this specific section over wildcard matches
return policy
}
}
}
for _, policy := range policies {
if backendTLSTargetMatched(policy, target, backendNamespace, false) {
return policy
}
}

Scan all ExtensionRefFilters per route rule with an extension filter and match by expensive unstructured.NestedString checks from GetKind/Name/... calls.

for _, res := range resources.ExtensionRefFilters {
if res.GetKind() == string(extFilter.Kind) && res.GetName() == string(extFilter.Name) && res.GetNamespace() == filterNs {
apiVers := res.GetAPIVersion()

List all RefGrants to a kind, whenever cross-namespace access is evaluated to a resource of that kind in reconciler. Maybe a more precise index can be added to perform more focused lookups, including namespaces.

func (r *gatewayAPIReconciler) findReferenceGrant(ctx context.Context, from, to ObjectKindNamespacedName) (*gwapiv1b1.ReferenceGrant, error) {
refGrantList := new(gwapiv1b1.ReferenceGrantList)
opts := &client.ListOptions{FieldSelector: fields.OneTermEqualSelector(targetRefGrantRouteIndex, to.kind)}
if err := r.client.List(ctx, refGrantList, opts); err != nil {
return nil, fmt.Errorf("failed to list ReferenceGrants: %w", err)
}
for i := range refGrantList.Items {
refGrant := &refGrantList.Items[i]
if refGrant.Namespace != to.namespace {
continue
}
var fromAllowed bool
for _, refGrantFrom := range refGrant.Spec.From {
if string(refGrantFrom.Kind) == from.kind && string(refGrantFrom.Namespace) == from.namespace {
fromAllowed = true
break
}
}
if !fromAllowed {
continue
}
var toAllowed bool
for _, refGrantTo := range refGrant.Spec.To {
if string(refGrantTo.Kind) == to.kind && (refGrantTo.Name == nil || *refGrantTo.Name == "" || string(*refGrantTo.Name) == to.name) {
toAllowed = true
break
}
}
if !toAllowed {
continue
}

These scans be optimized by building an index and possibly performing less expensive comparisons.

[optional Relevant Links:]

Any extra documentation required to understand the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions