I created a new module which is just a shorter alias for a module with a very long name:
module M = ModuleWithLongName
I'm in a situation where the size of the final executable matters. Is the above construct handled sanely by the compiler (i.e. M
is really just an alias), or does it replicate the entire contents of ModuleWithLongName
inside the module where M
is defined?
No, the OCaml language does not support "true module aliasing".
However, you probably won't notice until you try fairly advanced combination of functors and abstract types. In particular, you can only observe this issue in the type system, not in the runtime behavior of programs: modules are sometimes copied, but mutable states would be aliased between copies (in your example, if ModuleWithLongName.foo
is a mutable reference, then M.foo
is the same reference).
If you use first-class modules, or define local modules in deeply nested functions, you might observe module copy operations as a non-neglectible cost in the overall computation. The right mental model to reason about performance of first-class modules is that, after type-checking and module-checking, they're exactly records.
Since version 4.02, OCaml supports true module aliases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With