newsfilter.io
Conference Presentation, Lecture

Making OCaml Safe for Performance Engineering

  • OCaml 5's parallel runtime is expected to be a high-quality implementation leveraging the development delay to refine the architecture.
  • The language currently faces performance limitations due to uniform memory representation causing poor cache locality for non-immediate types like int64 and increased garbage collector workload from heap-managed integers.
  • New features aim to provide safe control over performance-critical aspects, specifically targeting the avoidance of nanosecond-level requirements for the majority of users while offering unboxed types (e.g., int32, float64) and narrow flat data layouts to improve cache locality.
  • The compiler will implement polymorphism by generating one copy per layout rather than per type, and the garbage collector will be modified to distinguish unboxed values from heap pointers to reduce scanning overhead.
  • Stack allocation is predicted to outperform minor heap allocation by avoiding GC scanning and reusing memory, provided the type system enforces a stack discipline where values are not stored on the heap.
  • To enforce stack discipline without manual annotations, the type system will utilize "local" and "global" modes, where "local" mode allows bypassing the garbage collector for closures and ensures standard library functions like with_file prevent premature handle closure.
  • Data race freedom will be enforced via type system modes tracking "contention," "portability," "uniqueness," and "linearity" to ensure shared mutable data is accessed only under locks and that values are not illegally shared across threads.
  • The shared memory API will employ phantom types to link pointers to specific locks, preventing keys from being sent to other threads and requiring portable functions for map operations.
  • C++ compilers are predicted to generate multiple copies of generic functions leading to larger binaries, while C# and similar languages may pause execution for JIT compilation, contrasting with OCaml's planned approach.
  • Unlike C++ data races which exhibit "halt and catch fire" semantics and affect unrelated variables, OCaml data races are predicted to be bounded in space and time to simplify debugging, though such races remain "almost always bugs."
  • Jane Street developers expect to write parallel applications with static guarantees of data race freedom very soon, with the implementation finished and currently in the initial beta-testing stages.
  • New extensions are expected to be backwards compatible with upstream OCaml, allowing continued use of public libraries, though the speaker anticipates significant effort is required to annotate standard libraries with portability and contention modes.
  • Existing unboxed types and stack allocation are currently in production use at Jane Street, where they are expected to reduce reliance on external function calls and workarounds for high-performance developers.
  • Reliance on compiler optimizations is considered unpredictable due to heuristic dependence, whereas the new type system modes aim to provide static guarantees for behavior previously difficult to verify.