From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 47ACEBC69 for ; Mon, 25 Jun 2007 16:39:04 +0200 (CEST) Received: from smtp20.orange.fr (smtp20.orange.fr [80.12.242.27]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5PEd3Vg005316 for ; Mon, 25 Jun 2007 16:39:04 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2007.orange.fr (SMTP Server) with ESMTP id C0AA61C000A0 for ; Mon, 25 Jun 2007 16:39:03 +0200 (CEST) Received: from [192.168.1.162] (ALagny-153-1-15-87.w83-200.abo.wanadoo.fr [83.200.126.87]) by mwinf2007.orange.fr (SMTP Server) with ESMTP id 977521C0009F for ; Mon, 25 Jun 2007 16:39:03 +0200 (CEST) X-ME-UUID: 20070625143903620.977521C0009F@mwinf2007.orange.fr Mime-Version: 1.0 (Apple Message framework v752.2) Content-Transfer-Encoding: quoted-printable Message-Id: Content-Type: text/plain; charset=ISO-8859-1; format=flowed To: caml-list@inria.fr From: =?ISO-8859-1?Q?Qu=F4c_Peyrot?= Subject: Function inlining and functor Date: Mon, 25 Jun 2007 16:39:02 +0200 X-Mailer: Apple Mail (2.752.2) X-Miltered: at discorde with ID 467FD387.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; lrde:01 inlining:01 functor:01 inlining:01 endline:01 inlined:01 lwz:01 lwz:01 functor:01 compiler:01 sig:01 val:01 struct:01 struct:01 endline:01 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 =3D (* 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 =3D let a =3D ref 0 in for i =3D 0 to 10 do a :=3D !a + f i done; !a let f a =3D a * a let _ =3D 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 =3D sig val f: int -> int end module Make (F:A) =3D struct let toto () =3D let a =3D ref 0 in for i =3D 0 to 10 do a :=3D !a + F.f i done; !a end let f x =3D x * x module Mod =3D Make (struct let f =3D f end) let _ =3D 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 using namespace std; template class Mod { public: int toto() { int res =3D 0; for (int i =3D 0; i <=3D 10; ++i) res +=3D F::f(i); return res; } }; class Foo { public: static int f(int i) { return i * i; } }; int main(int argc, char**argv) { Mod 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, --=20 Best Regards, Qu=F4c