Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!psuvax1!wuarchive!cs.utexas.edu!uunet!taumet!mike From: mike@taumet.com (Michael S. Ball) Newsgroups: comp.lang.c++ Subject: Re: >>> Virtual functions and inline ... Message-ID: <618@taumet.com> Date: 2 Mar 91 17:42:50 GMT References: Reply-To: mike@taumet.UUCP (Michael S. Ball) Organization: Taumetric Corporation, San Diego Lines: 59 In article ssimmons@convex.com (Steve Simmons) writes: >>From: gas@cs.nott.ac.uk (Alan Shepherd) >> Organization: Nottingham University >> Date: 28 Feb 91 14:15:48 GMT >> Subject: Virtual functions and inline > >> Is it okay to declare virtual functions inline ? Even though the code >> to be excuted is substituted rather than a function call made, does >> the correct method still get called ? > > This is not a wise thing to do although the language will > allow it. Depending on your situation, it may be a very wise thing to do. If The compiler can compute the actual type of an expression at compile time it can do the inline expansion. One of the advantages of this is the additional optimization which can be done because the exact characteristics of the arguments are known. Particularly when the use is in a loop this can result in a very large performance increase. > In the first place, you are never guaranteed that a routine > is inlined. It is only a suggestion to the compiler. If So what? If it is inline you have the chance to win. If it isn't, you have lost very little. > the routine is either too complicated or a virtual function, > then a static copy of the routine will be placed in the > compiled module. Thus, a routine that was coded as inline > may cause a performance degradation because it may consume > much more space when it is not physically inlined. Possible, but unlikely in most situations. The compiler should warn you if it can't expand the function inline at all, and the other cases, like taking a pointer to the function, are pretty rare. > With a virtual function, a copy of that routine will be > created for every module that has at least one instantiation > of that class. False. A copy of that routine will be created whenever the compiler has to create a virtual table. This will usually be exactly once in the entire program. > Inlining should be done as part of the optimizing phase. True, but irrelevent. Given that most compilers don't do this, the user's question is how to make use of the facility which is provided. The answer is to inline the function when: It is very short (shorter than the call, for example), or It is a bottleneck (better measure this) Questions of virtualness are relatively unimportant. -- Michael S. Ball mike@taumet.com TauMetric Corporation (619)697-7607