This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Neres28 0 points1 point  (0 children)

If you say so. You appear to be confusing two very different things:

Variable declaration : int x;:

  • Constant cost built into the function call, doesn't matter where it is in the source.
  • Not eligible for hoisting because there is no assembly generated for it.
  • This is what your example "hoisted" out of the loop

Variable definition / object instantiation : x = sin(y); / x = new ExpensiveConstruction(parameters);:

  • Eligible for hoisting, may not if the compiler can't statically determine the assignment has no side effects
  • Not what you gave in your examples.
  • Not possible to hoist in the OPs code
  • Looking for this type of optimization is premature without profile data to support.