News for hackers. Stuff that matters.
Re:Performance of green threads and why it matters by hellofunk on Sunday May 08, 2016 @04:32AM (#11653219)
>First of all, go() statement in Go language does parallelism in addition to simple green-threading. With all the talk about 'concurrency is not parallelism' that seems to be part of Go's DNA, I find the design decision to support both using a single statement a bit weird.

Have to agree on this, and often wonder the same thing.

Re:Performance of green threads and why it matters by eikenberry on Sunday May 08, 2016 @02:22PM (#11655070)
That's because parallelism is an optimization, not a programming abstraction. You might as well ask why function calls and inlined function calls don't have different statements. They certainly can (eg. C's inline keyword) but it is not necessary nor that important to inlining as an optimization.
Re:Performance of green threads and why it matters by ruggeri on Sunday May 08, 2016 @06:26PM (#11656287)
One difference is that inlining is a safe optimization in most contexts.

Parallelizing a goroutine is not safe if the goroutine shares memory. Obviously that's not the default Go style, but sometimes sharing state is unavoidable.

So that would be one point in favor of different syntax.

Re:Performance of green threads and why it matters by themartorana on Sunday May 08, 2016 @11:47AM (#11654491)
I'm having a hard time understanding where you would want concurrency without the benefit of parallelism where available? I also personally consider the idea of single-core concurrency as "not parallelism" to be semantics. It may not be truly parallel, but both routines are getting CPU time in the same algorithm, so, in terms of programmatic considerations, to me the two (at least with Go) are basically the same.
Re:Performance of green threads and why it matters by barrkel on Sunday May 08, 2016 @01:27PM (#11654817)
Concurrency models the nondeterminism of the world outside the program. Parallelism is a performance improvement, at the cost of synchronisation for shared data. The two are orthogonal. Many programs are better written using concurrent primitives; whether they are better of with parallelism is a separate optimisation subject to tradeoffs, like all optimisations. If you can live without preemption, a single threaded world is no bad thing for correctness. Everything that touches the outside world (blocks) may be a green thread switch point though, so it's no panacea.

Without hardware executing code in parallel, it's not parallelism, BTW. It's not semantics; it's specific jargon that means something.

Re:Performance of green threads and why it matters by cfallin on Sunday May 08, 2016 @12:26PM (#11654596)
Concurrency implies multiple partial computations in progress, but not necessarily that they run at the same time -- some models (e.g. a single-threaded event loop, like Node's) explicitly guarantee mutual exclusion and define certain preemption points. Cooperative multitasking, single-threaded event loops, and futures/promises (when not pushed to a thread pool) are good examples.

(I've also seen definitions of concurrency that do not imply the above, but I think the above definition is more in line with the distinction people try to make...)

Re:Performance of green threads and why it matters by justincormack on Sunday May 08, 2016 @12:07PM (#11654551)
So you don't need locking.
Re:Performance of green threads and why it matters by wyager on Sunday May 08, 2016 @12:18PM (#11654574)
Non-parallel (perpendicular?) green threads are semantically indistinguishable from parallel green threads. If you need locks with parallelism, you need them with green threads. Any locking unique to parallelism is handled by the runtime.
Re:Performance of green threads and why it matters by twotwotwo on Sunday May 08, 2016 @04:12PM (#11655532)
I like eikenberry's explanation of why in the sense of "what was the logic"; I speculated some on practical tradeoffs over on lobste.rs:

https://lobste.rs/s/c8pxgu/performance_green_threads_why_it_...

Re:Performance of green threads and why it matters by jeffreyrogers on Sunday May 08, 2016 @11:40AM (#11654456)
Here's a good (introductory) resource on implementing green threads: http://c9x.me/art/gthreads/intro.html. It's helpful for learning how they work in practice.

Are people running into performance issues with green threads in practice? It seems like they're already pretty performant, particularly if you're using them more as an abstraction for concurrency than as a way to get parallelism. There are lots of problems that are trivial to solve with green threads/coroutines but that are very difficult without them. If you really care about performance you probably shouldn't be using green threads at all but should instead be running separate processes on dedicated cores. Green threads also have the advantage over kernel threads in that they don't require the overhead of a kernel context switch, since the context switch can be done in userspace, which requires saving much less context and also affects your caches less.

Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @12:23PM (#11654588)
> There are lots of problems that are trivial to solve with green threads/coroutines but that are very difficult without them.

Name at least one that cannot be solved better without them.

Re:Performance of green threads and why it matters by rumcajz on Sunday May 08, 2016 @12:33PM (#11654622)
Network stacks. And pipelines in general.
Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @12:40PM (#11654645)
Be specific. Network stacks and pipelines are easier even with the most basic event loop, because you simply don't have to think about concurrent memory access whatsoever. But you do with green threads and coroutines.
Re:Performance of green threads and why it matters by barrkel on Sunday May 08, 2016 @01:16PM (#11654767)
Event loops lose your call stack; you need to rewrite your program as a state machine so you know where to pick back up; or you need to rewrite your program in continuation passing style with callbacks, like JavaScript; or you automate that CPS transform, and you've just reinvented green threads again.
Re:Performance of green threads and why it matters by Matthias247 on Sunday May 08, 2016 @05:46PM (#11656057)
I agree with what you (and the others) say.

Regarding green threads and state machines I would however add that while green threads allow to ditch some state machines (e.g. ones that wait until a complete packet was received) you won't get rid of all of them, e.g. you will still need state machines for lots of protocol implementations. E.g. if you are implementing the websocket protocol or HTTP/2, you have some interaction between incoming data, data that must be sent out and requests from the [library] user. The state of the connection depends on all of these, so you need some kind of connection state machine which would run most likely in it's own green thread (in addition to a sending thread and a receiving thread and a user thread). And now you have to implement clean synchronization between all of those threads. The lack of necessary synchronization (and the ability to observe a huge amount of state at once without danger) can make the implementation a little bit more easy in my experience. But here I also have to agree with strmpnk that a good design is necessary for both approaches, and accessing lots of shared mutable data in multiple callbacks is probably also not the cleanest approach.

Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @05:51PM (#11656093)
There is a very big difference between implicit flow control, that you have with green threads and explicit one, that you have with "reinvented green threads", as you call them. First one will force you to think about synchronization to access your memory, but the second one - won't. It will also let you do clean cancellation, etc., that you won't be able to do with green thread.
Re:Performance of green threads and why it matters by strmpnk on Sunday May 08, 2016 @01:16PM (#11654765)
A lot of the edge cases like back pressure and certain kinds of state machines are a lot easier to represent as function calls rather than an asynchronous events. Most of the good implementations of these ideas with events have some sort of manually managed stack-equivalent device, which is exactly what a green thread already is.

So from there the benefits are around being able to use the sequential features of the language in combination (error handling, flow control, &c). Stackless transformations are a middle ground but there are plenty of things that become inconvenient without green thread stacks which make composition much easier.

Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @01:40PM (#11654873)
> which is exactly what a green thread already is.

No, it's not. See my other reply.

> there are plenty of things that become inconvenient without green thread stacks

There are no such things. Because you can have all of the sequential code you want, but without any of the downsides of threads.

Re:Performance of green threads and why it matters by strmpnk on Sunday May 08, 2016 @02:29PM (#11655104)
I think we have different definitions then. A thread (green or not) is a context of execution which includes a stack. Saying that it's not what it is seems like you might be thinking of stackless generators or something, which are most certainly not green threads as it's a shallow control switch.

I've spent plenty of time with both green threaded languages and non-green threaded languages. There is a most definitely a difference. A lot of the critiques you've given apply to event loops as well.

For example, data access, sure you can't write half a value but interleaving callbacks for work broken up into multiple stages is just as complex in terms of interleaved flows and would require the same careful treatments you would need in threaded code. The solution there is not to share data, even with events. I'm sure you implicitly do this yourself but there is nothing that makes either better or worse at it.

What you've failed to address are all of the trade-offs event loops have, like forgetful stack traces (and any effective fix keeps a stack of event flow contexts on the side, ironically).

In reality neither green threads or event loops are all that amazing on their own. Great runtimes need a whole lot more, so it seems kind of silly to sit on one side of the fence shouting that there are no downsides on your side of things because there obviously are plenty of problems worth solving better in both camps.

Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @05:42PM (#11656028)
Thread from a programming perspective is pretty much anything, that shares memory and has an implicit context switching. Which is very different from other, mostly event-loop based, concepts, that don't give up control implicitly. It is very important not to mix them. Even something that looks like a thread, but can only switch context when there is a special keyword, is not a thread anymore, and unlike a thread is very precise of what can and cannot be executed out of order and access memory willy-nilly. I actually implemented a C-dialect DSL once, that did basically this, i.e. added to C reenterable functions with heap allocated context and an event loop to run them. I eventually abandoned the project and the idea, as synchronous code isn't that good to work with. Even Erlang's "synchronous" processes receive messages in an event driven fashion.

I think you didn't have a pleasure to work with event loops in languages with first-class functions, that's why you think they are on the same boat with green thread. They are not, of course. There are no trade-offs. You have multiple scopes to share memory from, one for each handler, that can execute only atomically, one for all handlers created by that function, that may execute out of order, and a global scope of course. You can compose functions, give them names and what not. You can even have a console, like Erlang does.

I've tried like a dozen of concurrency models, you cannot really convince me, that threads are any good for that.

Re:Performance of green threads and why it matters by bobinator606 on Monday May 09, 2016 @08:30AM (#11659245)
programs that use lots of signals and/or asynchronous completion of IO or timers ("completions")

completions create all sorts of concurrency problems because they are delivered to the program without any concurrency protection, and writing such protection correctly turns out to be non-trivial.

By delivering completions into coroutines, you gain all the concurrency protection that is offered by the green threads, without having to write it from scratch.

Re:Performance of green threads and why it matters by zzzcpan on Monday May 09, 2016 @09:29AM (#11659644)
> programs that use lots of signals and/or asynchronous completion of IO or timers ("completions")

This is actually what the most basic event loop handles for you. OS signals, timers and asynchronous events are not delivered into the middle of your program, but into the point, where all of the event handlers are not running. That's the beauty of it. You can then react to the event and cancel whatever you are waiting to be running, or schedule something new.

With coroutines you can only safely handle those things each in a separate coroutine. But you don't need them isolated, you need your code to actually do something on those signals and timer events. So, you end up with a lot of flags spread all over other coroutines, protected by mutexes/channels. But then it's still not responsive, because you cannot unblock a currently blocking call, so many concurrency patterns do not apply and even bad things happen, like leaks and deadlocks. You rethink it again and move blocking calls each into a separate coroutine, so you would be able to construct a loop that waits for messages from other coroutines and reacts right away, abandoning blocking coroutines, if required. Hopefully you eventually realize, that you are emulating an event loop, but a very clumsy one, and go back to working with event handlers and higher-order functions.

Re:Performance of green threads and why it matters by auxym on Sunday May 08, 2016 @12:06PM (#11654547)
Why processes vs os-level threads?
Re:Performance of green threads and why it matters by learc83 on Sunday May 08, 2016 @01:22PM (#11654787)
The OP said processes on dedicated cores, so I guess they meant you should use processes if you need fewer threads than you have available cores.

If that is the case then processes could be faster than OS threads because you'd avoid context switches altogether. However, if your system supports pinning OS threads to processors and you limit the number of threads to the number of available processors, I can't think why that wouldn't be just as good.

Re:Performance of green threads and why it matters by halayli on Sunday May 08, 2016 @01:41PM (#11654874)
Goroutines use segmented stacks that allows them to grow dynamically as needed. so 4k goroutines != 4gb of ram. They currently start at 8k and grow as needed but copies are incurred to keep it contiguous.

In lthread I preallocate fixed size 1mb stack but madvise after a coroutine yields if the stack shrunk. No copies are needed but it does consume the vm address space.

Re:Performance of green threads and why it matters by ominous_prime on Sunday May 08, 2016 @04:18PM (#11655565)
Go hasn't used segmented stacks in quite a while. Go now has 2k stacks and uses stack copying to grow.
Re:Performance of green threads and why it matters by bobinator606 on Monday May 09, 2016 @08:12AM (#11659155)
we built a green threads implementation for VAX/VMS in 1984 with pre-allocated stacks because performance. the key is to put guard pages in between the stacks to protect from stack overruns.
Re:Performance of green threads and why it matters by zzzcpan on Sunday May 08, 2016 @01:36PM (#11654857)
There is a very big difference between implicit flow control, that you have with green threads and explicit one, that you have with "reinvented green threads", as you call them. First one will force you to think about synchronization to access your memory, but the second one - won't. It will also let you do clean cancellation, etc., that you won't be able to do with green thread.

Downvote all you want.

Re:Performance of green threads and why it matters by dang on Sunday May 08, 2016 @04:16PM (#11655548)
> Downvote all you want.

Please don't break the HN guidelines: https://news.ycombinator.com/newsguidelines.html.

We detached this subthread from https://news.ycombinator.com/item?id=11654767 and marked it off-topic.

Re:Performance of green threads and why it matters by jcoffland on Sunday May 08, 2016 @02:55PM (#11655210)
Go is an interesting language but the fanboys on HN tend to down vote any criticism. Same goes for the other golden child, Rust.
Re:Performance of green threads and why it matters by dang on Sunday May 08, 2016 @04:18PM (#11655558)
This breaks the site guidelines by calling names ('fanboys') and complaining about downvoting.

The problem with comments like this is that they degrade the discourse even if they're true.

https://news.ycombinator.com/newsguidelines.html