-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
cmd/cgo: avoid calls to cgoCheckPointer when debug.cgocheck=0 #28454
Comments
I see the advantage but I'm not excited about encouraging people to use |
We could file a separate issue for making cgocheck=1 faster, but as it stands, it has a pretty substantial cost. I measured ~50ns per checked argument in the trivial case ( In these environments, it makes much more sense to run with cgocheck=2 during development, but to use cgocheck=0 in production. |
Just to clarify, I'm not excited about it either and would rather see a check that has close to zero cost. The other things I thought that might be possible are:
However, all of these seem to need significantly more effort than this change, but also are complimentary to the external check. |
I did a bunch of experiments on https://github.com/egonelbre/exp/blob/master/bench/call/cgo.go#L50. Experiments:
PS: note, my machine is somewhat noisy, so take the results with a grain of salt. ResultsRaw results https://gist.github.com/egonelbre/fe11fc2a3bf1617e1e14dfc562bb3e2a Baseline vs disabling code generation:
Baseline vs outer if:
Baseline vs
Baseline vs combined:
|
Change https://golang.org/cl/198081 mentions this issue: |
Change https://golang.org/cl/226342 mentions this issue: |
@ianlancetaylor I finally thought it would be nice to get this closed one way or another :). I made a proof-of-concept change in https://go-review.googlesource.com/c/go/+/226342. I guess the main question is, whether it should be done at all. The performance improvements are significant:
Should I try to finish the CL properly and add missing parts from gcc-go or is it better to drop this issue altogether? |
Sorry, but I'm still not at all persuaded that we should try to make That said I think the CL would be simpler if you introduce a new function func cgoMuchCheckPointers() {
return debug.cgocheck != 0
} and call that. The compiler should inline that into the calling code. (If it doesn't, let's find out why not.) |
Fair enough, I won't bother with making But, while doing this, I also realized why the array/slices cases are still so slow. Here's a reproducer, where a single call takes 15ms:
Cgo ends up generating this code:
I'll look into fixing this and then use it as a resolution for this issue. |
Change https://golang.org/cl/226517 mentions this issue: |
Have been any updates about this? |
No updates so far, I got stuck trying to make it work with gccgo and haven't taken the time to fix it. |
I use |
@Zyl9393 As far as I can tell your "create false positives" link is actually describing true positives: cases where cgocheck is correctly warning. In Go an invalid pointer must never have pointer type. Doing so will break the garbage collector. |
@ianlancetaylor You are technically 100% correct. The problem is that for the purpose of getting work done today (and, for that matter, 6 years ago), this 100% gets in the way and needs to be disabled. I am of the opinion that half of the problems people are experiencing with Go can be fixed by improving the quality of the projects using it; we desperately need more of that to be happening, alas it does not appear to be the kind of work a community of FOSS hobbyists can accomplish. Just a thought. /digress |
With
DEBUG=cgocheck=0
Go still makes calls tocgoCheckPointer
which will bail out early ingo/src/runtime/cgocall.go
Line 412 in 8f4fd3f
https://golang.org/cl/142884 changes cgo generated code to:
I propose, instead of checking
debug.cgocheck=0
insidecgoCheckPointer
it would check it before callingcgoCheckPointer
, so cgo would generate:The text was updated successfully, but these errors were encountered: