Why cgo's performance is so slow? is there something wrong with my testing code?

后端 未结 3 721
情歌与酒
情歌与酒 2020-12-13 16:25

I\'m doing a test: compare excecution times of cgo and pure Go functions run 100 million times each. The cgo function takes longer time compared to the Golang function, and

3条回答
  •  情歌与酒
    2020-12-13 16:31

    As you've discovered, there is fairly high overhead in calling C/C++ code via CGo. So in general, you are best off trying to minimise the number of CGo calls you make. For the above example, rather than calling a CGo function repeatedly in a loop it might make sense to move the loop down to C.

    There are a number of aspects of how the Go runtime sets up its threads that can break the expectations of many pieces of C code:

    1. Goroutines run on a relatively small stack, handling stack growth through segmented stacks (old versions) or by copying (new versions).
    2. Threads created by the Go runtime may not interact properly with libpthread's thread local storage implementation.
    3. The Go runtime's UNIX signal handler may interfere with traditional C or C++ code.
    4. Go reuses OS threads to run multiple Goroutines. If the C code called a blocking system call or otherwise monopolised the thread, it could be detrimental to other goroutines.

    For these reasons, CGo picks the safe approach of running the C code in a separate thread set up with a traditional stack.

    If you are coming from languages like Python where it isn't uncommon to rewrite code hotspots in C as a way to speed up a program you will be disappointed. But at the same time, there is a much smaller gap in performance between equivalent C and Go code.

    In general I reserve CGo for interfacing with existing libraries, possibly with small C wrapper functions that can reduce the number of calls I need to make from Go.

提交回复
热议问题