3 ms·
You wrote that Unity FFI is much faster than cgo. I'd be interested in knowing more about this. Why is it faster? Can you share a link to some reference materia
by ngrilly 8y ago
You wrote that Unity FFI is much faster than cgo. I'd be interested in knowing more about this. Why is it faster? Can you share a link to some reference material and/or benchmark?
- dilap 8y agogood question! i'm not an expert. my understanding is go is slow because each function call has to copy over to a bigger stack (goroutines have tiny stacks to start, and grow on demand, but c code can't do that, natch) and because it has to tell the goroutine scheduler some stuff for hazy reasons. this github issue has a lot of interesting discussion of go ffi: https://github.com/golang/go/issues/16051 https://github.com/golang/go/issues/16051 these benchmarks https://github.com/dyu/ffi-overhead https://github.com/dyu/ffi-overhead seem to show that a c call via cgo will be about 14 (!) times slower than a c call from c# in mono, which itself is about 2 times slower than just a plain c call.
- ngrilly 8y agocgo doesn't copy the stack to a "bigger stack" when calling a C function. It just uses another stack, dedicated to cgo, and disallows passing pointers into the stack: https://github.com/golang/go/issues/11089 https://github.com/golang/go/issues/11089 So I think the slowness is caused by something else. Thanks for sharing the benchmark.
- dilap 8y agoah, that makes more sense. thanks.