ARM: Efficient C for ARM: Hoisting

by David Thomas on

Hoisting

Constant expressions inside a loop can be hoisted to outside of the loop.

  • That way the code costs just once, rather than on every loop iteration.
  • Keeps code out of the way – denser code.

Examples

Doing the same test repeatedly in a loop:

txt->flags |= METRICS; /* for example */

for (i = 0; i < N; i++)
{
  if (txt->flags & TRANSLATE)
    translate(i);
  else if (txt->flags & METRICS)
    get_metrics(i);
  else
    draw(i);
}

We hoist out the loop:

if (txt->flags & TRANSLATE)
{
  for (i = 0; i < N; i++)
    translate(i);
}
else if (txt->flags & METRICS)
{
  for (i = 0; i < N; i++)
    get_metrics(i);
}
else
{
  for (i = 0; i < N; i++)
    draw(i);
}

Navigate