3 ms·
Using the syscall interface is a lot faster in Go than calling to libc, because the system libc is going to expect large stacks and so you incur the overhead of
by pcwalton 11y ago
Using the syscall interface is a lot faster in Go than calling to libc, because the system libc is going to expect large stacks and so you incur the overhead of a stack switch when you switch out of M:N threading into the C world. I assume that's why Go calls syscalls to begin with.
- teacup50 11y agoHowever, calling directly into the syscall interface is illegal on most platforms other than Linux. I'm not sure Go can fix this without getting rid of their M:N threading.
- 4ad 11y agoOn Windows and Solaris, Go doesn't do its own syscalls.
- teacup50 11y agoHow about OS X? (and, how does that work when calling into userspace APIs that assume fixed 1:1 thread/stack correlation)
- 4ad 11y agoOn OS X Go does its own system calls, ignoring the OS X guidlines. Go uses M:N scheduling, but you still have N threads with (relatively) large stacks. When calling into C code, Go switches stacks, so it runs on the (relatively) large stack where C code can run. Of course there's some overhead associated with this, but system call overhead is large on its own compared to this anyway. During the system call the thread (obviously) can't run any other Go code, but that's ok, the runtime makes sure there are always threads available to run user Go code (that's one reason why the runtime stands between user code and system calls).
- deleted 11y ago[deleted]
- pjc50 11y agoI'm assuming you mean "unsupported" rather than "prohibited by law", although with the DMCA one can never be sure.
- zaphar 11y agoThe correct go way to do this is to use conditional compilation and hide your platform specific optimizations in a lib_{linux,darwin,...}.go file with the slower default in a file that only gets compiled if the other files don't. And indeed this is how the stdlib does it. If the rest of the go community is not adopting this practice then they aren't following good Idioms in their Go code.
- awalton 11y agoAnd I totally would have agreed with the author of this article if the tl;dr of the article was just the first sentence you wrote. Unfortunately, sanity did not prevail today.
- 4ad 11y agoNo, the stack switch overhead is trivial. As explain in my other comment, Go does system calls in order not to have any dependencies on target libraries.