evil optimization

View: New views
2 Messages — Rating Filter:   Alert me  

evil optimization

by Arne Goedeke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the following case the optimizer seems to assume, that the array does
not change during the loop and optimizes away the sizeof check:

  int main() {

  array a = ({ 1, 2, 3, 4, 5 });

  void magic(int k) {
  a = a[0..k-1] + a[k+1..];
  };

  for (int i = 0; i < sizeof(a); i++) {
  if (a[i] == 2) {
  magic(i);
  i--;
  }
  }

  return 0;
  }

I assume cases like this are hard to detect, especially because
this can me made even more complicated. I will put in some
  if (0) { ret = ({});}
equivalent to trick the optimizer for now.

arne

evil optimization

by Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>In the following case the optimizer seems to assume, that the array does
>not change during the loop and optimizes away the sizeof check:
[...]
>I assume cases like this are hard to detect, especially because
>this can me made even more complicated. I will put in some
> if (0) { ret = ({});}
>equivalent to trick the optimizer for now.

I guess it would be possible to check if any of the variables in the
expression are in min_number_of_locals (cf program.c:pop_local_variables()),
but I believe the problem is quite rare.

>arne