Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't call MethodByName with a variable arg
Go 1.22 goes somewhat toward addressing the issue using reflect MethodByName disabling linker deadcode elimination (DCE) and the resultant large increase in binary size because the linker cannot prune unused code because it might be reached via reflection. Go Issue golang/go#62257 reduces the number of incidences of this problem by leveraging a compiler assist to avoid marking functions containing calls to MethodByName as ReflectMethods as long as the arguments are constants. An analysis of Uber Technologies code base however shows that a number of transitive imports still contain calls to MethodByName with a variable argument, including GORM. In the case of GORM, the solution we are proposing is because the number of possible methods is finite, we will "unroll" this. This demonstrably shows that GORM is not longer a problem for DCE. Before ``` % go version go version devel go1.22-2f3458a8ce Sat Sep 16 16:26:48 2023 -0700 darwin/arm64 % go test ./... -ldflags='-v=2' 2> >(grep -i 'deadcode: giving up') deadcode: giving up on static analysis due to relect method symbol gorm.io/gorm/schema.ParseWithSpecialTableName<3938> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).Method<119948> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).MethodByName<119949> deadcode: giving up on static analysis due to relect method symbol gorm.io/gorm/schema.ParseWithSpecialTableName<49693> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).Method<117898> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).MethodByName<117899> deadcode: giving up on static analysis due to relect method symbol gorm.io/gorm/schema.ParseWithSpecialTableName<54029> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).Method<117635> deadcode: giving up on static analysis due to relect method symbol reflect.(*Value).MethodByName<117636> ok gorm.io/gorm (cached) ok gorm.io/gorm/callbacks (cached) ? gorm.io/gorm/migrator [no test files] ok gorm.io/gorm/clause (cached) ok gorm.io/gorm/logger (cached) ? gorm.io/gorm/utils/tests [no test files] ok gorm.io/gorm/schema (cached) ok gorm.io/gorm/utils (cached) % ``` After ``` % go version go version devel go1.22-2f3458a8ce Sat Sep 16 16:26:48 2023 -0700 darwin/arm64 %go test ./... -ldflags='-v=2' 2> >(grep -i 'deadcode: giving up') ? gorm.io/gorm/migrator [no test files] ? gorm.io/gorm/utils/tests [no test files] ok gorm.io/gorm 0.474s ok gorm.io/gorm/callbacks 0.372s ok gorm.io/gorm/clause 0.605s ok gorm.io/gorm/logger (cached) ok gorm.io/gorm/schema 0.689s ok gorm.io/gorm/utils (cached) % ```
- Loading branch information