[Development] QString::arg()

Olivier Goffart olivier at woboq.com
Sun Feb 17 22:13:26 CET 2013


On Sunday 17 February 2013 23:53:35 Mohsen Akhlaghi wrote:
> QString::arg() must have Escapes cached.
> 
> Currently it iterates with every call that doesn't seem too perfect. The
> better solution is to have the iteration once at the first call after the
> value change so further calls won't need to  iterate again.
> 
> Since the maximum number  of arguments for a single pass is 9, it's not
> actually enough in action and many calls to arg() with cached method would
> be the best. In this method the cache won't be filled before the first time
> arg() is called. So we won't have problems memory wise.

Hi.

QString::arg is not very optimized, that's true.
I beleive caching is not a real problem. One bigger problem is that each call 
to args re-allocates a QString and copy all the data.

Notice that there is already calls to arg with many arguments which is not 
only safer (because your string could contains %1), but also faster, so it 
should be used instead. (that call also could be optimized using some kind of 
QVarLenghtArray instead of the QMap)

If you really want to optimize the usecase of 
QString(...).arg(...).arg(...).arg(...) , you could so something with 
expression template. (returning a QStringBuilder that lazily replaces the %1 
as it builds the string)
The problem is that not all overload of arg are inline, and even when it is it 
is not that easy to change in a binary compatible way. (not impossible, but  
hard)

Feel free to go ahead and try something. 
I don't know if it will be worth it. I would find hard to beleive that arg() is 
a bottleneck. If it is in your application, avoid calling it in loop, or use 
something else.

-- 
Olivier 

Woboq - Qt services and support - http://woboq.com http://code.woboq.org/qt5/



More information about the Development mailing list