I have an inner loop that executes many trillions of times. For various
reasons, I've implemented it via an m4 macro and instantiate it where
needed. Because of this, it is easy for me to try slight variations on code
in that loop and test the resulting operation rates. I just made a change
that I was sure wold be redundant to what the complier is already doing,
yet it turns out that I got a ~8% speedup here and that suggets a missing
compiler optimization.
The code is this (ignore the details just now, will explain the change
below):
m4_define(`COVER',`{
T_c := $1
if T_c.supply == 0 {
T_c.covered = true
T_c.next.prev = T_c.prev
T_c.prev.next = T_c.next
$2++
for T_i := T_c.down; T_i != &T_c.tNode; T_i = T_i.down {
for T_j := T_i.right; T_j != T_i; T_j = T_j.right {
T_j.down.up = T_j.up
T_j.up.down = T_j.down
T_j.col.size--
$2++
}
}
} else {
T_c.supply--
}
}')m4_dnl
The change is right at the top, "T_c := $1", which creates a block-local
variable of the value of the "function's" first argument. In the call
sites, this is sometimes a pointer, sometimes an index in to an array of
pointers (a[i]), and sometimes a member of a structure identified by a
pointer to structure ("thing *structname; ... COVER(thing.p)"). Before this
I had "$1" everywhere you see "T_c" now. The call sites look like this:
COVER(column,exact.Updates) // variable
COVER(j.col,exact.Updates) // struct member
COVER(forced[j],exact.Updates) // array element
Here is what is missing. When in a basic block, with a "RHS expression"
that needs to be validated (nil pointer, range check, ...) and that
expression appears multiple times, the checking happens multiple times,
even when the expression is as simple as a[i] where neither a nor i appear
on a left-hand side, or sp.member when sp is only used for reading.
Suggest that this go onto the compiler's optimization/CSE todo list. The
general idea is to CSE "whole expressions" before disassembling them and
doing CSE on their constituant parts.
8% is a lot of performance to waste.
Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
650-335-5765