Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "function size profiler" out there?

After three years working on a C++ project, the executable has grown to 4 MB. I'd like to see where all this space is going. Is there a tool that could report what the biggest space hogs are? It would be nice to see the size by class (all functions in a class), by template (all instantiations), and by library (how much belongs to the C standard library and STL? how much for each library in the exe?)

Edit: Note, I am using Visual C++ on Windows.

like image 628
Qwertie Avatar asked Jun 26 '09 22:06

Qwertie


3 Answers

In Linux, you can use nm to show all symbols in the executable and to sort them in reverse order by size:

$ nm -CSr --size-sort <exe>

Options:

  • -C demangles C++ names.
  • -S shows size of symbols.
  • --size-sort sorts symbols by size.
  • -r reverses the sort.

If you want to get the results per namespace or per class, you can just grep the output for 'namespace::', 'namespace::class_name::', etc..

If you only want to see symbols that are defined in the executable (not ones defined elsewhere, like in libraries) then add --defined-only. Sorting by size should take care of this, though, since undefined symbols aren't going to have a size.

For Windows, you should still be able to use nm on your binary files, since nm supports COFF binaries. You can install nm via cygwin, or you could copy your windows executable to a linux box and run nm on it there.

You could also try dumpbin, which dumps info about a binary on Windows. You can get info on symbols with the /SYMBOLS switch, but it doesn't look like it directly provides information about their size.

like image 178
Todd Gamblin Avatar answered Nov 04 '22 07:11

Todd Gamblin


In Windows under Visual Studio compiles, this information is in your .map file (it'll be near the .pdb).

ADDED: To convert the decorated names found in the .map file to something more human-readable, you can use the undname.exe utility included with Visual Studio. It accepts individual names on the commandline or you can feed it a .map file.

For example,

Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of "?push_back@?$mini_vector@U?$Point@U?$FixedPoint@$0O@H@Math@@@Math@@$05@@QAAXABU?$Point@U?$FixedPoint@$0O@H@Math@@@Math@@@Z" is 

"public: void __cdecl mini_vector<struct Math::Point<struct Math::FixedPoint<14,int> >,6>::push_back(struct Math::Point<struct Math::FixedPoint<14,int> > const &)"
like image 7
Crashworks Avatar answered Nov 04 '22 06:11

Crashworks


Get a link map, or use dumpbin to get a list of symbols and sizes.

Chances are there's a lot of stuff being pulled in that you don't strictly need.

ADDED: Did you get a satisfactory answer? I realized there are two ways people approach problems like this:

  • Get measurements before they do anything.
  • Just find something big that they don't need, rip it out, and repeat until they can't.

Personally I prefer the latter - it gets results quicker.

You say the app is 4MB. Suppose the true necessary size is 1MB (or some such size). That means if you pick a routine at random from the map file, it is 75% likely to be something you don't need. Find out what's causing it to be included, and see if you really need it.

In the example you gave, you saw a class that wraps device-independent-bitmaps. You could find instances of that class in your app, and possibly replace them with basic WIN32 bitmaps. It would be less pretty, but save gobs of app size.

Then keep on doing it. Each large piece you get rid of makes the remaining pieces take a larger percentage of the app, because the app has shrunk but the pieces haven't. That makes them easier to find in the map file.

like image 2
Mike Dunlavey Avatar answered Nov 04 '22 06:11

Mike Dunlavey