Lesson 9 of 9 · 28m
Performance, FFI, and When OCaml Fits Production
Measure allocation and latency honestly, cross the C boundary carefully, and decide where ML's correctness-performance balance beats mainstream alternatives.
The wrong way to sell OCaml is with a benchmark screenshot. The right way is with a production constraint: you need high assurance, native performance, fast iteration on symbolic logic, and a runtime your team can understand. If your constraint is maximum parallelism on mutable numeric arrays, or an ecosystem dominated by another platform's libraries, OCaml may be the wrong answer. Senior judgment is knowing the difference.
Allocation is usually the first performance lesson
OCaml makes immutable values pleasant, which means it also makes allocation easy. Allocation is not automatically bad; the minor heap and garbage collector are designed for short-lived values. But hot paths that generate endless temporary objects pay real costs.
let slow xs =
xs
|> List.map (fun x -> x + 1)
|> List.filter (fun x -> x mod 2 = 0)
|> List.map string_of_intEach stage allocates an intermediate list. For small inputs, clarity wins. In a hot path, combine passes, use loops where appropriate, or choose data structures designed for the workload.
Measure with realistic data
Use benchmarking tools rather than intuition. Core's core_bench is common in the OCaml ecosystem:
open Core
open Core_bench
let () =
Command.run (Bench.make_command [
Bench.Test.create ~name:"pipeline" (fun () -> ignore (slow sample));
])Measure allocation counts as well as wall time. Latency-sensitive services often care more about tail behavior and GC pauses than mean throughput.
Immutable does not mean slow, mutable does not mean wrong
OCaml supports mutable record fields, references, arrays, and bytes — for example, a ring buffer might expose only safe operations while internally using mutable head : int and buffer : bytes. Encapsulation matters here. The point of a module is that you can use mutation internally while exposing a safe, invariant-preserving interface externally. Functional style is a default, not a purity contest.
The FFI is powerful and dangerous
OCaml can call C directly or through libraries such as Ctypes. Every crossing introduces questions:
ownership? GC interaction? blocking behavior?
domain/thread state? exceptions? integer widths?A small binding error can corrupt memory outside the OCaml heap and produce crashes far from the cause. Keep FFI shims small, validate invariants aggressively, and prefer maintained binding libraries over handwritten stubs when possible.
Multicore changes the concurrency conversation
OCaml 5 added shared-memory parallelism through domains, plus effect handlers. This does not mean every program should now spawn domains. Parallelism is appropriate when the workload and memory-sharing costs justify it. Many services remain simpler with process isolation or a mature concurrency runtime. Evaluate contention, allocation locality, and observability before rewriting architecture around domains.
Where OCaml fits best
Strong fits include compilers, static analyzers, theorem-adjacent tooling, rule engines, financial systems, symbolic network control planes, protocol implementations, and developer infrastructure. Weaker fits include GUI-heavy desktop products, ecosystems requiring constant proprietary SDK integration, and teams unwilling to own a smaller dependency ecosystem.
Field notes
- Do not optimize before profiling; in OCaml the compiler already eliminates more than newcomers expect, but it cannot fix algorithmic or allocation design mistakes.
- Float arrays and unboxed representations have nuanced performance behavior; measure the exact representation you plan to use.
- C library versions are part of your software supply chain. FFI reproducibility includes system dependencies, not just opam packages.
- Tail-recursive list processing matters, but data-structure choice often matters more than syntactic recursion style.
Was this lesson helpful?