Welcome, Guest. Please login or register.

Author Topic: Optimization help needed (possible C limitation ?!)  (Read 2114 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
Optimization help needed (possible C limitation ?!)
« on: September 29, 2007, 06:31:58 PM »
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:
Code: [Select]

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 ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show all replies
Re: Optimization help needed (possible C limitation ?!)
« Reply #1 on: November 27, 2007, 11:21:24 PM »
@Caius
Thhx for the reply, been ages since I posted this... I've sometime ago modified the code to use an inlined function in one part, cheers.
\\"We made Amiga, they {bleep}ed it up\\"