TODO list addition (struct optimization)
Hi all,
please add (something like) the following text to
http://janitor.kernelnewbies.org/TODO :
----------
Examine struct definitions used in kernel files and kernel driver files on whether they have optimized layout.
Very frequently accessed struct member variables (e.g. locks or I/O address base values) should be at the first struct position.
This will save additional asm opcodes (at least on x86) and code will thus be faster and smaller.
Silly but simple example code making use of a struct:
void lock(void **lock)
{ ... }
...
lock(&lck.mylock);
...
** The BAD Case **
typedef struct {
char *name;
void *mylock; <--- not at first position, thus inefficient!
} lock_t;
resulting x86 asm:
8048444: 8d 45 f8 lea 0xfffffff8(%ebp),%eax 8048447: 83 c0 04 add $0x4,%eax <--- extra offset needed! 804844a: 50 push %eax 804844b: e8 e1 ff ff ff call 8048431 <lock>
** The GOOD Case **
typedef struct {
/* most-accessed member first! */
void *mylock;
char *name;
} lock_t;
resulting x86 asm:
8048444: 8d 45 f8 lea 0xfffffff8(%ebp),%eax 8048447: 50 push %eax <---- no extra offset! 8048448: e8 e4 ff ff ff call 8048431 <lock>
opcode savings: 3 bytes (9 vs. 12 bytes!)
Note that you should find out the most frequently accessed member not only by looking at the *number of code places* that actually access this variable, but also by properly adding up access counts in case a variable gets accessed multiple times in a *loop*.
Of course any structs that need to keep a backwards compatible struct layout should not be altered, though hopefully that won't apply to many structs.
----------
Thanks,
Andreas Mohr
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoting Andreas Mohr <[email protected]> Sent on Thu, 8 Dec 2005 11:13:31 +0100
Hello,
Quoted 6 lines
Examine struct definitions used in kernel files and kernel driver files on whether they have optimized layout.
Very frequently accessed struct member variables (e.g. locks or I/O address base values) should be at the first struct position. This will save additional asm opcodes (at least on x86) and code will thus be faster and smaller.
I'll surely look at this. I have a great interest on this subject. :)
Regards.
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 6 lines · Andreas Mohr
Examine struct definitions used in kernel files and kernel driver files on whether they have optimized layout.
Very frequently accessed struct member variables (e.g. locks or I/O address base values) should be at the first struct position.
This will save additional asm opcodes (at least on x86) and code will thus be faster and smaller.
Not necessarily true.
Suppose you have a group of struct members which are mostly written in sequence. You move one member out of cluster. Now you produce 2 dirty cachelines after each sequence.
Or a split group of mostly-read members will require filling of 2 cachelines instead of one.
This stuff is not all clear and not obvious. You may want to look at
[PATCH] reorder struct files_struct
P. S.: search on MARC is down, so I can't find more examples of what was
already done.
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors