Skip to content

discovery/azure: keep SD clients from defeating dead-code elimination - #18824

Merged
roidelapluie merged 1 commit into
prometheus:mainfrom
roidelapluie:roidelapluie/azure-sd-binary-size
Jun 1, 2026
Merged

roidelapluie merged 1 commit into
prometheus:mainfrom
roidelapluie:roidelapluie/azure-sd-binary-size

Conversation

@roidelapluie

Copy link
Copy Markdown
Member

The azureClient struct held the armcompute and armnetwork SDK clients as concrete fields while satisfying the client interface. Once such a client is reachable through an interface, Go's linker conservatively retains every exported method of the concrete type plus the entire (de)serializer graph those operations drag in, even though discovery calls only a handful of them.

Wrap each SDK client in a small adapter that captures only the operations discovery uses as method-value closures, and box the adapters instead of the raw clients. The concrete clients then live only inside closure contexts, which reflection cannot traverse, so dead-code elimination drops the unused operations.

This drops the retained operations per client from ~60 down to the 2-3 actually used (UsedInIface markers go from 244+66 to 0), shrinking both the prometheus and promtool binaries by ~3.2 MB each. No functional or API change.

Which issue(s) does the PR fix:

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

NONE

The azureClient struct held the armcompute and armnetwork SDK clients as
concrete fields while satisfying the client interface. Once such a client is
reachable through an interface, Go's linker conservatively retains every
exported method of the concrete type plus the entire (de)serializer graph those
operations drag in, even though discovery calls only a handful of them.

Wrap each SDK client in a small adapter that captures only the operations
discovery uses as method-value closures, and box the adapters instead of the raw
clients. The concrete clients then live only inside closure contexts, which
reflection cannot traverse, so dead-code elimination drops the unused
operations.

This drops the retained operations per client from ~60 down to the 2-3 actually
used (UsedInIface markers go from 244+66 to 0), shrinking both the prometheus
and promtool binaries by ~3.2 MB each. No functional or API change.

Signed-off-by: Julien Pivotto <[email protected]>
@roidelapluie
roidelapluie requested a review from a team as a code owner May 29, 2026 15:54
@roidelapluie
roidelapluie requested a review from kakkoyun May 29, 2026 15:54

@kakkoyun kakkoyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. TIL an interesting trick!

@roidelapluie
roidelapluie merged commit 3294878 into prometheus:main Jun 1, 2026
35 checks passed
@roidelapluie

Copy link
Copy Markdown
Member Author

LGTM. TIL an interesting trick!

Well it is only applicable because we use reflection

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants