Page 1 of 1

Optmization Policy

Posted: 05 Sep 2004 18:55
by matthijs
After seeing some patches, I noticed that a lot of people tend to over-optimize here and there.

In particular (the direct cause of this post) is replace this:

Code: Select all

a / 256
by

Code: Select all

a >>  8
The argument is, that bitshifting is a lot faster that dividing. I won't argue with that, because it is. But, I believe that making this optimization makes the code less readable, which is something we should prevent.
Besides that, this optimization is quite trivial and will allways be made by the compiler (I tested, even gcc -O0 does it).

There are probably other optmisations like this, though I can't recall anything from the top of my head right now. To prevent the code becoming even more unreadable because of these things, I propose the following policy:
  • The only kind of optmization that can be applied, is optmization in the algorithms or data structures used. Other optimisations that simply replace one piece of code by another, semantically similar one, should be handled by the compiler. Anything that isn't too complex for you, won't be too complex for the compiler either.
  • If any optmization is applied it should always be commented. No Exceptions.
  • If any optimization is applied, include the original code in comments.
    A hint here is to just code something the way you would want it and when applying optimization, comment out the original and replace it by the optimized code.
What do we think of this?

Matthijs

Posted: 06 Sep 2004 09:09
by lucaspiller
It sounds good, but remember quite a few of the patch makers had very little or no knowledge with C before finding this game, and have learnt it to make patches for it. I was one of these people and I didn't even know what bit shifting was until a few weeks ago.

It you could explain how all these optimisations work (or post some links) I think it would be a lot better for people to learn about them.

Posted: 06 Sep 2004 10:02
by Korenn
he wants us to NOT do optimizations, so you don't have to learn them ;)

His point is that most of the optimizations that coders apply are done by the compiler too, so you might as well leave the slower but more readable code.

Posted: 06 Sep 2004 10:31
by Darkvater
Optimizations are of course important. But senseless optimizations that if you want to divide by 256 you write >>8 instead of /256. Optimizations are important, but keep it sensible :)

If you are unsure if a given optimizations is senseless or not, have a look at the assembly output for a comparison :)