The mangled names are usually only slightly longer than the original ones. However, when you start using templates, the symbols begin getting very large, and that can become a serious problem. In fact, one of the worst problems in the once long-awaited Microsoft VC++ 4 was that it was unable to compile STL-based code because of 256-character limit on the symbol size. But it can get much worse than that. Listing 1 shows a seemingly innocent piece of code that uses a map from a string to a vector of strings. When compiled with g++, the size of the largest symbol produced by the compiler is 1,997 bytes! This is actually quite modest compared to other compilers. For example, if compiled with HP aCC compiler, the largest symbol produced is 4,246 bytes. To make things even worse, aCC has 4,000-byte limit on symbol size, so normally it is not able to compile the code at all.
One solution to the problem is to just use shorter names in your code. For instance, instead of template <class T> class my_proj::AVeryLongDescriptiveName, you could use just my_proj::AShortName and then define a typedef to be able to use the long variant of the name in your code. Unfortunately, this solution requires template typedefs, which are not currently a part of the C++ Standard (if planned for inclusion in the future).
However, there is an easy way to solve the problem -- template parameter substitution.
Note that you do not have to actually hold std::strings in the map, but instead something compatible with them. Therefore, you can write a simple non-template class gtring that inherits std::string and use it as the key to the map. Similarly, you can inherit gstringvector from std::vector<std::string>.
In inheriting gtring from std::string, you lose none of the latter's functionality, except possibly for some of the constructors. Therefore, the substitution is transparent to the users of the map.
Listing 2 shows the full implementation of this technique. In the first case (#ifdef SMART), only the first parameter is substituted. In the second (#ifdef SMARTER), we change both parameters.
Table 1 shows the size of the largest symbol in the resulting program. As you can see, the size is reduced by more than a factor of five.
You may find this technique useful even when the compiler does not choke on your code. Table 2 shows the size of the executable file when compiled with the different options. You can see that the file size is reduced by a factor of two, saving as much as 800 KB on a single template instance.
Shachar Itzhaki works as a full-time software engineer. His expertise includes C++ and Python programming and software design. He is currently studying towards an M.Sc. degree in Computer Science at the Open University.