Re: [cc65] snapshot #1

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2001-09-26 17:31:27


Hi!

On Wed, Sep 26, 2001 at 04:25:36PM +0200, MagerValp wrote:
> So would I -- just post the changes here. I know how to optimize asm,
> but I'm fairly clueless when it comes to C.

With a really good compiler, it should not be needed to write C code in a
special way to optimize it, but unfortunately cc65 is far from being an
optimizing compiler, so...

...most of the hints to help the compiler generate good code are listed in the
"coding.sgml" document in the doc directory, which is also available as an
HTML version at http://www.cc65.org/doc/coding.html.

In the given case of the plasma demo, the most helpful change was to replace
all post-increment operators by the pre-increment form. Since the demo uses
nested for loops, all additional code gets executed 1000 times in each frame -
and this really adds up.

The problem with the post-increment operators is that it tells the compiler
that the old value (before the increment) may be used later. The cc65 compiler
is not smart enough to figure out in all cases that this never happens, so it
generates additional code. If you write pseudo code for the pre-increment and
post-increment operators you will end up with something like this:

    unsigned postincrement (unsigned* v)
    {
        unsigned save = *v;
	*v += 1;
	return save;
    }

    unsigned preincrement (unsigned* v)
    {
        return *v += 1;
    }

The need for a temporary copy is what makes the difference here.

C++ programmers usually have less problems using pre-increment instead of
post-increment, because C++ lets you write these operators yourself for some
classes. And when writing these operators, one will notice that the
pre-increment operator is always a lot simpler than post-increment one (this
is of course also true for pre-decrement compared to post-decrement).

The new compiler will make some additional efforts to find useless temporary
copies, but this may still fail.

So my advice for any C programmer is to use the pre-increment/decrement form
whenever possible. This is even more true for C++ programmers, because in the
case of C++, such an operator may involve generation of a temporary copy of a
class instance, which is even more expensive than the copy of a native data
type.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:42 CET