you are viewing a single comment's thread
view the rest of the comments
[–] 2 points 11 months ago (3 children)

I feel like an idiot. Also, in the "Good" example, no underflow occurs. i goes from 0 to -10, and x is assigned to -i every loop.

It might still be possible to optimize away the random number example, if the random function were made a magic language item, but it would not be even remotely close to being worth the effort.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 2 points 11 months ago (2 children)

    The question the optimizer can't really answer is: will Random.nextInt() ever return 10? If that's a 64 bit integer it could be a LOOOOOONG time before 10 ever shows up.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 11 months ago

    If it were a magic lang item, you could treat the resulting value in a special way. Then, you could create an optimization pass for this situation: if a variable is assigned random in a loop and the loop can only be exited with a certain value, the compiler can coerce the magic rand value to it.

  • source
  • parent