I've been wondering whether the ocaml compiler does any sort of parameter passing optimizations for data structures, e.g. stack allocating, or destructuring them when it can determine their scope does not escape the call. My first conclusion is that it does for tuples only, but I wanted to see what others might know.

I wrote a number of simple test programs that each pass 6 arguments through 2 levels of functions by a number of means (main calls foo 1M times, foo calls bar once). For 1M iterations, the times I saw (optimized) are posted below.

It looks like the case of passing parameters in a tuple doesn't allocate (since the number of minor words is the same as the positional parameters case). However, it still seems to run more than an order of magnitude slower so perhaps it loses the benefits of register allocation (?).

Warren


let bar a b c d e f =
  a + b + c + d + e + f

minor_words: 7950
promoted_words: 2251
major_words: 2266
minor_collections: 1
major_collections: 0
heap_words: 61440
heap_chunks: 1
top_heap_words: 61440
live_words: 2266
live_blocks: 354
free_words: 59174
free_blocks: 1
largest_free: 59174
fragments: 0
compactions: 0

real    0m0.860s
user    0m0.835s
sys     0m0.017s


let bar (a, b, c, d, e, f) =
  a + b + c + d + e + f

minor_words: 7950
promoted_words: 2251
major_words: 2266
minor_collections: 1
major_collections: 0
heap_words: 61440
heap_chunks: 1
top_heap_words: 61440
live_words: 2266
live_blocks: 354
free_words: 59174
free_blocks: 1
largest_free: 59174
fragments: 0
compactions: 0

real    0m11.067s
user    0m10.942s
sys     0m0.037s


let bar (Foo(a, b, c, d, e, f)) =
  a + b + c + d + e + f

minor_words: 7000224061
promoted_words: 2251
major_words: 2266
minor_collections: 213629
major_collections: 0
heap_words: 61440
heap_chunks: 1
top_heap_words: 61440
live_words: 2266
live_blocks: 354
free_words: 59174
free_blocks: 1
largest_free: 59174
fragments: 0
compactions: 0

real    0m17.443s
user    0m17.176s
sys     0m0.089s


let bar {a=a; b=b; c=c; d=d; e=e; f=f} =
  a + b + c + d + e + f

minor_words: 7000223886
promoted_words: 2251
major_words: 2266
minor_collections: 213629
major_collections: 0
heap_words: 61440
heap_chunks: 1
top_heap_words: 61440
live_words: 2266
live_blocks: 354
free_words: 59174
free_blocks: 1
largest_free: 59174
fragments: 0
compactions: 0

real    0m16.314s
user    0m16.167s
sys     0m0.059s


let bar ?(a=0) ?(b=0) ?(c=0) ?(d=0) ?(e=0) ?(f=0) () =
  a + b + c + d + e + f

minor_words: 12002946309
promoted_words: 2251
major_words: 2266
minor_collections: 366300
major_collections: 0
heap_words: 61440
heap_chunks: 1
top_heap_words: 61440
live_words: 2266
live_blocks: 354
free_words: 59174
free_blocks: 1
largest_free: 59174
fragments: 0
compactions: 0

real    0m33.036s
user    0m32.648s
sys     0m0.156s

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to