Skip to content

perf: intern AttachedPolicyRef ID strings#14217

Merged
puertomontt merged 1 commit into
kgateway-dev:mainfrom
puertomontt:intern-attached-policy-ref-id
Jun 10, 2026
Merged

perf: intern AttachedPolicyRef ID strings#14217
puertomontt merged 1 commit into
kgateway-dev:mainfrom
puertomontt:intern-attached-policy-ref-id

Conversation

@puertomontt

Copy link
Copy Markdown
Contributor

Description

A memory profile of a large production environment showed ~6.6% of the controller's live heap attributed to AttachedPolicyRef.ID. The concatenated ID string is retained in MergeOrigins sets for every route a policy attaches to, so a policy attached to many routes retains many identical copies of the same string.

This PR:

  • Interns the ID via Go's unique package, so all retained copies share a single canonical backing string. The interning map is weakly held, so entries are garbage collected once the policy refs go away.
  • Hoists the policyRef.ID() computation out of the per-field loop in MergeOrigins.GetRefCount, which recomputed the concatenation once per merged field.

The emitted Envoy configuration and status reporting are unchanged; only the memory sharing changes.

Change Type

/kind cleanup

Changelog

Reduced controller memory usage by interning policy ref ID strings retained in policy merge tracking.

Additional Notes

An alternative considered was caching the computed ID on the AttachedPolicyRef struct, but a lazily-written field would be a data race under concurrent gateway translations; interning achieves the same retained-memory deduplication without changing struct semantics.

A memory profile of a large production environment showed ~6.6% of the
controller's live heap in AttachedPolicyRef.ID: the concatenated ID string
is retained in MergeOrigins sets for every route a policy attaches to, so
a policy attached to many routes retains many identical copies.

Intern the ID via the unique package so all copies share one canonical
backing string, and hoist the ID computation out of the per-field loop in
MergeOrigins.GetRefCount, which recomputed it once per merged field.

Signed-off-by: omar <[email protected]>
Copilot AI review requested due to automatic review settings June 10, 2026 10:48
@gateway-bot gateway-bot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note labels Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces controller heap usage by deduplicating AttachedPolicyRef.ID() strings that are retained across many MergeOrigins sets, and removes redundant per-field recomputation of that ID during merge-origin reference counting.

Changes:

  • Intern AttachedPolicyRef.ID() using Go’s unique package so retained copies share a single canonical backing string.
  • Hoist policyRef.ID() out of the MergeOrigins.GetRefCount per-field loop to avoid repeated concatenation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/pluginsdk/ir/merge.go Avoids recomputing the policy ref ID for each merged field when counting references.
pkg/pluginsdk/ir/gw.go Interns the computed policy ref ID string to reduce retained duplicate string backing storage.

@davidjumani
davidjumani added this pull request to the merge queue Jun 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 10, 2026
@puertomontt
puertomontt added this pull request to the merge queue Jun 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jun 10, 2026
@puertomontt
puertomontt added this pull request to the merge queue Jun 10, 2026
Merged via the queue into kgateway-dev:main with commit 0e8463b Jun 10, 2026
44 of 45 checks passed
@puertomontt
puertomontt deleted the intern-attached-policy-ref-id branch June 10, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants