Optmization Policy

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
matthijs
Engineer
Engineer
Posts: 76
Joined: 25 Jun 2004 15:20
Location: Enschede, Netherlands
Contact:

Optmization Policy

Post 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
User avatar
lucaspiller
Tycoon
Tycoon
Posts: 1228
Joined: 18 Apr 2004 20:27

Post 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.
No longer active here, but you can still reach me via email: luca[at]stackednotion[dot]com
User avatar
Korenn
Tycoon
Tycoon
Posts: 1735
Joined: 26 Mar 2004 01:27
Location: Netherlands
Contact:

Post 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.
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post 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 :)
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 34 guests