r/Common_Lisp • u/ConnectionCorrect530 • 19d ago
SBCL Looking for a virtual thread library with preemptive scheduling
I want to create a AI automation tool which would need to run AI generated scripts in parallel, so I want library/framework which would support virtual threads with preemptive scheduling and isolation.
Till now LFE(Lisp Flavoured Erlang) seems like the best option for my needs but I am too accustomed to using sly and common lisp syntax
So, any suggestions are welcome
5
Upvotes
4
u/sionescu 19d ago edited 19d ago
If you want virtual threads, the best you can get is cooperative threads in Common Lisp. Erlang achieves partial preemptiveness by introducing checkpoints everywhere and having an interpreter with no JIT, which slows down the code a lot. It also doesn't have full isolation because of the imperfect preemption: a call into a foreign library, a syscall or a hot loop can block a worker thread.
2
u/tdrhq 19d ago
Why not use real threads?
2
u/Aromatic_Ad3754 19d ago
ABCL on JDK 25?
1
u/A1rax 19d ago
Can't you implement it yourself using real threads and building a scheduler on top?
1
u/DrPepper836 5d ago edited 5d ago
First, virtual threads are only better than a traditional worker scheduler in very specific workloads. Specifically, they're better if you have lots of decomposable, non-IO bound tasks. In those settings, where you might need to fork hundreds of thousands of times and then recombine the results, they can be more ergonomic to write performant code. A good example is parallelizing quicksort. Running AI generated scripts in parallel does not sound like a problem that would benefit from virtual threads over using a traditional worker scheduler. The tasks just don't decompose that much, and even the agents spawn the occasional subagent, they still won't be forking nearly as often as something like quicksort can.[Edit: I was thinking about this, and this may not be so true. If you have truly blocking IO, then green threads won't help, because they'll have to go in a blocking pool anyway. I was assuming that was the case. But there's probably a way to hook external process calls or LLM inference into an event loop, so green threads probably would help with that part. But regardless, I think what I said below still applies.]
Second, this is an IO bound task. Forking system threads is not *that* slow. Forking a system thread takes microseconds, almost always less than a millisecond. It's a common trap to worry about micro-optimizations in tasks that are going to be IO bound anyway. I guarantee that even if you used system threads and mutexes all the way down, your runtime will be dominated by the costs of (1) running the LLM inference and (2) running the generated scripts.
All that is to say, you should check out lparallel (https://github.com/lmj/lparallel) and start there. I'm 99% sure it'll be fast enough to meet your needs.