Hacker News Viewer

Show HN: C discrete event SIM w stackful coroutines runs 45x faster than SimPy

by ambonvik on 2/3/2026, 4:09:07 PM

Hi all,<p>I have built <i>Cimba</i>, a multithreaded discrete event simulation library in C.<p>Cimba uses POSIX pthread multithreading for parallel execution of multiple simulation trials, while coroutines provide concurrency inside each simulated trial universe. The simulated processes are based on asymmetric stackful coroutines with the context switching hand-coded in assembly.<p>The stackful coroutines make it natural to express agentic behavior by conceptually placing oneself &quot;inside&quot; that process and describing what it does. A process can run in an infinite loop or just act as a one-shot customer passing through the system, yielding and resuming execution from any level of its call stack, acting both as an active agent and a passive object as needed. This is inspired by my own experience programming in Simula67, many moons ago, where I found the coroutines more important than the deservedly famous object-orientation.<p>Cimba turned out to run <i>really</i> fast. In a simple benchmark, 100 trials of an M&#x2F;M&#x2F;1 queue run for one million time units each, it ran <i>45 times faster</i> than an equivalent model built in SimPy + Python multiprocessing. The running time was <i>reduced by 97.8 %</i> vs the SimPy model. Cimba even processed more simulated events per second <i>on a single CPU core</i> than SimPy could do on all 64 cores.<p>The speed is not only due to the efficient coroutines. Other parts are also designed for speed, such as a hash-heap event queue (binary heap plus Fibonacci hash map), fast random number generators and distributions, memory pools for frequently used object types, and so on.<p>The initial implementation supports the AMD64&#x2F;x86-64 architecture for Linux and Windows. I plan to target Apple Silicon next, then probably ARM.<p>I believe this may interest the HN community. I would appreciate your views on both the API and the code. Any thoughts on future target architectures to consider?<p>Docs: <a href="https:&#x2F;&#x2F;cimba.readthedocs.io&#x2F;en&#x2F;latest&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cimba.readthedocs.io&#x2F;en&#x2F;latest&#x2F;</a><p>Repo: <a href="https:&#x2F;&#x2F;github.com&#x2F;ambonvik&#x2F;cimba" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ambonvik&#x2F;cimba</a>

https://github.com/ambonvik/cimba

Comments

by: sovande

Didn’t read the code yet, but stuff like this tend to be brittle. Do you do something clever around stack overflow, or would that just mess up all coroutines using the same stack?

2/3/2026, 7:22:35 PM


by: jerf

While that speed increase is real, of course, you&#x27;re really just looking at the general speed delta between Python and C there. To be honest I&#x27;m a bit surprised you didn&#x27;t get another factor of 2 or 3.<p>&quot;Cimba even processed more simulated events per second on a single CPU core than SimPy could do on all 64 cores&quot;<p>One of the reasons I don&#x27;t care in the slightest about Python &quot;fixing&quot; the GIL. When your language is already running at a speed where a compiled language can be quite reasonably expected to outdo your performance on 32 or 64 cores on a single core, who really cares if removing the GIL lets me get twice the speed of an unthreaded program in Python by running on 8 cores? If speed was important you shouldn&#x27;t have been using pure Python.<p>(And let me underline that <i>pure</i> in &quot;pure Python&quot;. There are many ways to be in the Python ecosystem but not be running Python. Those all have their own complicated cost&#x2F;benefit tradeoffs on speed ranging all over the board. I&#x27;m talking about pure Python here.)

2/3/2026, 6:27:15 PM


by: anematode

Looks really cool and I&#x27;m going to take a closer look tonight!<p>How do you do the context switching between coroutines? getcontext&#x2F;setcontext, or something more architecture specific? I&#x27;m currently working on some stackful coroutine stuff and the swapcontext calls actually take a fair amount of time, so I&#x27;m planning on writing a custom one that doesn&#x27;t preserve unused bits (signal mask and FPU state). So I&#x27;m curious about your findings there

2/3/2026, 6:44:29 PM


by: quibono

I don&#x27;t know enough about event simulation to talk API design in depth but I find the stackful coroutine approach super interesting so I&#x27;ll be taking a look at the code later!<p>Do you plan on accepting contributions or do you see the repo as being a read-only source?

2/3/2026, 5:39:41 PM


by: qotgalaxy

[dead]

2/3/2026, 5:40:47 PM