HeadlinesBriefing favicon HeadlinesBriefing.com

SBCL Fibers Revolutionize Lightweight Threading with 256KB Stacks and GC-Safe Cooperative Scheduling

Hacker News •
×

SBCL Fibers introduce lightweight cooperative threads for Common Lisp, reducing per-fiber stack size from 8MB to 256KB while maintaining garbage collection safety. Developed for high-concurrency scenarios like web servers, the project addresses OS thread overhead by using user-space scheduling with minimal kernel transitions. Fibers preserve sequential code structure while enabling efficient I/O handling through cooperative yielding, avoiding complex event-loop state machines.

The implementation prioritizes GC integration, ensuring the SBCL garbage collector can accurately track live objects across fiber stacks and binding stacks. Designers emphasize transparent compatibility - existing SBCL code runs unmodified within fibers, with blocking primitives automatically yielding to the scheduler. Context switches save/restore only six registers in user space, drastically reducing overhead compared to kernel thread switching.

Hunchentoot web server integration demonstrates practical application, handling 10,000+ connections with reduced memory footprint. Technical specifics include stack pooling using madvise(MADV_DONTNEED), thread-local storage management via TLS overlay arrays, and platform support for x86-64, ARM64, and RISC-V architectures. The :sb-fiber feature flag enables experimental use while maintaining backward compatibility.

Performance benchmarks show 40% lower memory usage versus traditional threading models, with sub-microsecond context switch times. The scheduler employs work-stealing algorithms and deadline-based fiber migration for load balancing. This approach balances developer productivity with system resource efficiency, offering a middle ground between OS threads and event-driven programming.