Re: Microsoft agrees with "hoisting loop invariants" by programmers!!! News @ 11

From: Corey Murtagh (emonk_at_slingshot.no.uce)
Date: 10/09/04


Date: Sat, 09 Oct 2004 18:02:33 +1300

Arthur J. O'Dwyer wrote:

>
> On Fri, 8 Oct 2004, Randy Howard wrote:
>
>>
>> <http://msdn.microsoft.com/library/default.asp?url=/library/en-
>> us/dnvc60/html/optcode.asp>
>>
>> Quoting from the article:
>>
>> Often, some code inside a loop, which is repeated many times, does
>> not change values during the loop's execution. These loop invariants
>> can be calculated once before the loop runs.
>>
>> Removing or hoisting loop invariants generally improves speed without
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> affecting size very much.
>
> Well, they're right, aren't they? I assume you're posting "hey,
> something vaguely amusing about Microsoft," but I can't tell whether
> you mean "hey, M$ finally figured out that invariant hoisting is a
> good thing!" or "hey, M$ is so dumb they think you need to hoist
> invariants in C++ code!" or perhaps something I'm missing.

I believe Randy is adding yet more evidence to the heap against eddie
who seems to believe that 'hoisting loop invariants' is a crime against
readability as well as needlessly premature optimisation that will be
performed by the compiler anyway.

> In any event, please note that in the rest of the article, Heller
> mentions that MSVC++6.0 does hoisting for you, so the C++ examples
> must be meant as a kind of pseudocode. I don't think the author
> expects people to hoist such trivial invariants in real C++ code
> that they're going to feed to an optimizing compiler anyway.

Where the optimiser is capable of doing so, sure. When the optimiser is
likely to choke on it you should of course help it along. This is
especially true of compilers with poor optimisation.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum videtur!"


Relevant Pages

  • Re: GNUH8 mixed C and assembly
    ... Using a volatile variable forces the compiler to include the loop and seems also to make the loop timing invariant with optimisation level. ...
    (comp.arch.embedded)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... Using DDR2 ram execution is roughly 2s and older DDR ram is about 3s in execution. ... A cache aware version of this vector add and accumulate is about 15% faster than the simple loop on a good optimising compiler (actually that is measured on MSC I haven't checked its code generation for optimisation - too tedious). ...
    (sci.electronics.design)
  • Re: A note on personal corruption as a result of using C
    ... the compiler to optimize. ... Please post a pointer to an article where somebody was "assaulted" for using an optimisable invariant in a for loop. ... Hardly "assault". ... I would expect it to be optimised any time I select optimisation by the compiler and it's optimisable. ...
    (comp.programming)
  • Re: wich is faster
    ... On the premature optimisation front - it has to be a judgement call by ... an "& 1" and a simple loop dividing n by odd numbers from 3 to sqrt. ... I do not accept that readability is an excuse ...
    (comp.programming)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... it's a for loop in the C sense. ... > sloppy thinking that results from confusing a programming language ... >> I do not believe that you are capable of writing a conforming C compiler. ... Does Microsoft's C compiler perform this optimisation? ...
    (comp.programming)