Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/cgo: unexpected allocations when passing void pointers #15048
I was helping to debug several unaccounted for allocations and we came across what we thought was very peculiar behavior. Consider the following two implementations of the same package
And the following benchmark
Neither implementation appears to allocate anything in the Foo function. So I expect
For the first example (hC-b5VSG58) the command
The second example avoids any allocation.
The first example does see a performance impact, I assume because of the allocations.
The difference is that in the void* case, cgo doesn't figre out that the value does not point to a value that contains a Go pointer, and therefore inserts a check call. In the char* case, cgo can see that the type C.char can not contain a pointer, and therefore there is no need to use a check call. The check call causes the pointer to appear to escape, forcing buf to be allocated on the heap.