Skip to content

discovery/gce: keep Compute SD client from defeating dead-code eliminationation - #18819

Merged
roidelapluie merged 1 commit into
prometheus:mainfrom
roidelapluie:roidelapluie/gce-sd-binary-size
May 29, 2026
Merged

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

Conversation

@roidelapluie

Copy link
Copy Markdown
Member

The Discovery struct held *compute.Service and *compute.InstancesService as fields and is boxed into the discovery.Discoverer interface. Once reflection is reachable in the program (it always is, via the YAML/config machinery), the Go linker conservatively retains every exported method of any concrete type reachable through an interface, including via struct fields. *compute.Service exposes ~150 sub-services and their operations, so all of them — 994 list/get operations and their serializers — were retained even though discovery only calls Instances.List.

Wrap the single used operation in a closure over the concrete *compute.Service so the service lives only in closure context, which reflection cannot traverse. Returning a *compute.InstancesListCall would not help, since that type has an s *Service back-reference that re-propagates the marker, so the closure encapsulates the whole List/Filter/Pages chain and only exposes the *compute.InstanceList data type discovery already uses.

compute/v1 footprint drops from ~4.9 MB to ~33 KB, and the prometheus and promtool binaries each shrink by ~13.5 MB. 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

@roidelapluie
roidelapluie requested a review from a team as a code owner May 29, 2026 11:55
@roidelapluie
roidelapluie requested a review from bitfehler May 29, 2026 11:55
@roidelapluie
roidelapluie force-pushed the roidelapluie/gce-sd-binary-size branch from fd4a5eb to eaf30be Compare May 29, 2026 11:56

@bwplotka bwplotka 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.

Thanks!

Comment thread discovery/gce/gce.go Resolved
…ation

The Discovery struct held *compute.Service and *compute.InstancesService as
fields and is boxed into the discovery.Discoverer interface. Once reflection is
reachable in the program (it always is, via the YAML/config machinery), the Go
linker conservatively retains every exported method of any concrete type
reachable through an interface, including via struct fields. *compute.Service
exposes ~150 sub-services and their operations, so all of them — 994 list/get
operations and their serializers — were retained even though discovery only
calls Instances.List.

Wrap the single used operation in a closure over the concrete *compute.Service
so the service lives only in closure context, which reflection cannot traverse.
Returning a *compute.InstancesListCall would not help, since that type has an
s *Service back-reference that re-propagates the marker, so the closure
encapsulates the whole List/Filter/Pages chain and only exposes the
*compute.InstanceList data type discovery already uses.

compute/v1 footprint drops from ~4.9 MB to ~33 KB, and the prometheus and
promtool binaries each shrink by ~13.5 MB. No functional or API change.

Signed-off-by: Julien Pivotto <[email protected]>
@roidelapluie
roidelapluie force-pushed the roidelapluie/gce-sd-binary-size branch from eaf30be to fd493fe Compare May 29, 2026 12:00
@roidelapluie
roidelapluie enabled auto-merge May 29, 2026 12:25
@roidelapluie
roidelapluie merged commit 0d7c6fa into prometheus:main May 29, 2026
35 checks passed
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