improvement in Font::drawText: fix or only workaround?

Stefan Heimers stefan at heimers.ch
Sat Mar 5 10:40:55 CET 2005


Am Samstag, 5. März 2005 09.58 schrieb Simon Hausmann:

> That's not a problem. QConstString doesn't create a copy of the data,
> it's cheap to construct and destruct.

Hi,

It's still creating a new object, which is not negible even if it's not 
that bad.

If it's not possible to fix things in Qt, I would recommend checking the 
size of the string and only creating a substring if the original qstr 
is larger than a certain size. You could guess that size to be about 
50kB, or do some more tests to see at which exact size the workaround 
starts being better than the original code.

Don't do a qstr.length(), that's another unnecessary function call. 
Font::drawText is called with a length argument "int slen". Use this, 
so you only need to compare two integers to decide if the copy is worth 
creating, which is really cheap.

My proposed code:

// TODO: remove this workaround in case the QPainter::drawText
// can be optimized
if (slen > 50000){
  // patch from  l.savernik at aon.at
  QConstString cstr = QConstString(str + pos, len);
  p->drawText( x, y, cstr.string(), 0, len, d );
}
else{
 // original code
 p->drawText( x, y, qstr, pos, len, d );
}



Stefan


More information about the Kde-optimize mailing list