News for hackers. Stuff that matters.
Re:Purego – A library for calling C functions from Go withou... by slimsag on Sunday February 12, 2023 @11:44AM (#34764246)
Very cool! I will definitely give this a try, I've been looking to build Go bindings to Mach[0] soon.

It looks like this would make cross-compiling CGO easier (no target C toolchain needed?)

Does this do anything to reduce the overhead of CGO calls / the stack size problem? IIRC the reason CGO overhead exists is at least partly because goroutines only have an ~8k stack to start with, and the C code doesn't know how to expand it-so CGO calls "must" first have the goroutine switched to an OS thread which has an ~8MB stack.

One reason I think Go <-> Zig could be a fantastic pairing is that Zig plans to add a builtin which tells you the maximum stack size of a given function[1], so you could grow the goroutine stack to that size and then call Zig (or, since Zig an compile C code, you could also call C with a tiny shim to report the stack required?) and then eliminate the goroutine -> OS thread switching overhead.

[0] https://github.com/hexops/mach

[1] https://github.com/ziglang/zig/issues/157

Re:Purego – A library for calling C functions from Go withou... by totallygamerjet on Sunday February 12, 2023 @12:02PM (#34764450)
Contributor here: Purego doesn’t do anything to improve the overhead of calling into C. It uses the same mechanisms that Cgo does to switch to the system stack and then call the C code. Purego just avoids having to need a C toolchain to cross compile code that calls into C from Go.

I’ve actually been quite interested in Zig. If that built-in was added than it would likely be possible to grow the goroutine stack to the proper size and than call the Zig code. Very interesting stuff!

Re:Purego – A library for calling C functions from Go withou... by slimsag on Sunday February 12, 2023 @12:09PM (#34764524)
Makes sense! I also wonder (if you know): last I looked I recall that each CGO call requires switching to the system stack, but I can't recall what happens after. Does it switch back to a regular goroutine stack once the syscall has completed?

I wonder if a more tailored CGO implementation could pin a goroutine to a thread which is guaranteed to have a system stack available, so that each CGO call need not worry about that switching at all. Maybe that'd require runtime changes though?

Re:Purego – A library for calling C functions from Go withou... by totallygamerjet on Sunday February 12, 2023 @12:16PM (#34764611)
Yeah the default behavior is to switch back. It’s possible to pin a goroutine to a thread with runtime.LockOSThread(). However I don’t believe it avoids the stack switching. It’s purpose it to make sure that Thread Local Storage works properly. The runtime is pretty smart though so it might already do the optimization you suggested in someway. I know it has a goroutine specifically for monitoring if a thread is stuck in a external call and therefore spawns a new thread to continue work (sysmon)
Re:Purego – A library for calling C functions from Go withou... by slimsag on Sunday February 12, 2023 @12:23PM (#34764677)
Ahh that's right, I see.

Maybe I should play with removing the stack switching from purego so that under condition of a locked OS thread you can avoid that overhead :) I might give that a shot sometime

Re:Purego – A library for calling C functions from Go withou... by EdiX on Monday February 13, 2023 @07:42AM (#34772779)
All threads have a system stack available at all times.
Re:Purego – A library for calling C functions from Go withou... by voxtech on Monday February 13, 2023 @10:55AM (#34775120)
Stack switching isn't that much of the overhead. "ordinary" cgo overhead is <100ns now, has been for a few years, and is much closer to 30 than 80 on recent processors. Most of the overhead is a set of 4 CAS operations (incidentally this means that AMD has measurably lower cgo overhead because of something with its caching model I don't understand).

If cgo's only overhead was the "ordinary" overhead, most people wouldn't have an issue with it. It's downright zippy, in fact... as long as your syscall/C call takes less than 1us. If you stay under the 1us threshold, go will put the OS thread used for the syscall back where it found it and everything moves on.

The issue is that the OS thread was previously serving N goroutines that other parts of the program may be waiting on to move forward, and the OS thread is in a state where go can't pre-empt it and allow those other goroutines to move forward, and it has no idea how long it will be until it can move forward.

As a result, if a syscall/c call takes longer than 1us, go has no choice at this time but to resume a new thread, context switch all the old work onto that thread, and then suspend the syscall thread when it comes back. If you do this a lot, your performance will crater.

There's also separately a few issues around how go chooses to resume/suspend OS threads (for instance, if an os-locked goroutine does coooperative park for any reason to wait on another thread to do something, go will suspend the thread it was on, context switch to a different thread, then when the goroutine wakes up, it will realize its mistake, resume the thread it was on and context switch again).

This is all fixable stuff, but all the use cases that google cares about are working fine so it doesn't really get any attention.

Re:Purego – A library for calling C functions from Go withou... by TheLonelyGecko on Sunday February 12, 2023 @11:53AM (#34764362)
Nice, just-in-time goroutine -> OS thread switching.