caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Quôc Peyrot" <chojin@lrde.epita.fr>
To: caml-list@inria.fr
Subject: Function inlining and functor
Date: Mon, 25 Jun 2007 16:39:02 +0200	[thread overview]
Message-ID: <B7781FA7-8E67-48C0-8F3B-09AB58EF3E4E@lrde.epita.fr> (raw)

Hello,

I have a program with a generic function which takes a function as
a parameter and calls it heavily. Something along the lines of:

let toto f =
   (* call f a couple of million times *)

I was trying to see wether or not I could force the inlining of "f"
when f is small function.

For the sake of simplicity, let's imagine we have:

let toto f =
   let a = ref 0 in
   for i = 0 to 10 do
     a := !a + f i
   done;
   !a

let f a = a * a

let _ =
   print_endline (string_of_int (toto f))

of course we can see that f is not inlined in the inner loop:
(PPC)
L106:
    lwz   r4, 0(r1)
    lwz   r17, 0(r4)
    mtctr r17 -> prepare the call
L108: bctrl -> call it


I tried to use a functor, hoping that it would help the compiler to
inline the function:

module type A =
sig
val f: int -> int
end

module Make (F:A) = struct

let toto () =
   let a = ref 0 in
   for i = 0 to 10 do
     a := !a + F.f i
   done;
   !a

end

let f x = x * x

module Mod = Make (struct let f = f end)

let _ =
   print_endline (string_of_int (Mod.toto ()))

but it doesn't seem to help at all, I can still see the call in my
inner loop:
L109:
    lwz   r5, 0(r1)
    lwz   r19, 8(r5)
    lwz   r4, 0(r19)
    lwz   r17, 0(r4)
    mtctr r17
L114: bctrl

I was in fact hoping to get the same results than in C++ using
meta-programming/template:
#include <iostream>

using namespace std;

template<class F>
class Mod
{
   public:
   int toto()
   {
     int res = 0;
     for (int i = 0; i <= 10; ++i)
       res += F::f(i);
     return res;
   }
};

class Foo
{
   public:
   static int f(int i) { return i * i; }
};

int main(int argc, char**argv)
{
   Mod<Foo> mod;
   cout << mod.toto() << endl;
   return 0;
}

which gives this nice inlining:
L15:
    mullw r0,r2,r2
    addi r2,r2,1
    add r4,r4,r0
    bdnz L15
    addis r2,r31,ha16(L__ZSt4cout$non_lazy_ptr-"L00000000002$pb")
    lwz r3,lo16(L__ZSt4cout$non_lazy_ptr-"L00000000002$pb")(r2)


Am I out of luck to get similar performance than C++?

Thanks,

-- 
Best Regards,
Quôc



             reply	other threads:[~2007-06-25 14:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-25 14:39 Quôc Peyrot [this message]
2007-06-25 15:22 ` [Caml-list] " Jon Harrop
2007-06-25 15:36   ` Joel Reymont
2007-06-25 15:41     ` Basile STARYNKEVITCH
2007-06-25 15:50       ` Quôc Peyrot
2007-06-25 15:58         ` Jon Harrop
2007-06-25 17:04           ` Quôc Peyrot
2007-06-26  3:22             ` Jon Harrop
2007-06-25 16:07     ` Jon Harrop
2007-06-25 20:11       ` Joel Reymont
2007-06-26  3:22         ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B7781FA7-8E67-48C0-8F3B-09AB58EF3E4E@lrde.epita.fr \
    --to=chojin@lrde.epita.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).