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

List:       kde-core-devel
Subject:    Tip: library size reduction
From:       Christoph Feck <christoph () maxiom ! de>
Date:       2009-07-02 17:36:46
Message-ID: 200907021936.47283.christoph () maxiom ! de
[Download RAW message or body]

Hi there :)

Not that we run out of disk space, but now that KDE is more and more used on 
embedded platforms, we could use a simple trick to save some space.

The problem is with class constructors. Imagine a simple dialog class that is 
created from an .ui file:

	MyDialog::MyDialog(QWidget *parent)
	 : QDialog(parent)
	{
		ui.setupUi();
	};

Now, where would you save space? Obviously, you cannot write that any better 
(except maybe doing the UI setup by hand to save some setObjectName() calls 
on layouts, but this is not what I am after).

The problem is GCC "bug" 2163 (Code for constructors emitted twice). If you 
objdump the generated code, you will indeed see the whole UI setup in the 
binary twice, because gcc generates two constructors (and it generates three 
destructors). And the larger your UI gets, the bigger the space wasting.

The solution is to use a simple trick that is used throughout in Qt library 
code: use a private init() method to do the actual work for the constructor.

	MyDialog::MyDialog(QWidget *parent)
	 : QDialog(parent)
	{
		init();
	}

	void MyDialog::init()
	{
		ui.setupUi();
	}

I did not check how many classes in KDE have "complex" constructors to measure 
the actual savings we could get. But I guess there is several hundreds KB to 
save in an KDE install.

Christoph Feck (kdepepo)

Reference: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2163
[prev in list] [next in list] [prev in thread] [next in thread] 

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