1. Preface
  2. Part I: Preliminaries
  3. 1. Preliminaries
    1. 1.1. How to Use This Book
    2. 1.2. Who This Book Is For
    3. 1.3. What Makes OCaml Fast
    4. 1.4. The Observability Stack
  4. 2. Philosophy and Methodology
    1. 2.1. The USE Method for OCaml
    2. 2.2. The Performance Triage Process
    3. 2.3. Setting Up Reproducible Benchmarks
    4. 2.4. The Performance Checklist
  5. 3. Build Configuration for Performance
    1. 3.1. Compiler Optimization Levels
    2. 3.2. Flambda: The Optimizing Backend
    3. 3.3. Native Code vs Bytecode
  6. Part II: Measurement, Profiling, and Observation
  7. 4. Profiling and Observability Tools Overview
    1. 4.1. The Observability Landscape
    2. 4.2. Tool Categories for OCaml
    3. 4.3. Choosing the Right Tool
    4. 4.4. Profiling Ecosystem
  8. 5. CPU Profiling
    1. 5.1. Generating Flamegraphs
      1. 5.1.1. Flamegraphs on Linux
      2. 5.1.2. Flamegraphs on macOS
    2. 5.2. Using Instruments on macOS
    3. 5.3. Time-based Profiling with Landmarks
  9. 6. Dynamic Tracing and USDT Probes
    1. 6.1. OCaml Runtime USDT Probes
    2. 6.2. Application-Level USDT
    3. 6.3. Tracing Workflows
    4. 6.4. Building Custom Analysis Tools
  10. 7. Memory Profiling
    1. 7.1. Statistical Memory Profiling with statmemprof
      1. 7.1.1. statmemprof in OCaml 5.x
    2. 7.2. Using memtrace and memtrace-viewer
    3. 7.3. Other Memory Tools
    4. 7.4. GC Statistics and Tuning
    5. 7.5. Combining Memory Profiling with USDT
  11. 8. Debugging for Performance
    1. 8.1. GDB on Linux Debugging Session
    2. 8.2. LLDB on macOS Debugging Session
    3. 8.3. Bytecode Debugging
      1. 8.3.1. Debugging OCaml with Emacs DAP
    4. 8.4. Inspecting Compiler Output
  12. 9. Benchmarking
    1. 9.1. Macro-benchmarking Strategies
    2. 9.2. Continuous Benchmarking in CI
  13. Part III: Optimizations
  14. 10. Compiler-Level Optimizations
    1. 10.1. Inlining
    2. 10.2. Unboxing and Specialization
    3. 10.3. Tail Call Optimization
  15. 11. Data Structure Choices
    1. 11.1. Hash Tables and Maps
    2. 11.2. Strings and Buffers
    3. 11.3. Records and Tuples
    4. 11.4. Custom Data Structures
  16. 12. Memory and Allocation
    1. 12.1. Boxing and Unboxing
    2. 12.2. GC-Friendly Programming
    3. 12.3. Memory Layout Optimization
  17. 13. Function Optimization
    1. 13.1. Partial Application
    2. 13.2. Higher-Order Functions
    3. 13.3. Exception Handling
  18. 14. I/O Performance
    1. 14.1. Network I/O
    2. 14.2. Serialization
  19. Part IV: Parallelism and Concurrency (OCaml 5)
  20. 15. Understanding OCaml 5's Parallelism Model
    1. 15.1. Domains: True Parallelism
    2. 15.2. The Multicore GC
    3. 15.3. Effects for Concurrency
    4. 15.4. Observing Multicore Behavior
  21. 16. Parallel Programming with Domainslib
    1. 16.1. Async/Await Patterns
    2. 16.2. Channels and Communication
    3. 16.3. Common Pitfalls
  22. 17. Concurrent I/O with Eio
    1. 17.1. Fiber Scheduling
    2. 17.2. Combining Eio and Domainslib
    3. 17.3. Performance Tuning
  23. 18. Memory and Synchronization
    1. 18.1. Lock-Free Data Structures
    2. 18.2. Avoiding Contention
    3. 18.3. Cache Coherency Costs
  24. 19. Migrating from OCaml 4 to 5
    1. 19.1. Adapting Multi-process Code
    2. 19.2. Common Migration Issues
  25. Part V: Interoperability
  26. 20. C Bindings Performance
    1. 20.1. The Foreign Function Interface
    2. 20.2. Minimizing Crossing Overhead
    3. 20.3. Callback Costs
    4. 20.4. Memory Management Across Boundaries
    5. 20.5. ctypes vs Hand-Written Stubs
  27. 21. JavaScript Compilation
    1. 21.1. Optimization Flags
    2. 21.2. Dead Code Elimination
  28. Part VI: Compile Times
  29. 22. Reducing Compilation Time
    1. 22.1. Understanding What's Slow
    2. 22.2. Module Structure for Fast Builds
    3. 22.3. Flambda Compile Time Trade-offs
    4. 22.4. Parallel Compilation
    5. 22.5. Incremental Builds and Caching
  30. Part VII: Case Studies
  31. 23. Real-World Optimization Examples
    1. 23.1. Case Studies
  32. 24. Common Performance Patterns
    1. 24.1. Continuation-Passing for Tail Calls
    2. 24.2. Memoization Strategies
    3. 24.3. Lazy Evaluation Trade-offs
    4. 24.4. Data-Oriented Design in OCaml
  33. Appendices
  34. 25. Quick Reference: Compiler Flags
  35. 26. Quick Reference: Dune Configuration
  36. 27. Tool Installation Guide
  37. 28. Glossary
  38. 29. Further Reading

OCaml Debugging and Performance

Records and Tuples

Memory Layout Differences

Unboxed Fields

Flat vs Boxed Representations