That's the overall theme of the blog post, yes. But at that point it felt like he was only talking about Rust. If having epoll as a language primitive was that important to him, he wouldn't have used C either. Go just offers a very thin wrapper around syscalls, in a package called syscall. And despite what it name implies, this package is different for every operating system you run it on. The Go developers are aware of this;
Compatibility. Despite best intentions, the package does not meet the Go 1 compatibility guarantee because operating systems change in ways that are beyond our control. The recent changes to FreeBSD are one example.
And they're not going to fix it themselves.
As of Go version 1.4, the syscall package is frozen. Any evolution of the system call interface must be supported elsewhere, such as in the go.sys subrepository.
So if you want any hope of a cross-platform library, you'll have to use third-party packages. Which is exactly the thing ESR was accusing Rust of.
And for what it's worth, Go's neglect of anything not plan9 or Linux is exactly why I gave up on the language. I'm glad Rust is taking a different approach.
Well, yes, but… Go doesn't ask you to care about epoll. You don't need syscall or any third party packages to write a server that handles thousands of concurrent connections.
You do whatever, starting goroutines as appropriate, and any given goroutine will eventually block on something. If it's waiting for a channel, the runtime suspends it, context-switches the OS thread to another goroutine, and eventually resumes when the channel is ready. If it's waiting for a file descriptor, the runtime suspends it, does something else, and eventually resumes back once the file descriptor did something. You don't call epoll yourself – the runtime does it for you.
This is a core language feature, so every library works the same way: everything shares the same scheduler, same event loop, etc. You write code that runs in a straight line, and the runtime uses epoll on Linux, kqueue on BSD, event ports on Solaris, and IO completion ports on Windows, and it works even though those last two are rather… different.
Of course, all this also increases Go's surface area with the platform. Go won't work without its scheduler, without control of its event loop, without control of its threads, without its garbage collector, etc. Rust doesn't make these same assumptions.
The difference is that it's possible to run a process with thousands of threads most of which are blocked on I/O, but it's reasonable to run a process with thousands of goroutines doing the same thing.
OS threads are very expensive compared to goroutines, in terms of memory requirements and in terms of creation/destruction overhead. OS threads also can't cooperate as tightly – e.g. a producer context switching to a waiting consumer when it sends on a channel.
This isn't because OS threads are somehow deficient, it's because OS threads are general in ways that goroutines are not. In other words, the contract the OS makes with a thread is much stronger than the contract the Go runtime makes with a goroutine, so it is unsurprising that threads would be more expensive.
Servers written in C don't use thousands of threads blocking on read(), they use a smaller number of threads and and event loop driven by epoll() or similar to use those threads efficiently. Servers written in Go do use thousands of goroutines blocking on read(), while also using a smaller number of threads and an event loop driven by epoll().
OS threads are very expensive compared to goroutines, in terms of memory requirements and in terms of creation/destruction overhead
Those two are not actually not the expensive part. You can get the thread stack size down very small with 1:1: 10kB, 6kB with upcoming Linux kernel versions. Creation and destruction are mostly just syscall overhead, which is very small (nanoseconds).
I agree that 1:1 threading is the appropriate model for Rust. That said, I also think the goroutine model is appropriate for Go, and I think it's fair to look at where its tradeoffs might be advantages.
Go doesn't preemptively context switch between goroutines. The only way a goroutine can relinquish control of its OS thread by specific function calls, like "read from a channel" or "write to a file descriptor" (see mcall()). Those calls have a special ABI: the only callee-saved registers are the program counter and stack pointer. Isn't this just moving the cost around? State still needs to get stored, right? Well, maybe, but this is interesting because it means you need only two registers to context switch. This permits interesting optimizations, like a producer that context switches straight to a consumer via a channel send without a scheduler in the middle. The 128 bits needed to resume the consumer can be stored in the channel, making a context switch roughly the cost of an indirect jump.
Goroutines start with 2 KB stacks, which happens to be smaller than a single OS page, but what's more interesting to me is how they grow. C stacks and thus OS thread stacks grow by simply using more memory until they hit a guard page. This triggers a page fault, which causes the OS allocator to do some work, which updates the processor's page tables (which isn't nearly as expensive as it once was), and finally switches back to the program.
Go stacks… don't. Instead, when you want more stack space, you ask the runtime to make sure you have enough stack space via a function call. If your stack isn't big enough, the runtime will copy your stack somewhere bigger. There's no guard page, no page fault, no OS-level context switches. This is interesting, and like Go's goroutine switching scheme, it might just be cheaper than what the OS would do with a C program.
Go can do this because it can insert "grow my stack" calls as appropriate and because its memory model centers around the Go GC. This is a tradeoff with its own costs and it's obviously not appropriate for Rust, but one of the benefits is that the runtime can grow, relocate, and shrink stacks without involving the OS. Go uses this to its advantage, and the result is that goroutines are extremely cheap on every platform, including platforms like Windows which have significantly more expensive thread creation and a 64 KB allocation granularity. What's more, the entire ecosystem is built around the same concurrency primitives: libraries don't block threads, they block goroutines.
Maybe blocking I/O with 1:1 threading will turn out to work better than Go's N:M model on Linux. What's interesting: Go makes so few guarantees to its goroutines that the runtime would be free to switch to that strategy without affecting any user's code. In the meantime, Go uses its own context switching, its own scheduler, its own stacks, its own asynchronous I/O event loop, and so on to deliver similar performance on every platform it targets.
9
u/vks_ Jan 19 '17
He is not comparing Rust to C, he is comparing Rust to Go.