This is actually the 1st thing I've run into that seems a disadvantage when compared to C++, in terms of code efficiency.
I have this loop that I know is gonna be used plenty. But a huge part of it's code, is also gonna be used somewhere else.
If I make it a function it's gonna slow down the loop. Maybe I could jump into it with goto's but with those there's no easy way to come back to the original code that jumped into it.
Another problem exists cause the function would need to make changes to some state variables which are common to other code parts where same function code is needed. In C++ this would be accomplished simply declaring the variables so that their scope spans both the function and the other parts of the code that call it (not possible in C). By the way, this could be done by declaring them globally in C, but in this case I can't cause they're to be used in a dynamic shared Library, so I need them to be made an indefinite number of times.
Solutions I've remembered:
- Use a call by reference to said variables. Well, it's not as efficient is it ? The indirect addressing modes are slower (someone will probably say I'm nitpicking at this point :-D).
- Make the function call arguments all register qualified so that the compiler simply jumps into the function directly (it's probably already using those variables in registers since they're in the loop). The same variables would also be declared as register types externally (in the code that calls the function) and the function would return it's values. Like:
struct StatusVars
{int StsVarA;
int StsVarB;
...
};
register struct StatusVars StsVars;
struct StatusVars CommonCode (register struct StatusVars StV);
...
/* Function would be called like this, so that changes to StatusVars would be common to exernal code */
StsVars = CommonCode (StsVars);
Would the compiler be smart enouph to COMPLETELY eliminate the common overhead in function calls on this case ?
Wich solution do you think is better ?