Herb Sutter: never make virtual functions public(!)

Started by
90 comments, last by Shannon Barber 19 years, 7 months ago
With a small tweak to Herb's idea, you can use it as an optimization tool sometimes. If you don't like my example classes, please ignore them, and mentally insert your own substitute.

Consider the following set of classes. A base class Array, and two subclasses, LinearArray, SegmentedArray. The LinearArray ensures that the array is always a single fixed block of RAM, usable by memcpy, fwrite, fread, and with quick access. The SegmentedArray tries to avoid realloc type calls by allocating memory in pages. The pages might not be sequential in memory, and thus is useless to memcpy, etc. There are times when each array type is appropriate.

Now, imagine you want a base class such that you can have code that doesn't really care how the array works, it just needs an array. The first instinct is to make a base class with 'operator[]' virtual... and it works fine. The trouble is, if you *know* you have a LinearArray, you're taking quite a bit of extra overhead to use the virtual function to fetch from the array instead of a simple inlined 'return m_pMem[nIndex]'.

So, instead, in the base class lets make the operator[] non-virtual, and call a virtual GetElement(idx).

The derived class implements GetElement(idx), and a newer non-virtual operator[] which doesn't use GetElement(). In fact, the GetElement of the derived class can probably use the operator[] of the derived class, so you don't duplicate code.

What does this gain us? If you use the base array class, it uses the necessary virtual method to access the array. If you know you're using the derived version you get quicker access a nicely inlined non-virtual call. Thus, operator[] uses the most optimal code path based on what it knows of the data type.

Now, VS.NET 7.1 Whole program Optimization might already do this, since at link time it can tell a method isn't derived further. If you're using another compiler (or don't think WPO does this) and you do a lot of unneccessary virtual calls, this could be a useful trick.
Advertisement
Quote:Original post by Dmytry
Quote:Original post by MadKeithV
Quote:Original post by dot
This is possibly a rather invalid argument nowadays as compilation time has gotten shorter and shorter with more powerful cpu.


Oh please...

A real-world project needs to manage its dependencies really carefully, or compilation times get out of hand. Xoreax Incredibuild for instance is successful because out there in real-code-land, it's not uncommon to have compile times that hurt.
The project I have in front of me has a full rebuild time of about an hour, and link times for small changes of sometimes nearly 10 minutes.


I'm bookmarking this thread to later show it to people that replies "i prefer when computer saves _my_ time, and anyway, computers are extremely fast now", when i'm starting to blame C++ with something like "C++ is one of the slowest-to-compile languages _ever_ because of strange sintax and _complete_ lack of modules (.h[ack] , .pch[ack] files it's not a modules)". Really. For instance, Delphi. Compiles and links at >7k lines in 0.2 sec. It also supports modules, so full rebuild is really rarely needed. "D" also compiles fast, and have templates and other things.


Every translation unit is its own "module", that's the purpose of the linking phase, so you don't have to recompile the world when you make a change to one source file. When you re-engineer the language to not have a linking phase, this "problem" of needed modules comes up (and then you need add a hidden linker to put them together!).

Try C++ Builder, it uses the same back-end as Delphi and has a very similar look & feel. It's not quite as fast as compiling object pascal, but it's *much* faster than the other compilers. It's optimization is decent too.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement