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

6 Upvotes

19 comments sorted by

4

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

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

1

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

Erlang got jit with beam version 24.

1

u/sionescu 7d ago

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

2

u/tdrhq 7d ago

Why not use real threads?

2

u/ConnectionCorrect530 7d ago edited 7d 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 7d 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 6d 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 6d 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 4d 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 6d 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 5d ago

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

2

u/tdrhq 6d ago

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

2

u/Aromatic_Ad3754 7d ago

ABCL on JDK 25?

3

u/AWonderingWizard 7d ago

But ABCL doesn't officially support JDK 25 right?

1

u/A1rax 7d ago

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