Xref: utzoo comp.lang.c++:11917 comp.object:2626 comp.lang.objective-c:181 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!bionet!agate!shelby!neon!hoelzle From: hoelzle@Neon.Stanford.EDU (Urs Hoelzle) Newsgroups: comp.lang.c++,comp.object,comp.lang.objective-c Subject: Re: Static typing and OOP efficiency Message-ID: <1991Mar1.024233.19383@Neon.Stanford.EDU> Date: 1 Mar 91 02:42:33 GMT References: <1991Feb16.121825.15353@gpu.utcs.utoronto.ca> <27BFDF44.3EA6@tct.uucp> <27C523A2.2155@tct.uucp> Organization: Computer Science Department, Stanford University Lines: 40 pcg@cs.aber.ac.uk (Piercarlo Grandi) writes: >pcg> 3) Dynamic dispatchin can be made impressively fast, by the use of >pcg> various tricks (hashed tables, type deductive compilers, clever >pcg> linkers, hinting and caching). >chip> There's no way, however, that the dynamic dispatching of >chip> Objective-C can outperform even a virtual C++ member function, >chip> much less a normal member function, assuming that the C++ >chip> implementor was in her right mind when she wrote the code >chip> generator. Sure it can. The fact that x.foo *conceptually* is a dynamically- dispatched call does not mean that there has to be a dynamic dispatch *at run-time*. The keywords here are type "analysis" and "inlining". If the compiler knows the type of x (e.g. because the previous statement was "x = new baz") it can statically bind the call, and if bar is small, it can inline it, thus eliminating the call overhead entirely (and opening new opportunities for global optimizations). For example, on a simple nested loop benchmark, the Self compiler generates code which performs more than two conceptual message sends *per machine cycle* because it is able to completely eliminate the dynamically-bound sends. (In Self, every method send is a "virtual" in C++ terminology, and there are no type declarations.) The techniques used to achieve this are beyond the scope of this posting, but I can give references if you need them. This proves that, at least in principle, your assertion is incorrect. Now, I'm not claiming that dynamically-dispatched calls are always faster in untyped languages, or even that they are faster on average. What I am saying is that dynamically-dispatched calls can be faster than C++ virtual function calls *as they are implemented today*. C++ could actually benefit from the same optimization techniques that the Self compiler uses. -- ------------------------------------------------------------------------------ Urs Hoelzle hoelzle@cs.stanford.EDU Center for Integrated Systems, CIS 42, Stanford University, Stanford, CA 94305