comments on KDE performance tips

Roger Larsson kde-optimize@mail.kde.org
Sat, 18 Jan 2003 02:16:42 +0100


On Friday 17 January 2003 18:00, Maksim Orlovich wrote:
> > Do you still remember pre KDE 2.0 times ?
> > At this time there was a *slow* data structure used (was it a simple li=
st=20
?),=20
> > and changing it to a tree improved the performance dramatically.
> >=20
> > But of course string comparisons aren't the fastest thing in the world.=
=20
But=20
> > I'm afraid there is no way around it.
>=20
> Sure there is, more than one.
>=20
> a. Use a hashtable - they do a single comparison, or a constant number of
> comparisons, not a logarithmic one, and no re-balancing (which takes
> time).=20
>=20
> b. Use hashes of key strings within the red-black tree context for
> first-try comparisons, only resorting to string comparisons in case of  a
> tie (doesn't kjs use a technique like that?)
>=20

c. Use QString as it first compares the location of the string before
doing the actual char by char comparison.

Take a look at:
	const QString hallo =3D "hallo"; // constructor with copy
	if (hallo =3D=3D hallo) ...  // only compares the string storage location

	if (hallo =3D=3D "hallo") ... // no copy but full compare will be necessary

	if (QString("hallo") =3D=3D QString("hallo")) ...=20
	// full constructors with copy of strings, and full compare, and destructo=
rs
	// think about what would happen if this was a while loop instead...



The second case could get some help from the compiler. Two equal and consta=
nt=20
strings need to be saved at only one place.

Then the QString constructor needs to find out that this is such a string.=
=20
And let the constructor point to the static area... possible?
	Remember this case.
	const char *hallo =3D strdup("hallo");
	if (QString("hallo") =3D=3D hallo) ...
	Note: there will be only one string constant "hallo" stored in the program
	due to g++ option '-fmerge-constants' (default with -O2)
That is why we have QConstString, as a bonus space is saved and that
reduces time too. Less cache/pages misses.
	if (QConstString("hallo") =3D=3D QConstString("hallo")) ...=20
	// cheap constructors, no copy, comp ares only string storage location,
	// cheap destructors

	const QConstString bye =3D "bye";  // cheap constructor with no copy
	if (bye =3D=3D bye) ...  // only compares string storage location

/RogerL


PS
	* found an option in g++
		-Weffc++
	have anyone tried to compile with that one?

	* found a little optimization opportunity in QString.

	bool operator!=3D( const char *s1, const QString &s2 )
	{ return !(s1=3D=3Ds2); }

	Should return !(s2=3D=3Ds1) instead since (s1=3D=3Ds2) will match
	 =3D=3D(const char *, const QString &) in its turn.
	A compiler with automatic inlining will help...

	Probably Qt should always be compiled with that kind
	of optimizations.
	-finline-functions
DS

=2D-=20
Roger Larsson
Skellefte=E5
Sweden