Codebloat and templates - big deal?

Started by
17 comments, last by ApochPiQ 17 years, 4 months ago
For my last engine, I was compiling it to a static library, the debug version of which actually came to a total of 18mb and the release version came to 16mb. For one part, I'm actually generating a total of 22 template classes using boost preprocessor magic. They're not very big themselves, but they do use other parts of the boost libraries. Is it possible that generating these 22 classes and using them is causing such ridiculous sizes for the libraries? I think I'm also using RTTI. Does this have a big impact on the size of a program or library? PS. For those who don't know, the dll of the Irrlicht open source graphics engine (version 1.0) is a total of 1.5mb, and it does approximately 22079460347 times better than mine (just an approximation of course :D ).
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
More importantly - how big is a release executable linked against that static library?

Did you strip out all debug information & symbols?

I doubt templates are taking up ~20mb. Code is pretty small.

And which boost libraries are you using?
With a program that big it has to be due to a declaration of a large static array, not code bloat.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
With a program that big it has to be due to a declaration of a large static array, not code bloat.


I have a single static class, which isn't very big at all. It's members are an integer and a dynamic array, which allocates the array space on the heap.

That's it. I have a lot of stuff with the word "static" in their names (not the keyword), but they have nothing to do with actual static variables.


Quote:Original post by RDragon1
More importantly - how big is a release executable linked against that static library?

Did you strip out all debug information & symbols?

I doubt templates are taking up ~20mb. Code is pretty small.

And which boost libraries are you using?


I've compiled a basic tetris game (not completed) that uses my engine, and it is a total of 560kb with the library being 16mb.

I was also fairly sure that code is pretty small, but I was a little afraid that generating classes at compile time could rack up some space issues.

Okay, boost libraries. Do you mean actual libraries that I'm linking to? Because I'm linking only to the boost::regex library, but I'm #includ'ing a whole bunch of stuff to use:

  • boost::format
  • boost::variant
  • boost::regex
  • boost::preprocessor
  • boost::lexical_cast
  • boost::tuple
  • boost::type_traits
  • boost::function
  • boost::bind

So, with these ones, I'm simply including the headers, with the exception of regex, which I'm linking against it's library.

Yes, I'm as sure as I can be that I've set it to not include debug information. I've taken a look at the settings at anything that looks like it's associated with debugging, and it is all "no" for compiling a release version (and I did re-compile the library in release configuration).
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
the word "static" is extremely overloaded in the programming world. When they said it must be due to a large static library, they did not mean any code you have written yourself ... they meant code someone else had written, that you use, that is linked into your library or program statically (as a static library versus a dynamic link library).

For example the microsoft visual c++ runt time is about 0.5 mb, and the libc is about 2.5 mb ... many libraries are larger ...

besides boost, what do you link to in your program?
Apart from boost? I link to the OpenGL libraries, the standard C++, and probably C stuff. I use tinyxml, but I'm compiling the source for that, and not linking to a library. Also the stuff for compiling windows programs using VC++ express.

That's it. Nothing special or unusual as far as I know.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Quote:
I've compiled a basic tetris game (not completed) that uses my engine, and it is a total of 560kb with the library being 16mb.


Your executables are normal size - but your library is huge. What's probalby happening is that your library is full of stuff that's never being used - it's generally not a good idea to put explicit template instantiations in libraries, as they increase the size of the library even if they aren't used. Just stick with a header only library for template classes.

FYI, when your linker creates executables using your library, it only includes the code that's actually used, throwing away all the junk.
Quote:Original post by Nitage
Quote:
I've compiled a basic tetris game (not completed) that uses my engine, and it is a total of 560kb with the library being 16mb.


Your executables are normal size - but your library is huge. What's probalby happening is that your library is full of stuff that's never being used - it's generally not a good idea to put explicit template instantiations in libraries, as they increase the size of the library even if they aren't used. Just stick with a header only library for template classes.

FYI, when your linker creates executables using your library, it only includes the code that's actually used, throwing away all the junk.


For statically linked libraries, yes. For dynamically linked libraries, it basically doesn't include anything - but you have to keep a copy of the DLL around in order for the exe to work :)
Quote:Original post by Nitage
Your executables are normal size - but your library is huge. What's probalby happening is that your library is full of stuff that's never being used - it's generally not a good idea to put explicit template instantiations in libraries, as they increase the size of the library even if they aren't used. Just stick with a header only library for template classes.

A header only library for template classes? I'm not quite understanding that. The template classes are being generated in a header file, but of course are being included in source files.

Quote:Original post by Zahlman
For statically linked libraries, yes. For dynamically linked libraries, it basically doesn't include anything - but you have to keep a copy of the DLL around in order for the exe to work :)

You don't have to keep a copy of the library around when you're using a static library?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
In this case, static vs dynamic is referring to compile time vs run-time. A static library is linked at compile time, so the code is copied into the resulting executable (and there might even be some optimizations around the call boundaries). A dynamic library is linked at run-time, by the executable telling the operating system to load the dynamic library into its memory and locating the required functions. This allows the library to be replaced by a newer version without having to recompile; thus, the linking is more dynamic.

Only dynamic libraries are needed at run-time. The needed parts of static libraries were already inserted into the executable.

This topic is closed to new replies.

Advertisement