From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27041 invoked by alias); 5 Sep 2004 23:22:56 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 27015 invoked from network); 5 Sep 2004 23:22:54 -0000 Received: from unknown (HELO mailgw4.technion.ac.il) (132.68.238.37) by sourceware.org with SMTP; 5 Sep 2004 23:22:54 -0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mailgw4.technion.ac.il (Postfix) with ESMTP id 71472F798D for ; Mon, 6 Sep 2004 02:22:53 +0300 (IDT) (envelope-from staube@t2.technion.ac.il) Received: from mailgw4.technion.ac.il ([127.0.0.1]) by localhost (mailgw4.technion.ac.il [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 23550-01-8 for ; Mon, 6 Sep 2004 02:22:53 +0300 (IDT) Received: from techmail.technion.ac.il (techmail.technion.ac.il [132.68.0.66]) by mailgw4.technion.ac.il (Postfix) with ESMTP id B2276F7ADA for ; Mon, 6 Sep 2004 02:22:52 +0300 (IDT) (envelope-from staube@t2.technion.ac.il) Received: from localhost (localhost.localdomain [127.0.0.1]) by techmail.technion.ac.il (Postfix) with ESMTP id 8BD884C02D; Mon, 6 Sep 2004 02:22:52 +0300 (IDT) Received: from techmail.technion.ac.il ([127.0.0.1]) by localhost (techmail.technion.ac.il [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17646-09; Mon, 6 Sep 2004 02:22:52 +0300 (IDT) Received: by techmail.technion.ac.il (Postfix, from userid 48) id 52FC44C04E; Mon, 6 Sep 2004 02:22:52 +0300 (IDT) Received: from 132.69.225.106 ([132.69.225.106]) by webmail.technion.ac.il (IMP) with HTTP for ; Mon, 6 Sep 2004 02:22:52 +0300 Message-ID: <1094426572.413b9fcc3e6db@webmail.technion.ac.il> Date: Sun, 05 Sep 2004 23:22:00 -0000 From: staube@t2.technion.ac.il To: Dave Korn Cc: gcc@gcc.gnu.org Subject: RE: Idea of feature/optimization for C++ References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.4 X-Originating-IP: 132.69.225.106 X-Virus-Scanned: by amavisd-new at technion.ac.il X-Virus-Scanned: by amavisd-new at technion.ac.il X-SW-Source: 2004-09/txt/msg00223.txt.bz2 Dave, thank you very much for all your comments and also for the google link (however if you have an exact link to a page that talks about this topic i would appreciate even more because i found it difficult to find it in google). Anyway, since you said the optimization is already implemented i have been trying to test its performance in "gcc version 3.3.3 (mingw special)". Unfortunatelly the results are not good. I have made a very simple and short programm in only one short .cpp file. I compiled it in two versions, using templates and using virtual functions, the performance is pretty different. The classes are the same HappyHelloWorld and SadHelloWorld of my previous post, only that this time they dont print anything because i want to measure only the function calls overhead. And now instead of calling the functions two times it calls them 200000000 (two hundred billion times????). This is the code for the two versions. main.cpp (virtual function version) ******** #include #include using namespace std; class HelloWorld { protected: HelloWorld() { }; public: // no virtual virtual void doIt() = 0; }; class HappyHelloWorld : public HelloWorld{ public: HappyHelloWorld() { }; public: virtual void doIt() {}; }; class SadHelloWorld : public HelloWorld { public: SadHelloWorld() { }; public: virtual void doIt() {}; }; void letsSee(HelloWorld &hw) { hw.doIt(); } int main(int argc, char *argv[]) { SadHelloWorld shw; HappyHelloWorld hhw; DWORD previous = GetTickCount(); for(unsigned i=1; i<100000000;i++) { letsSee(hhw); letsSee(shw); } DWORD current = GetTickCount(); cout << (current-previous) << " milliseconds have passed\n"; return 0; } main.cpp (templates version) ******** #include #include using namespace std; class HelloWorld { protected: HelloWorld() { }; public: // no virtual // virtual void doIt() = 0; }; class HappyHelloWorld : public HelloWorld{ public: HappyHelloWorld() { }; public: /*virtual*/ void doIt() {}; }; class SadHelloWorld : public HelloWorld { public: SadHelloWorld() { }; public: /*virtual*/ void doIt() {}; }; template void letsSee(MyHelloWorld &hw) { hw.doIt(); } int main(int argc, char *argv[]) { SadHelloWorld shw; HappyHelloWorld hhw; DWORD previous = GetTickCount(); for(unsigned i=1; i<100000000;i++) { letsSee(hhw); letsSee(shw); } DWORD current = GetTickCount(); cout << (current-previous) << " milliseconds have passed\n"; return 0; } NOW THE RESULTS *************** I have run each version three times compiling normally and other three times compiling with -O3. All the results are in milliseconds. virtual normal - 4586, 4507, 4546 virtual OPTIMIZED - 1432, 1462, 1452 template normal - 1983, 2043, 2043 template OPTIMIZED - 992. 1022, 1002 My conclusion is that the optimization i am proposing is not implemented in "gcc version 3.3.3 (mingw special)". Maybe what you said is implemented is something which i saw documented in some C++ tutorials, and its optimizing to early binding if you call directly the virtual memeber function which is of course different of calling a function which calls the virtual member function. Am i right? Again, I wish i could get some more comments about the optimization i am proposing. Quoting Dave Korn : > > -----Original Message----- > > From: gcc-owner On Behalf Of staube > > Sent: 01 September 2004 10:30 > > > I am writing this mail in order to propose and new > > optimization/feature in the > > C++ front end of GCC. > > > 3) Who knows?, maybe the feature is already implemented, so, > > i am not adding > > anything :-( > > > > The issue i want to cover is virtual functions and > > late/early-binding. > > > As you see both examples do the same thing, only that the > > second one uses Early > > Binding and then is more efficient. > > > functions whenever possible Í was thinking that if the > > compiler would be > > inteligent enought it could do early binding in the first > > example too, because > > theres no need to do late-binding. > > http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=binding+compile-time+resol > ution+virtual+function+c%2B%2B > > It is indeed an excellent idea, and your presentation of the idea and > analysis of the benefits of this optimisation were spot on, but it has > already been done; at least to some extent. > > > Another optimization: inline and virtual!!! > > A method could be declared at the same time inline and > > virtual. then when a > > function call that method using earlybinding it will inline > > the function, but > > in the latebinding version it would call the method in the > > standard way. > > That too is done, in the cases where the compiler can deduce the binding > at compile time and where the function body has already been seen and was > defined directly in the class declaration or has the inline attribute > applied to it. > > cheers, > DaveK > -- > Can't think of a witty .sigline today.... >