-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/go: optimize CGO caching and dead code removal #38886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
One C file is generated for each Go file that uses |
Related #9887 Currently, I think we cache the generated We should have a separate action for each |
Note that proper caching of C object files require detecting whether any included .h files change. This does arise when people update to a newer version of some C dependency. |
Good point. We should be incorporating hashes of included .h files into the cache keys for compiled .a files at least. I don't believe we currently are. |
Maybe we could only run the C compiler when the package is actually being compiled in order to get the values of C constants and make sure C functions are called properly. |
We run the C linker, rather than using only the Go linker, because the Go linker doesn't support C++ global constructors or C++ exception handling or a few other cases that the C linker handles. We could support those things in the Go linker, but it's not trivial. |
@Keithcat1 How you solved this issue? |
By putting CGO code in sub-packages so if you make a change to the main package, it won't recompile those packages that use CGO unless you changed them |
The Go linker does support reading C object files in various formats (ELF, Mach-O, PE, etc.) on some platforms. But as @ianlancetaylor pointed out, the support of C linking is limited, and it doesn't support fancy C/C++ features. If you're sure your C code doesn't use any of those, and you're targeting a platform that supports internal linking, you can try passing That does not get deadcode elimination on C code automatically, as C object semantics is section based, not function based. Perhaps you could try using C compiler options |
I'm writing this with the assumption that each CGO-generated wrapper for each C function is stored in its own C file.
Editing and compiling a CGO package can take a minute or 2, even for one little change.
It should be possible to cache the generated .o files and only recompile them if the code for that function changes.
When linking the CGO wrappers, Go should avoid linking unused wrappers to reduce size.
The text was updated successfully, but these errors were encountered: