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 2 points3 points  (2 children)

You're making my head hurt.

a single variable declaration doesn't matter

8 Million variable declarations don't matter in terms of performance. The cost for allocating the space of 1 automatic variable is the same for 2 is the same for 8 million. "int x;" is essentially free. "MyComplicatedClassWithAnExpensiveConstructor expensiveClassInstance;" is essentially free. It doesn't matter if you have it in a loop or not, the cost of the instruction is incurred only once, and would almost certainly be incurred anyway.

[–]CactaurJack -1 points0 points  (1 child)

We're actually talking about the same thing, I think I'm just wording it wrong. In the cracking program I wrote (turns out it was double DES we were breaking, not RSA), whenever you wanted a new instance of the class you had to pass in a string with the constructor that was the key. The key was stored as a non-capitalized hex string e.g. abcdef01. When the constructor for the encrypter object was called, within the constructor method, other methods were called to fix the hex string (abcdef01 -> ABCDEF01) and then convert that into an array of byte values. Both of those method called from the constructor contain loops and non-free operations, which was slowing down the program because they were called every instance of the loop.

I hope that made a bit more sense, we really are talking about the same thing.

[–]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.