Written But Never Read Variables
Kye
Posts: 2,200
Does GCC optimize out written but never read variables in an object?
So, for example, calling setVar() calls the doCoolStuff() function and sets the value of var. If getVar() is never called then the var private variable is not needed. So, is GCC smart enough to remove this unused variable?
I wrote an object that has a large payload of cache variables like this that are written but never read. Since I need to save RAM I'm wondering if removing them will help.
class thingy { public: void setVar(int newVar) { var = newVar; doCoolStuff(); } int getVar() const { return var; } void doCoolStuff() { ... } private: int var; }
So, for example, calling setVar() calls the doCoolStuff() function and sets the value of var. If getVar() is never called then the var private variable is not needed. So, is GCC smart enough to remove this unused variable?
I wrote an object that has a large payload of cache variables like this that are written but never read. Since I need to save RAM I'm wondering if removing them will help.
Comments
inline int getVar() const { return var; }
In general it might make sense to separate the "setVar" so that it only sets a variable, and rely on the user to call "doCoolStuff()" manually.
https://github.com/omniacreator/omniacreator-plugin/blob/master/interfacelibrary/iloscilloscopegraph.h
So, all the code is in a class in a header only library. This means that the C file that includes my header only library can see everything.
I have these get and set functions which generate serial traffic and can take a long time to execute. So, to speed up the get function which has to ask an external computer for a data value I just cache the value when you set it.
As long as you don't call the get function the cached value is never needed.
As you can see in that file I linked to... the number of private variables has grown quite large... and may continue to grow if I add more features to these classes.
If the methods that read an objects state are never called then the compiler could, in theory, remove all the calls to methods that set that state.
As far as I know optimization is only done on a per file basis so the information is not available to the compiler to make such pruning.
Unless someone knows a magic option to get the compiler to optimize globally.
Marking methods as "inline" might do it, but as far as I know "inline" is only a suggestion.
Bottom line is, compile the thing, dump the resulting assembler and see what you have got.
Any easy way to go about doing that using SimpleIDE? The library is about 17K LOC.
Off the top of my head I'm not sure but if you can set the -g option in SimpleIDE to compile and include debugging symbols in the executable that is a good start.
Then you need to go to the command line and use the propgcc objdump command with the -A or -a options. Then you can search te output for the methods you think should or should not be there.
It's a long time since I have done this kind of thing, sorry to not be more specific.
Sure
Open the Project manager and right click on some filename to get the Popup window. You can also Enable Pruning in the Compiler tab to do compile time "garbage collection" (not memory management).