You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What version of Go are you using (go version)? go1.6
What operating system and processor architecture are you using (go env)? linux/amd64
What did you do?
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
Neither implementation appears to allocate anything in the Foo function. So I expect go test -bench to report zero allocations for both implementations of the package.
What did you see instead?
For the first example (hC-b5VSG58) the command go test -bench=. -benchmem reports two allocations (48 bytes total) when the C function is passed a non-nil argument.
$ go test -bench=. -benchmem
testing: warning: no tests to run
PASS
BenchmarkFoo-4 5000000 317 ns/op 48 B/op 2 allocs/op
BenchmarkFoo_nil-4 10000000 164 ns/op 0 B/op 0 allocs/op
ok github.com/bmatsuo/lmdb-go/tmp/cgo-allocs 3.718s
The second example avoids any allocation.
$ go test -bench=. -benchmem
testing: warning: no tests to run
PASS
BenchmarkFoo-4 10000000 151 ns/op 0 B/op 0 allocs/op
BenchmarkFoo_nil-4 10000000 151 ns/op 0 B/op 0 allocs/op
ok github.com/bmatsuo/lmdb-go/tmp/cgo-allocs 3.345s
The first example does see a performance impact, I assume because of the allocations.
The text was updated successfully, but these errors were encountered:
ianlancetaylor
changed the title
cgo: unexpected allocations when passing void pointers
cmd/cgo: unexpected allocations when passing void pointers
Mar 31, 2016
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.
That mostly makes sense. But you are saying it is allocating space on the heap for the 24-byte slice header (pointer + 2 ints)? It seems strange that a slice header can "escape".
go version
)? go1.6go env
)? linux/amd64I 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
http://play.golang.org/p/hC-b5VSG58
http://play.golang.org/p/soCMGF1WjG
And the following benchmark
http://play.golang.org/p/e37ObBxwJP
Neither implementation appears to allocate anything in the Foo function. So I expect
go test -bench
to report zero allocations for both implementations of the package.For the first example (hC-b5VSG58) the command
go test -bench=. -benchmem
reports two allocations (48 bytes total) when the C function is passed a non-nil argument.The second example avoids any allocation.
The first example does see a performance impact, I assume because of the allocations.
The text was updated successfully, but these errors were encountered: