Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
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 Caius

  • Sr. Member
  • ****
  • Join Date: Nov 2007
  • Posts: 294
    • Show only replies by Caius
Re: Optimization help needed (possible C limitation ?!)
« Reply #1 on: November 20, 2007, 08:20:15 PM »
Specifying the 'register' storage class is only a suggestion to the compiler. The compiler may very well choose to ignore it, for example if there isn't a sufficient number of registers for the compiler to use.

Also, compilers that does good optimization may choose to put values in a register without you specifying it. Which compiler are you using? Are you compiling with optimizations?

If your compiler supports it, you could try inlining the function. Again, the compiler may choose to ignore it. Inlined functions need to be very small for the compiler to do it. The trade-off is that the function will be inserted everywhere it's being called (much like a macro), so your program will usually become larger.

If your compiler doesn't support inlining, or your concerned that it may ignore it, you could try a macro.


Theology is just a debate over who to frame for creating reality.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: Optimization help needed (possible C limitation ?!)
« Reply #2 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\\"