News for hackers. Stuff that matters.
Re:Co-routines as an alternative to state machines by silentbicycle on Sunday August 30, 2009 @08:32AM (#794409)
As a data point, creating 100,000 coroutines in Lua uses about 183 MB* , which works out to about 1.9K apiece. This seems independent of the function included in the coroutine -- "return 3" and a function with several pages of code use the same space. It's probably a small stack, a closure for "upvaues", and a function pointer.

Note that this doesn't include memory for the IO connection, but the original question was about the space overhead of coroutines themselves. How much do Perl and Haskell use without the IO handle?

* On an AMD64 running OpenBSD 4.6. On an i386 (32-bit) system, it took 142 MB.

Re:Co-routines as an alternative to state machines by jrockway on Sunday August 30, 2009 @10:50AM (#794535)
With empty suspended coros, we use about 169M for 300,000 of them. So that's about .5K per coroutine.

In Haskell, 500,000 threads waiting for an empty mvar used about 1.4G of RAM. But if they just spin forever (foo = foo), not waiting for anything, then it uses 129M for 500,000 threads; .2K each.

So really, coroutines are not costly at all. Most of the price you pay you would have to pay anyway. The overhead of the coroutine itself is tiny.

Re:Co-routines as an alternative to state machines by silentbicycle on Sunday August 30, 2009 @11:41AM (#794588)
Good to know, thanks for the quick response.
Re:Co-routines as an alternative to state machines by jrockway on Monday August 31, 2009 @12:02AM (#795404)
And just for the record, my examples were on a 64 bit machine.