]>
> But it seems that ocaml implements functors with dispatch tables, so
> it runs a bit slowly. Is there some reason why functors cannot be
> treated as normal code, much as C++ treats templates, and functions
> from functors treated as candidates for inlining?
To be exact, functions defined by functors are candidates for direct
call and inlining; but it is true that functions taken from the
functor parameter are always called via their closures (what you call
"dispatch tables"). E.g. consider the following example:
module F(X : sig val f : int -> int end) =
struct
let g x = 1 + X.f (x - 1)
end
module A = F(struct let f x = x * 2 end)
... A.g 10 ...
The function g can be inlined at point of call in (A.g 10), but the
call to function f (i.e. fun x -> x * 2) will not.
The only reasons for this behavior is that OCaml's flow analysis and
inline expander are fairly naive and concentrate on the most obvious
opportunities for direct calls and inline expansion.
In principle, the module language is terminating, so the compiler
could simply generate a copy of the functor body at each functor
application point, and work from here. This would allow more direct
calls and inline expansion -- as much as if you'd written your code
without functors -- but results in loss of separate compilation
(the functor body is recompiled over and over again) and code bloat.
C++ templates have precisely those two problems.
So, I don't think we want to go all the way down to the C++ solution.
But it is true that more opportunities for inlining (as in the example
above) could certainly be exploited.
- Xavier Leroy