Version française
Home     About     Download     Resources     Contact us    
Browse thread
Why + vs +. but "fake" parametric polymorphism for <
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Why + vs +. but "fake" parametric polymorphism for <
On 10/12/06, Carlos Pita <cpitadev@yahoo.com.ar> wrote:
> Umh, soon after writing my previous post I found out an interesting
> chapter in the tutorial giving an exact example of asm code generated by
> the compiler for comparison operator <:
>
> # let max a b =
>  if a > b then a else b;;
> val max : 'a -> 'a -> 'a = <fun>
>
> ===>
>        [...]
>        ; Call the C "greaterthan" function (in the OCaml library).
>        pushl   %ebx
>        pushl   %eax
>        movl    $greaterthan, %eax
>        call    caml_c_call
> .L102:
>        addl    $8, %esp
>        ; If the C "greaterthan" function returned 1, jump to .L100
>        [...]
>
> Pretty expensive for a simple int comparison I would say.

It is not an int comparison! max here would work on lists and probably
most other non-abstract types in expected ways.

Try:

let int_max a b : int = if a > b then a else b

ocamlopt -S <source> will help you see what it generates for yourself.

e.g.:

camlTest__int_max_57:
.L101:
        cmpl    %ebx, %eax
        jle     .L100
        ret
        .align  16
.L100:
        movl    %ebx, %eax
        ret
        .text
        .align  16

Jonathan