View on GitHub


Documenting everything about OCaml


Optimizing OCaml Performance

OCaml benefits from having a very lean runtime representation for a GC-based language. Other languages that use a tracing GC generally need to allocate a lot more memory to do the same operations. This often results in a serious speed boost, since modern architectures are dominated by caching. However, this in itself is not always enough:

  • Functional code writing patterns tend to allocate a lot of memory, which is expensive.
  • Functional data structures are tree-based and immutable, and in most cases mutable data structures are faster, but not fully compatible with functional paradigms.
  • OCaml’s default data format is good for certain cases such as symbolic computation, but the fact that floats, for example, are allocated on the heap, means that some programs will be slower than in other languages.

Due to these reasons, it’s good to know how to optimize OCaml for performance when you need it.


  • Flambda is OCaml’s built-in optimizer. By default, OCaml will compile without Flambda since it takes longer to do so. Compiling without Flambda is recommended when running code that isn’t performance-sensitive. To switch to a compiler with flambda, use opam’s opam switch X.YY+flambda command, where X.YY refers to a compiler version. Flambda will try to inline and then optimize code where possible.
  • perf is a Linux performance tool that works with OCaml.
  • Flame graph tools like speedscope give a great view of where your program spends its time. Very useful for optimization.
  • This is a great article on the details of profiling with different tools and the comparison between them.

Writing Efficient Code