-
-
Notifications
You must be signed in to change notification settings - Fork 4
Description
At the moment, the compiler has a nice little memory-usage optimization where, once we've generated a self-standing bit of code, we write it immediately to the output file rather than storing it in RAM until compilation's finished.
This means, however, that we end up writing a lot of methods which go unused. Once a class has been defined, all of its methods are immediately written to the output file, even if some or all of them are never used anywhere else in the program
In C++, the generated assembly for:
#include <iostream>
struct Example {
void A() {
std::cout << "This code is never called" << std::endl;
}
};
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}Is exactly the same as the generated assembly for:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}Even when compiled with -O0. That is: g++ doesn't even consider this to be an optimization.
We'll have to forego the nice bit of memory saving in order to save on the size of the compiled program & remove unused functions.
Exceptions should be made for compiling libraries for dynamic linking, which obviously are unlikely to use their own methods, but expect those methods to be used by someone else
One quick note on when we should consider a method to have been used:
- Build up a chain of references for each method:
Ahas been referenced byB,Bhas been referenced byCetc, thereforeAandBshould be considered used onceCis used - Mark something as "used" once it's been referenced in any context outside of a class, ie, in the main program, in a supershell, etc
- Methods marked as
@privatein libraries should not be marked as used until they're used by methods marked as@publicor@protected