From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public From: brianh@bit3.com Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/01 Message-ID: #1/1 X-Deja-AN: 277114316 References: <3431A2DB.4273@EasyInternet.net> <60t8fo$of7$1@lyra.csx.cam.ac.uk> <34326556.2216DC70@virginia.edu> Organization: Bit 3 Computer Corp Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec In article <34326556.2216DC70@virginia.edu>, Michael D. Nahas wrote: >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 problem is not the double indirection- which can be handled if the CPU prefetchs data as well as instructions- but the fact that you are doing two memory accesses now instead of one, and usually widely seperate memory accesses (most implementations don't AFAIK actually keep the virtual function tables with the class data, but in a seperate structure shared by all objects of the same type- which is usually somewhere far away from where the data ends up). This is a symptom of the fact that memory has not increased in speed the same way CPUs have (I put 70ns memory into my 386- granted, no FPM or EDO, but still... And the 386 still needed cache). The only thing more painfull on performance than leaving cache and waiting on main memory is leaving main memory and swapping pages into disk. Compilers (and programmers!) should worry more about locality of reference. If you are going to blow cache, the question arises "is it worth it?". For sparse matricies, the answer is obviously yes (the alternative is to swap like mad)- although the matrix solving programs should be written to be cache aware, and keep locality of reference up. For virtual functions, I think the benefits outweigh the gains, although it depends critically on what you're doing. I note the Fortran is still one of the two best languages for doing numeric computation in- only recently have there been C compilers as good as Fortran compilers (and even in these cases you often find they share an optimizer with a Fortran compiler- examples include SGI's MipsPro compilers, and HP's compilers). > >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 * >\********************************************************/ > I don't speak for Bit 3. Brian -- "NT and security should never be used in the same breath." - Winn Schwartau, EETimes #972, 22 September 1997 pp.96 -Brian