[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    A note to exceptions && some notes to codesize
From:       Mario Weilguni <mweilguni () sime ! com>
Date:       1999-06-07 18:58:18
[Download RAW message or body]

Here are some tips and tricks I found useful to reduce code and data size.
Maybe there is something interesting in for you. If not, or if you think
optimizing KDE is a waste of time please ignore me.

Today I browsed a little in the assembler listings for kdecore, and found some
interesting things:

Somebody mentioned here that exceptions can be used to report "no-more-memory"
for C++. According to the GNU info for gcc, this is not necessary, since the C++
standard requires that "new" never returns NULL. 

* the exception tables are quite big, and there are tons of _throw() calls that
are never used (Qt uses no exceptions, and the KDE core classes don't use them
as well). I see no reason having the libraries bloated with this exception code
if it is unused. Why not enable exception for libraries that use them, and
disable for all other libraries? I don't think that is a problem.

* avoid using too much Qt stuff when there are plain C methods for this. This
produces better code (shorter and faster), and avoids to have all those
constructors and destructors called. An example from kapp.cpp:
Instead of:
  QString aArgv0 = argv[0];
  int nSlashPos = aArgv0.findRev( '/' );
  if( nSlashPos != -1 )
	aAppName = aArgv0.remove( 0, nSlashPos+1 );
  else
	aAppName = aArgv0;

use this:
  char *nSlash = strrchr(argv[0], '/');
  if(nSlash)
    aAppName = nSlash+1;
  else
    aAppName = argv[0];

You don't think this is a good idea? Well, it saves 460 bytes of code and quite
some execution time (at least 300 clock cycles - not too much, a microsecond
on a modern CPU). And this are only 6 lines of code; the result is as easy to
understand as the old method.

To give you an idea how much this can make a difference: I patched Qt to save 15
bytes per QString declaration and recompiled kdecore, which was ~20KB
(2.5%) smaller afterwards and a little bit faster because of better pipeline
usage on the pentium class processors (avoids a jump). Unfortunatly I'm not yet
sure if this patch really works, I've yet to test it better. 

* take advantage of the stack. Stacks never get fragmented, and are easily
freed after the function finishes. Don't use QString if you can avoid it (of
course if not making the code totally unreadable or if you need Unicode
support). But for many cases the plain C string functions are better.


Probably I'll find more stuff like this the next days.

Ciao,
	Mario

--
It has just been discovered that research causes cancer in rats.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic