Skip to content

Reduce build size with dead code elimination#1542

Draft
zemzale wants to merge 3 commits intomasterfrom
codex/novalidatefn-dead-code-elimination
Draft

Reduce build size with dead code elimination#1542
zemzale wants to merge 3 commits intomasterfrom
codex/novalidatefn-dead-code-elimination

Conversation

@zemzale
Copy link
Member

@zemzale zemzale commented Mar 14, 2026

Based on discussion in 1496 a experimental change to allow reducing the build sizes.

This adds a build tag novalidatefn that makes any usage of validateFn panic. The idea is that we can eliminate calls to tryCallValidateFn that in turn uses field.MethodByName that can't be dead code eliminated.

This is more of POC for now, with showing how this could be done. If we are okay with this change I will update the docs and any other thing that is required for this to actually ship.

@zemzale zemzale requested a review from a team as a code owner March 14, 2026 10:18
@zemzale zemzale marked this pull request as draft March 14, 2026 10:44
@vincentbernat
Copy link

I think the build tag should be validator_novalidatefn to avoid collision.

@sverdlov93
Copy link

sverdlov93 commented Mar 18, 2026

We ran into this exact issue at JFrog while optimizing our Go service binary. Here's our analysis confirming the impact:

Our setup: ~1,480 packages, 99 MB binary (Linux amd64). We use validator.New() with custom tags but never use validateFn.

What whydeadcode shows on our binary today:

github.com/go-playground/validator/v10.tryCallValidateFn reachable from:
   github.com/go-playground/validator/v10.isValidateFn
   github.com/go-playground/validator/v10.isValidateFn·f
   github.com/go-playground/validator/v10.map.init.11
   github.com/go-playground/validator/v10.bakedInValidators
   github.com/go-playground/validator/v10.New

Reviewed the code in this PR — it addresses the issue correctly. The MethodByName call moves into no_validate_fn.go (default build), and -tags validator_novalidatefn compiles only the panic stub. The registration in baked_in.go stays in both paths, but the stub prevents MethodByName from appearing in the linker's reachability graph.

The build tag naming validator_novalidatefn is good — prefixed with the package name, consistent with how gin does nomsgpack. Would be great to see this land!

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.

3 participants