Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the compiler allowed to optimise out private data members?

If the compiler can prove that a (private) member of a class is never used, including by potential friends, does the standard allow the compiler to remove this member from the memory footprint of the class?

It is self-evident that this not possible for protected or public members at compile time, but there could be circumstances where it is possible regarding private data members for such a proof to be constructed.


Related questions:

  • Behind the scenes of public, private and protected (sparked this question)
  • Is C++ compiler allowed to optimize out unreferenced local objects (about automatic objects)
  • Will a static variable always use up memory? (about static objects)
like image 271
bitmask Avatar asked Dec 08 '20 15:12

bitmask


People also ask

How are compilers optimized?

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.

Do compilers Optimise code?

Compilers are free to optimize code so long as they can guarantee the semantics of the code are not changed.


2 Answers

Possible in theory (along with unused public members), but not with the kind of compiler ecosystem we're used to (targeting a fixed ABI that can link separately-compiled code). Removing unused members could only be done with whole-program optimization that forbids separate libraries1.

Other compilation units might need to agree on sizeof(foo), but that wouldn't be something you could derive from a .h if it depended on verifying that no implementation of a member function's behaviour depended on any private members.

Remember C++ only really specifies one program, not a way to do libraries. The language ISO C++ specifies is compatible with the style of implementation we're used to (of course), but implementations that take all the .cpp and .h files at once and produce a single self-contained non-extensible executable are possible.

If you constrain the implementation enough (no fixed ABI), aggressive whole-program application of the as-if rule becomes possible.


Footnote 1: I was going to add "or exports the size information somehow to other code being compiled" as a way to allow libraries, if the compiler could already see definitions for every member function declared in the class. But @PasserBy's answer points out that a separately-compiled library could be the thing that used the declared private members in ways that ultimately produce externally-visible side effects (like I/O). So we'd have to fully rule them out.

Given that, public and private members are equivalent for the purposes of such an optimization.

like image 56
Peter Cordes Avatar answered Oct 07 '22 19:10

Peter Cordes


If the compiler can prove that a (private) member of a class is never used

The compiler cannot prove that, because private members can be used in other compilation units. Concretely, this is possible in the context of a pointer to member in a template argument according to [temp.spec]/6 of the standard, as originally described by Johannes Schaub.

So, in summary: no, the compiler must not optimise out private data members any more than public or protected members (subject to the as-if rule).

like image 34
Konrad Rudolph Avatar answered Oct 07 '22 17:10

Konrad Rudolph