Author: Bruce Moreland
Date: 09:49:16 04/21/98
Go up one level in this thread
On April 21, 1998 at 10:07:57, Inmann Werner wrote: >Struct movetyp >{ >char from >char to >char beaten_figure (1 for pawn, 5 for queen etc.) >char promote figure (--"--) >char rochade (1 for small,2 for big rochade) >char ep_field (for epassant beats) >int evaluate (for move ordering) >char beaten_fig_pointer (the new field for do_move and >undo_move)!!!!!! >} When you have a structure like this, which is going to be used in arrays and in extreme high-performance situations, you have to consider the sizes of the items and their relative position. You have six chars followed by an int, followed by a char. Assume that we are talking about Intel machines for the rest of this post, so an int is 32 bits. I don't know about other compilers, but by default the Microsoft compiler will pad this structure. It will insert two bytes before the "int" field, and will insert three more bytes at the end. This will cause all of the elements to align on their natural boundaries, and will cause the structure as a whole to align properly when you make arrays of them. But if you stick "evaluate" before "rochade", you'll have the "int" after four chars, which is fine, and you'll have three chars after it, which is fine as long as the compiler sticks a dummy char at the end, which it will. So just by moving this integer a little bit, you save four bytes in your structure (25% of the size of the structure), and there is no down-side. Of course, if you turn structure packing on, you have a huge disaster either way, since this structure is 11 bytes long, which will cause the int to misalign no matter what you do. Misalignment won't cause you to crash, but it will affect performance, sometimes a lot. The best thing to do when you have a structure like this, is to try to make sure that everything in the structure is aligned on an n-byte boundary, where n is the size of the item. So a char can be anywhere, a short should be at 0, 2, 4, etc., and an int, unsigned, or long (once again assuming Intel throughout), should be at 0, 4, 8, etc. If you have something that is bigger than 4 bytes (a double or an __int64), I don't think it matters if you align it on an 8-byte boundary, I think 4 is good enough, unless you are running on an Alpha, when it matters again, so my suggestion is to align these bigger things properly if you can. In any case, you have room for one more char in that structure. Chess programming is an interesting change from normal business programming. The level of optimization you have to do tends to be a little bit greater, and your ability to verify that you did a good or bad job is a little bit greater as well, since there are other programs that *are* well-optimized. bruce
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.