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

List:       kde-optimize
Subject:    Re: comments on KDE performance tips
From:       Roger Larsson <roger.larsson () norran ! net>
Date:       2003-01-18 1:16:42
[Download RAW message or body]

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 list 
?), 
> > and changing it to a tree improved the performance dramatically.
> > 
> > But of course string comparisons aren't the fastest thing in the world. 
But 
> > I'm afraid there is no way around it.
> 
> Sure there is, more than one.
> 
> 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). 
> 
> 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?)
> 

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 = "hallo"; // constructor with copy
	if (hallo == hallo) ...  // only compares the string storage location

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

	if (QString("hallo") == QString("hallo")) ... 
	// full constructors with copy of strings, and full compare, and destructors
	// 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 constant 
strings need to be saved at only one place.

Then the QString constructor needs to find out that this is such a string. 
And let the constructor point to the static area... possible?
	Remember this case.
	const char *hallo = strdup("hallo");
	if (QString("hallo") == 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") == QConstString("hallo")) ... 
	// cheap constructors, no copy, comp ares only string storage location,
	// cheap destructors

	const QConstString bye = "bye";  // cheap constructor with no copy
	if (bye == 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!=( const char *s1, const QString &s2 )
	{ return !(s1==s2); }

	Should return !(s2==s1) instead since (s1==s2) will match
	 ==(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

-- 
Roger Larsson
Skellefteċ
Sweden

_______________________________________________
Kde-optimize mailing list
Kde-optimize@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-optimize
[prev in list] [next in list] [prev in thread] [next in thread] 

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