John C. Bollinger wrote:
> The above does provide a clue to the compiler, however, that it would
> be OK for some other variable outside the block to share storage with a.
> In fact, a smart compiler might even determine that i can share
> storage with a in that simple example.
It's worth emphasizing that this is true for a not-very-strict
definition of "smart".
Practically *any* modern compiler is going to notice and do this.
Modern compilers convert code into intermediate forms, generally using
something called SSA ("static single assignment"). SSA makes it
remarkably obvious which data can be shared in the same memory, but not
at all obvious which data originally had what scope or was assigned to
which local variable. In fact, if information about local variables and
scopes is saved at all, it's only so that the compiler can output
debugging information.
It's not necessarily true that *all* compilers use an SSA intermediate
form, and I haven't paid attention for a number of years, but it's quite
unlikely that a newer compiler would be less capable or more tied to the
form of the source code. That's true for native source compilers *and*
for Java JIT compilers. (I don't know about the Java bytecode compiler;
generally, it benefits from preserving information about source code, so
it may choose not to do this.)
The point is that while it may seem to a naive oberver to require some
degree of intelligence or special effort to rewrite the code in that
way, for a compiler with an SSA intermediate form or equivalent, it
would require immense amounts of intelligence to *not* do that
optimization. So don't look at this as an ideal-case scenario, but
rather as the norm. Small scope in a method's local variables is good
for code quality, but not performance.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation