Why cgo is slow @ CapitalGo 2018

Why cgo is slow @ CapitalGo 2018

https://capitalgolang.com/program#filippo_valsorda

This talk uses cgo and its below-average performance as an excuse to look into Go internals and what makes Go different from C.

We learn about calling conventions and the code-generated cgo trampolines; about the small goroutines stacks and how C doesn't know how to grow them; about the Go scheduler and how C doesn't yield to it; and about the garbage collector and how pointers in C memory can't be tracked.

9fdab9d005b82612cadbfe699b541f83?s=128

Filippo Valsorda

June 22, 2018
Tweet

Transcript

  1. 2.
  2. 4.

    I like FFIs. • From cgo back to Go @

    GopherCon 2016
 https://speakerdeck.com/filosottile/from-cgo-back-to-go-gophercon-2016 • rustgo: Building your own FFI @ GothamGo 2017
 https://speakerdeck.com/filosottile/calling-rust-from-go-without-cgo-at-gothamgo-2017 • Why cgo is slow @ CapitalGo 2018
 Hi!
  3. 5.

    C function call Java FFI Rust FFI LuaJIT FFI Node.js

    FFI cgo 2.364 ns 9.01 ns 2.386 ns 1.81 ns (!) https://nullprogram.com/blog/2018/05/27/ 18.33 ns 75.95 ns https://github.com/dyu/ffi-overhead
  4. 7.

    C function call Java FFI Rust FFI LuaJIT FFI Node.js

    FFI cgo 2.364 ns 9.01 ns 2.386 ns 1.81 ns (!) https://nullprogram.com/blog/2018/05/27/ 18.33 ns 68.77 ns https://github.com/dyu/ffi-overhead
  5. 8.

    C function call Java FFI Rust FFI LuaJIT FFI Node.js

    FFI cgo 2.364 ns 9.01 ns 2.386 ns 1.81 ns (!) https://nullprogram.com/blog/2018/05/27/ 18.33 ns 68.77 ns (29x) https://github.com/dyu/ffi-overhead
  6. 11.

    cgo: • cmd/cgo — a code generator • runtime/cgo •

    a sprinkle of cmd/link/internal/ld support • not a compiler feature!
  7. 15.
  8. 16.
  9. 17.
  10. 18.
  11. 20.
  12. 21.
  13. 22.
  14. 24.

    Learn more: • src/runtime/cgocall.go • rustgo: Building your own FFI

    @ GothamGo 2017
 https://speakerdeck.com/filosottile/calling-rust-from-go-without-cgo-at-gothamgo-2017
  15. 27.
  16. 29.
  17. 30.
  18. 31.
  19. 32.
  20. 33.
  21. 35.

    C doesn't call morestack C code needs to run on

    a system stack cgocall / asmcgocall
  22. 36.

    Learn more: • src/runtime/stack.go • src/runtime/cgocall.go • How stacks are

    handled in Go by Daniel Morsing
 https://blog.cloudflare.com/how-stacks-are-handled-in-go/
  23. 40.

    The Go scheduler is collaborative. It can't preempt running code.

    (ProTip: for {} is never what you want. Use select {}.)
  24. 41.
  25. 43.

    Learn more: • src/runtime/proc.go → reentersyscall • The Go scheduler

    by Daniel Morsing
 https://morsmachine.dk/go-scheduler • Performance without the event loop by Dave Cheney
 https://dave.cheney.net/2015/08/08/performance-without-the-event-loop
  26. 49.

    The cgo rules You may pass a Go pointer …

    if it doesn’t point to other pointers … and C can’t keep a reference to it
  27. 52.

    Learn more: • From cgo back to Go @ GopherCon

    2016
 https://speakerdeck.com/filosottile/from-cgo-back-to-go-gophercon-2016
  28. 53.

    Thank you! filippo@golang.org @FiloSottile Olga Shalakhina artwork under CC 3.0

    license based on Renee French under Creative Commons 3.0 Attributions.