Performance
Many compilers will optimize the switch into a jump table just as would be done in an assembler implementation. C's default fall-through in case statements has long been one of its most controversial features; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against."
The primary increase in speed versus a simple, straightforward loop comes from loop unwinding, which reduces the number of branches performed (which are computationally expensive due to the need to flush - and hence stall - the pipeline). The switch/case statement is used to handle the remainder of the data not evenly divisible by the number of operations unrolled (in this example, 8 byte moves are unrolled, so the switch/case handles an extra 1–7 bytes automatically).
This automatic handling of the remainder may not be the best solution on all systems and compilers — in some cases two loops may actually be faster (one loop, unrolled, to do the main copy, and a second loop to handle the remainder). The problem appears to come down to the ability of the compiler to correctly optimize the device; it may also interfere with pipelining and branch prediction on some architectures. When numerous instances of Duff's device were removed from the XFree86 Server in version 4.0, there was an improvement in performance. Therefore, when considering using this code, it may be worth running a few benchmarks to verify that it actually is the fastest code on the target architecture, at the target optimization level, with the target compiler.
Read more about this topic: Duff's Device
Famous quotes containing the word performance:
“True balance requires assigning realistic performance expectations to each of our roles. True balance requires us to acknowledge that our performance in some areas is more important than in others. True balance demands that we determine what accomplishments give us honest satisfaction as well as what failures cause us intolerable grief.”
—Melinda M. Marshall (20th century)
“So long as the source of our identity is externalvested in how others judge our performance at work, or how others judge our childrens performance, or how much money we makewe will find ourselves hopelessly flawed, forever short of the ideal.”
—Melinda M. Marshall (20th century)
“To vote is like the payment of a debta duty never to be neglected, if its performance is possible.”
—Rutherford Birchard Hayes (18221893)