Hiya,

In 3.5 branch, ChatMessagePart::formatStyleKeywords is a pretty expensive 
method. It's called for every message. But worse, it's called a *LOT* when 
the style changes, and this happens whenever a buddy icon changes.

A changing buddy icon on an open chat with a few hundred KopeteMessages is 
horribly expensive. formatStyleKeywords is called again for each of them.

Callgrinding Kopete I came up with the attached patch. Simple as it is, it 
already makes the method about 20% faster by killing most calls to the 
expensive QColor::name() and the conversion from a string to a color.

Additionally, replacing QString::arg() with a simple append also saves some 
cycles.

There is one very big fish in there that I am looking at now, but these 
optimizations should already help. Ok to commit?

Any taker for porting to 4.0?

-- 
Martijn

I'm not a professional, I'm an artist.
Index: chatmessagepart.cpp
===================================================================
--- chatmessagepart.cpp	(revision 627731)
+++ chatmessagepart.cpp	(working copy)
@@ -1036,7 +1036,7 @@
 #endif
 		if( !message.from()->metaContact()->picture().isNull() )
 		{
-			photoPath = QString("data:image/png;base64,%1").arg( message.from()->metaContact()->picture().base64() );
+			photoPath = QString( "data:image/png;base64," ) + message.from()->metaContact()->picture().base64();
 		}
 		else
 		{
@@ -1078,8 +1078,9 @@
 	int hash = 0;
 	for( uint f = 0; f < contactId.length(); ++f )
 		hash += contactId[f].unicode() * f;
-	QColor color = QColor( nameColors[ hash % nameColorsLen ] ).name();
-	kdDebug(14000) << k_funcinfo << hash << " has color " << nameColors[ hash % nameColorsLen ] << endl;
+	const QString colorName = nameColors[ hash % nameColorsLen ];
+	QString lightColorName;	// Do not initialize, QColor::name() is expensive!
+	kdDebug(14000) << k_funcinfo << "Hash " << hash << " has color " << colorName << endl;
 	QRegExp senderColorRegExp("%senderColor(?:\\{([^}]*)\\})?%");
 	textPos=0;
 	while( (textPos=senderColorRegExp.search(resultHTML, textPos) ) != -1 )
@@ -1090,7 +1091,13 @@
 		{
 			light=senderColorRegExp.cap(1).toUInt(&doLight);
 		}
-		resultHTML = resultHTML.replace( textPos , senderColorRegExp.cap(0).length() , doLight ? color.light(light).name() : color.name() );
+
+		// Lazily init light color
+		if ( doLight && lightColorName.isNull() )
+			lightColorName = QColor( colorName ).light( light ).name();
+
+		resultHTML = resultHTML.replace( textPos , senderColorRegExp.cap(0).length(),
+			doLight ? lightColorName : colorName );
 	}
 
 	// Replace message at the end, maybe someone could put a Adium keyword in his message :P
_______________________________________________
kopete-devel mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kopete-devel

Reply via email to