From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public From: "Michael D. Nahas" Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/01 Message-ID: <34326556.2216DC70@virginia.edu>#1/1 X-Deja-AN: 277055955 References: <3431A2DB.4273@EasyInternet.net> <60t4eu$moc$1@lyra.csx.cam.ac.uk> <60t42r$4pd@scnews.sc.intel.com> <60t8fo$of7$1@lyra.csx.cam.ac.uk> Organization: University of Virginia Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec Ketil Z Malde wrote: > nmm1@cus.cam.ac.uk (Nick Maclaren) writes: > > > If you take a close look at C++, the X Windowing System, the various > > IBM and Microsoft windowing systems etc., you will see that an > > 'object' is implemented as a data structure and you invoke a method > > by picking an address up from that structure and branching to it. > > Right - and in Pascal, Algol, Cobol, and all those other superior > languages, function calls are made without branching? Which is why > Pascal and Cobol is commonly used when performance is important? Right? > > And since programs written in C runs at a ``miniscule fraction of their > potential CPU performance'', could you please inform me which Pascal, > Algol, Cobol or Modula-[23] compiler that create such vastly improved > speed of execution? And - how much faster can I expect applications to > run? I'm afraid that I must admit that Maclaren, even with his outrageous claim, is correct. Malde has missed the problem - it is not branching, but indirection, A.K.A. pointer chasing. For a standard function, you just branch to the address. With C++'s virtual functions you either follow a pointer or perform an indexed lookup to a jump table, which will load the address you will jump to, and then branch to that address. Why is indirection bad? When the destination address of a jump is known, the processor can begin fetching from the new address and continue executing. When the destination address of the jump is unknown, the processor usually stalls until the address comes in from memory. Some processors do have hardware to speed up indirect jumps, but the hardware was developed using C benchmarks, where 90% of indirect jumps are just returns from functions. The truely evil part of indirection is not in function calls, but in the non-array data structures of most programs. Think of traversing a linked list. To load the first node in the list, it takes as much time as the memory latency. To load the second node, you must first load the first node, and then pay the cost of the memory latency time again. So to traverse a N node linked list, it takes N times the memory latency. For most cases, the nodes are contiguous in memory and most programs run inside the L1 and L2 cache, so these latencies are not too bad for most programs. However, programs which do not fit in large L2 caches, such as sparse matrix programs and the X Windows system, pay heavily. The latency time of going to main memory is growing exponentially in terms of CPU cycles. So, is this a problem, and if so, how do we fix it. Yes, it is a problem, because any stall to the pipeline which prevents speculative execution is bad. How do we solve it? The first thing to do is to make programs which have this problem part of the SPEC Benchmarks. This will make it the focus of everyone's attention. The other thing that can be done is to look at Compiler optimizations - many of the indirect function calls can be eliminated, especially if all the source is compiled together. Separate compilation of object files requires general interfaces which prevent many optimizations. Also, a note on Java. All Java functions are virtual and all require a general interface on a class level. Lastly, all objects are referenced indirectly, so even an array of Java objects is two levels of indirection to get the object you want! So, Java is even worse than C++. Mike As always, comments and criticism welcome. -- /********************************************************\ * Michael David Nahas nahas@virginia.edu * * Graduate Student in CS University of Virginia * \********************************************************/