Take the 2-minute tour ×

Some functions are really easy to implement in OCaml (for example, map from a list) but you may use the map of the OCaml library: List.map

However, we can wonder which code will be more efficient. Calling a module of a separate compilation unit (a library) may void some possible optimizations. I read in the news group fa.caml that when calling functions from libraries, closures are used.

I have OCaml code in production that use Modules and Functors for doing generic programming. For historical reason my code is monolitic: all in one file. Now I have more time, I'm willing to separate the code into files for such modules. However, I'm afraid I can lost performance, as it took me a while to get it right. For example, I have modules for wrapping complex objects with numbers, so I enforce unique representation and fast comparison. I use those wrapped objects with generic Maps, Sets, and build caches upon them.

The questions are:

  • Am I going to loose performance if I move to separate files?
  • Is OCaml doing many optimizations on my code full of modules, functors, etc?

In C++, if you define class method in a .h, the compiler may end up inlining short methods, etc. Is it possible to achieve that in OCaml using separated files?

share|improve this question
I don't know much about the internals of the ocaml compiler and linker, so I cannot answer your question. However, I would be EXTREMELY surprised if you lost any performance from breaking your code up into modules. Even if you did lose a few milliseconds, the increased clarity of your code would be well worth it. –  A. Levy Nov 11 '09 at 19:29
In C/C++ that may cause a huge difference. I have millions of objects, accessed/compared zillions of times. A set of aditional references per access can be very very bad. –  hectorpal Nov 12 '09 at 6:56
Yes, but the compiler should inline those references so that you are actually not doing more lookups than necessary. How about you try to make a test project with a bunch of dummy modules and try to measure whether there is any difference between breaking them up and keeping them in the same file. It may be a lot less work than refactoring your working codebase. Hopefully this will give you some more confidence that ocamlopt will be able to intelligently optimize your code. –  A. Levy Nov 13 '09 at 19:42
add comment

1 Answer

You may lose some performance. However, there are two mitigating factors:

  • The OCaml native code compiler can do cross-module inlining, so it is possible for code to be inlined even across the separate compilation units (with a couple caveats - recursive functions and function arguments are not inlined across modules[1]).
  • The code will still quite possibly be fast enough, and the gains in readability and maintainability will quite possibly outweigh any (marginal) performance cost.

I do not know if OCaml defunctorizes code where the functors are defined in the same source file. If it does not, then modules shouldn't add any performance hit above that already incurred by the functors.

In general, it is my opinion that it is best to write straightforward, readable, maintainable code and not worry too much about microscopic performance characteristics like this unless the code proves to be too slow in practice.

share|improve this answer
Thanks Michael. Good info. Of course I know I will gain readability. What I have now is not satisfactory. However, in my case runtime performance is a critical issue. I have already invested a lot of time on switching from list to array (I'm using now the Res library for growing arrays). I also have tons of cache and carefully choosen datastructure. I think I went down from 10 minuts or worst to 10 seconds by doing what I did. My concern is now mainly about modules that wrap representation of millions of objects. I will split anyway, but I need to assest the impact and may not split all. –  hectorpal Nov 12 '09 at 6:52
once you're done refactoring code, it should be relatively easy to selectively move some modules back into the main file as needed. –  Martin DeMello Nov 18 '09 at 9:18
add comment

Your Answer


