Hacker News new | past | comments | ask | show | jobs | submit login

> Is there any overhead with using these bindings

Not the author, but yes. Go calling C is expensive, and that's precisely how this works. Whether or not that matters for your use case is another topic.




There's a bit of a write-up here:

https://www.cockroachlabs.com/blog/the-cost-and-complexity-o...

In their benchmark, calling a `func() {}` in Go vs a `void foo() {}` in C (via CGo) is almost 100x faster.

  $ go test -bench . -gcflags '-l'    # disable inlining for fairness
  BenchmarkCGO-8  10000000              171 ns/op
  BenchmarkGo-8   2000000000           1.83 ns/op

EDIT: And then you still have the extra overhead when using `C.CString` and `C.GoBytes` if you're passing those sorts of arguments to C.


But if the called function takes more than 200ns to execute, the overhead is less than 50%. Which probably is true for most relevant functions in an API


Go calling C is expensive compared to the normal function call overhead, but if the called function does significant work, should not introduce too much overhead.


> Go calling C is expensive

Is this true even if only primitives are passed and returned? If so, why? For comparison, Java is fast at that these days, if I understand correctly.

(Interesting related reading regarding Java: https://web.archive.org/web/20160304055443/http://nerds-cent... )




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: