r/Common_Lisp • • 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

20 comments sorted by

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.

1

u/kchanqvq 19d ago

What common lisp has cooperative thread?

I know it can be done in user space via CPS but the overhead is huge.

1

u/ConnectionCorrect530 18d ago edited 18d ago

cl-async seems to provide virtual thread using libuv under the hood and also there is a sbcl fibers implementation going on in sbcl itself

1

u/sionescu 19d ago

Yes, I'm referring to CPS like in cl-coroutine.

1

u/ConnectionCorrect530 18d ago

Thanks I didn't know Erlang did it under the hood by adding checkpoints. Since, my scripts wont be the full Common LISP but instead a constrained DSL I think I can add those checkpoints while processing the DSL itself.

Maybe I would go with that and cl-async

1

u/Winchester5555 19d ago

Erlang got jit with beam version 24.

1

u/sionescu 19d ago

Ok, strike that. The rest of what I said holds.

2

u/tdrhq 19d ago

Why not use real threads?

2

u/ConnectionCorrect530 18d ago edited 18d ago

I would have used them if it was on client but it would be on a server on which it would mostly be IO bound calling tool calls and waiting, so I think it might be a waste to use 1 OS thread for each script(would love to be proven wrong as it seems the most easy way)

1

u/tdrhq 18d ago

How many threads are we talking about? Just ask your agent to do a benchmark, my guess is that it really doesn't matter. Importantly,  you'll lose a lot of CL's powerful debugging abilities with virtual threads.

3

u/ConnectionCorrect530 18d ago

I actually did the benchmark it and at about 1000 threads sb-thread was using 2x the memory and 2x the latency as LFE but when I combined sb-thread with cl-async it was at par(slightly better than LFE). But even without cl-async 2x memory is not bad at all

2

u/svetlyak40wt 17d ago

It would be interesting to add to the benchmark an implementation on top of actors model, using Sento (https://mdbergmann.github.io/cl-gserver/)

1

u/ConnectionCorrect530 16d ago

I used Sento it performed well although a little worse than LFE and the hybrid sb-threads + cl-coroutine it was simpler to implement than the hybrid

1

u/svetlyak40wt 17d ago

How did you combine cl-async with sb-thread?

As far as I know, cl-async uses an event loop that works only in a single thread.

1

u/ConnectionCorrect530 16d ago

I used a threshold system for every 100 tasks in cl-async I spawned a new sb-thread

2

u/tdrhq 18d ago

Yeah just pay for 2x more CPU, it's worth it for the simplicity and debugging ability

2

u/Aromatic_Ad3754 19d ago

ABCL on JDK 25?

3

u/AWonderingWizard 18d ago

But ABCL doesn't officially support JDK 25 right?

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.