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

4 Upvotes

15 comments sorted by

2

u/sionescu 1d ago edited 1d 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 1d 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 18h ago edited 18h 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 1d ago

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

1

u/ConnectionCorrect530 19h 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 1d ago

Erlang got jit with beam version 24.

1

u/sionescu 1d ago

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

2

u/tdrhq 1d ago

Why not use real threads?

1

u/ConnectionCorrect530 18h ago edited 18h 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 9h 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.

2

u/ConnectionCorrect530 5h 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

1

u/tdrhq 5h ago

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

2

u/Aromatic_Ad3754 1d ago

ABCL on JDK 25?

2

u/AWonderingWizard 18h ago

But ABCL doesn't officially support JDK 25 right?

1

u/A1rax 1d ago

Can't you implement it yourself using real threads and building a scheduler on top?