I have a huge problem. I have a common library, that is used all across my project. This library intensively uses boost.spirit
and boost.fusion
. Unfortunately, the library is approx. 700Mb in size. All the boost.spirit
-heavy code is used and it works well. What steps can be done to reduce its output size? Is there is a tool that can help to determine what template instantiations waste most of the space?
At first, I decided to move all spirit-aware code to cpp files. Second, I will try different compiler flags to optimize for size. I don't know what else to do.
Update(details)
I'm using GNU toolchain. Huge library is actually a static library. Executable, that uses this 700Mb library is 200Mb in size. At least half of the code is in *.h files. Some boost.spirit
grammars (very template heavy thing) is also located in *.h files.
Cheers!
Moving the spirit aware code to .cpp
files is a good first step, it might be incomplete though as you mention having spirit grammar in header files.
Make sure than none of the grammar / rules are ever exported outside the library. If you have the typical include
/src
directories, then move those files (even if headers) within the src
directory.
Mark all those symbols as internal to the library. They should not be accessible from outside the library at all. There are specific pragmas/attributes depending on your compiler, on gcc lookup the visibility attribute: __attribute__ ((visibility ("internal")))
. This helps the compiler optimizing them accordingly, notably a compiler may emit the code of a function even if it inlines it at a given call site, just in case this function address is taken. With internal visibility however, since it knows the code will not leave the object, it may elide the function.
I seem to remember a flag to fuse identical function bodies but cannot seem to find again...