R T
R T

Reputation: 1

Large memory-usage and object file size of certain C++ template class with -g compilation option

I have a template class simplified as following:

template<typename R>
struct MyStruct
{
    template<typename T>
    using ConstnessType = std::conditional_t<std::is_const_v<R>, std::add_const_t<T>, T>;

    char* name() const{return "MyStruct";};
    ConstnessType<int> myInt;
}

As it can be seen that type R is not actually instantiated other than used to define the other type. I have expected that using different Rs should result in the same instantiated code. However, when I compiled with -g and with different Rs, I found the compilation memory usage and final object file size are proportional to the complexity of Rs. Depending on the complexity of Rs, I had seen almost 10 times difference in the memory usage and final object file. Is this desired behavior? Or is there some compiler option to let the compiler smartly recognize there is no need to generate the debug info for the un-instantiated template parameter? Thanks!

Upvotes: 0

Views: 328

Answers (1)

metal
metal

Reputation: 6332

Welcome to Stack Overflow!

It's not entirely clear to me what your desired result is other than less memory usage.

You don't mention what compiler or debugger versions you are using, but here are four things you could try:

  1. Different debug info types (e.g., -g does the platform default, -ggdb does gdb-specific info, -gdwarf does DWARF info)
  2. Different debug info levels (e.g., -ggdb1 vs. -ggdb2 and -ggdb3, and -gdwarf4 is a different format entirely). Tinkering with these may yield different debug info more to your liking. Or not.
  3. Using the strip command to remove debug symbols where you don't want them.
  4. Selectively (or globally) increasing the optimization level. Technically this is mostly orthogonal to debug info, but it may still give you different behavior and footprint. You can do this for individual files with the -O switches, or you can do it for blocks of code with pragmas, as described here, e.g.,
#pragma GCC push_options
#pragma GCC optimize ("O3")

/*
 * Code that needs optimizing
 */

#pragma GCC pop_options

Upvotes: 1

Related Questions