News for hackers. Stuff that matters.
Re:A little Golang way by pcwalton on Wednesday August 05, 2015 @07:21PM (#10013573)
How does Rust not give you that option?

The choice of 1:1 and M:N does not affect the programming model at all. It is simply an implementation detail. M:N scheduling is no more a requirement for CSP than Unix is for TCP.

Re:A little Golang way by Scramblejams on Wednesday August 05, 2015 @08:48PM (#10013968)
Maybe I'm using terms incorrectly -- is there a good green thread library for Rust which supports the kind of Erlangy experience I've described? I looked around a couple of months ago but didn't find anything that seemed suitable.
Re:A little Golang way by pcwalton on Wednesday August 05, 2015 @09:06PM (#10014016)
Why do you need the threads to be "green"? That's what I'm asking. What's wrong with kernel threads, specifically?
Re:A little Golang way by Scramblejams on Wednesday August 05, 2015 @10:38PM (#10014248)
In my experience, kernel threads have a cost that is sufficiently far from that of, for example, Erlang processes (400+ bytes each) that it changes the way you program with them.

In Erlang you don't think twice about spawning a million threads if that models your problem nicely. The same isn't true when you're dealing with kernel threads. (Right?) So I'm interested in green threads because when I have to start thinking about the cost of the threads I'm using, then I'm thinking less about how to most elegantly solve the problem and more about how to satisfy the architecture I'm programming on.

Now, if kernel threads are massively cheap these days and there's no problem spawning a million of them, then I need to take another look at that model.

Re:A little Golang way by pcwalton on Wednesday August 05, 2015 @11:20PM (#10014366)
So since you're talking about scalability into the millions of threads, I think what you actually want is stackless coroutines rather than M:N threading with separate user-level stacks. If you have 1M threads, even one page of stack for each will result in 4G of memory use. That's assuming no fragmentation or delayed reclamation from GC. Stacks, even when relocating, are too heavyweight for that kind of extreme concurrent load. With a stackless coroutine model, it's easier to reason about how much memory you're using per request; with a stack model, it's extremely dynamic, and compilers will readily sacrifice stack space for optimization behind your back (consider e.g. LICM).

Stackless coroutines are great--you can get to nginx levels of performance with them--but they aren't M:N threading as seen in Golang. Once you have a stack, as Erlang and Go do, you've already paid a large portion of the cost of 1:1 threading.

Re:A little Golang way by Scramblejams on Thursday August 06, 2015 @01:09AM (#10014647)
Thanks for the tip, stackless coroutines are new to me. Any of that on the Rust roadmap?
Re:A little Golang way by Scramblejams on Thursday August 06, 2015 @04:50AM (#10015300)
Am I correct that coroutines are intrinsically non-preemptive? If so, I'll need to keep looking.
Re:A little Golang way by ansible on Thursday August 06, 2015 @02:22PM (#10018344)
Am I correct that coroutines are intrinsically non-preemptive? If so, I'll need to keep looking.

That's usually the case. Coroutines have their uses, but having used goroutines, that is my current preference.

Re:A little Golang way by pcwalton on Thursday August 06, 2015 @03:40PM (#10018864)
Coroutines are preemptible at I/O boundaries or manual synchronization points. Those synchronization points could be inserted by the compiler, but if you do that you're back into goroutine land, which typically isn't better than 1:1. In particular, it seems quite difficult to achieve scalability to millions of threads with "true" preemption, which requires either stacks or aggressive CPS transformation.
Re:A little Golang way by krakensden on Wednesday August 05, 2015 @10:47PM (#10014274)
A higher cost for context switching. Lots of people work on apps with lots of i/o, and there is a long history of coroutine/callback/green thread architectures beating the pants off of thread per request architectures.
Re:A little Golang way by pcwalton on Wednesday August 05, 2015 @11:02PM (#10014319)
No, the context switching overhead tends to be minimal if you're using a well-tuned kernel. You're doing a context switch to the kernel for I/O in the first place, and in a green thread model you have to do a userspace context switch in addition to the context switch the kernel imposes to get back into your scheduler.
Re:A little Golang way by acconsta on Thursday August 06, 2015 @01:59PM (#10018180)
Are there benchmarks to support that? JVM greenthreads vs. linux user threads, for example?
Re:A little Golang way by pcwalton on Thursday August 06, 2015 @03:41PM (#10018867)
Both the JVM and Linux pthreads are 1:1.