Class compilation performance

Consider the following two code generators:

(echo "class foo = object" ; for ((i = 0; i < 2000; i++)) do echo method x$i = $i; done ; echo end) > test.ml

      

and

(echo "" ; for ((i = 0; i < 2000; i++)) do echo let x$i = $i; done ; echo) > test.ml

      

The former creates one large class with a scalable number of methods, while the latter does the same with a module (toplevel).

Although, the compiler behaves quite differently:

% time ocamlc test.ml
ocamlc test.ml  19,89s user 0,01s system 99% cpu 19,950 total

% time ocamlc test.ml 
ocamlc test.ml  0,29s user 0,01s system 99% cpu 0,305 total

      

If you play with the loop limit, you will notice that the module can be compiled in linear time, but the compilation of the class is quadratic. I wonder what is causing this problem and is there a way to avoid it?

Is it really because the OCaml class implementation has a quadratic compilation process? If so, is there a semantic equivalent way to separate work and speed up work (i.e. inheritance)? I have to generate large classes as I rely on late binding of class methods.

+3


source to share





All Articles