Tip: library size reduction

Christoph Feck christoph at maxiom.de
Thu Jul 2 18:36:46 BST 2009


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




More information about the kde-core-devel mailing list