How does one accomplish loop-unrolling as in Duff's Device in rust, if case statements do not fall through?
> It turns out that with branch predictions and the relative speed of CPU
> vs. memory changing over the past decade, loop unrolling is pretty much
> pointless.
http://lkml.iu.edu/hypermail/linux/kernel/0008.2/0171.html int remaining = length % 4;
switch (remaining) {
case 3: h ^= (data[(length & ~3) + 2] & 0xff) << 16;
case 2: h ^= (data[(length & ~3) + 1] & 0xff) << 8;
case 1: h ^= (data[length & ~3] & 0xff);
h *= m;
} match x {
0 => { /* do some stuff */; fall_through; },
1 => true,
_ => false
}
However, I don't know yet how useful it would be. I can't remember ever really needing it, so it would probably need a few practical examples before it became a reality but its an idea. int remaining = len % 4;
if (remaining)
{
do
{
remaining--;
h ^= (data[(length & ~3) + remaining] & 0xff) << (remaining * 8);
}
while(remaining);
h *= m;
}
Fall-through's interesting, but at the same time, as architectures have changed, has become less useful. Self-modifying code at one time was near vital, but has fallen by the wayside, fall-through is doing much the same.If you're truly sure you're better than the compiler, I'd imagine an assembly language implementation would be easier to understand than nested switch/while fall through madness.
That said. There's an implementation of Duff's device in the Wikipedia entry which (IMO is much easier to understand and maintain) that doesn't use fall-through that should be just fine to write in Rust.