Skip to main content

Posts

Showing posts with the label OCaml

OCaml 5 Multicore Guide: Implementing Eio for High-Performance Concurrency

  For over a decade, OCaml engineers have lived in a paradox: writing type-safe, high-performance systems code while restricted to a single core. The ecosystem relied on   Lwt   and   Async —monadic concurrency libraries that simulated multitasking on a single OS thread. While effective for I/O-bound workloads, this approach introduced the "function coloring" problem (wrapping everything in promises) and hit a hard ceiling when CPU-bound tasks blocked the event loop. OCaml 5 removes the Global Runtime Lock. With the introduction of  Effect Handlers  and  Domains , we can now achieve direct-style concurrency (code looks synchronous but is asynchronous) and true parallelism. This guide demonstrates how to architect a high-performance network service using  Eio  (Effects-based I/O), OCaml's modern concurrency library, bridging the gap between lightweight fibers and heavyweight domains. The Architecture Shift: From Monads to Effects To migrate ef...

OCaml 5 Migration: Porting Lwt Promises to Eio Effects

  The shift from Lwt (cooperative threading via monads) to Eio (direct-style parallelism via Effect Handlers) represents the most significant paradigm shift in the OCaml ecosystem in a decade. While the promise of "no more monads" is alluring, the migration path is fraught with invisible dangers. The primary friction point is not syntax; it is the fundamental change in the execution model. In Lwt, context switches only occur at explicit bind points ( >>=  or  let* ). You implicitly relied on this behavior for atomicity. In OCaml 5 with Eio, code running on multiple domains introduces true parallelism. Consequently, logic that was thread-safe by accident in Lwt becomes a race condition in Eio. This post details the rigorous migration of a stateful, asynchronous module from Lwt to Eio, ensuring structured concurrency and thread safety. The Why: Monadic Cooperative vs. Direct Structured Concurrency To port correctly, you must understand the mechanical divergence. Lwt (...