MantisBT - Doomseeker
View Issue Details
0003814Doomseeker[All Projects] Cleanuppublic2020-06-07 05:202020-06-07 20:41
WubTheCaptain 
 
noneminorsometimes
confirmedopen 
1.3.1 
 
0003814: Inefficient string concatenation
operator+ creates temporary strings and unnecessary copies.
std::string’s (std::basic_string) class member function append(), and QString::appendmember, calculate the final result without any temporaries.
Affects performance.
Here's an example:
Quote from src/core/irc/ircnetworkadapter.cpp
void IRCNetworkAdapter::setChannelMode(const QString &channel, const QString &nickname, 
const QString &flag, bool bSet)
{
        /* ... */

        QString flagPrefixed;
        if (bSet)
                flagPrefixed = "+" + flag.trimmed();
        else
                flagPrefixed = "-" + flag.trimmed();

        /* ... */
}

There are many more. They're not always easy to search for.
$ grep -r '" + "' src/ | wc -l
1
$ grep -r '" + ' src/ | wc -l
98
$ grep -r ' + "' src/ | wc -l
118

Assumed good behavior:
$ grep -r "\.append(" src/ | wc -l
72
'https://clang.llvm.org/extra/clang-tidy/checks/performance-inefficient-string-concatenation.html [^]'
'https://www.qt.io/blog/efficient-qstring-concatenation-with-c17-fold-expressions [^]'
No tags attached.
Issue History
2020-06-07 05:20WubTheCaptainNew Issue
2020-06-07 05:20WubTheCaptainSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=13133#r13133
2020-06-07 05:20WubTheCaptainSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=13134#r13134
2020-06-07 05:23WubTheCaptainNote Added: 0021366
2020-06-07 20:41Pol MNote Added: 0021388
2020-06-07 20:41Pol MStatusnew => confirmed

Notes
(0021366)
WubTheCaptain   
2020-06-07 05:23   
Actually, that might be a terrible example in OP's steps to reproduce. Is it?
(0021388)
Pol M   
2020-06-07 20:41   
Sounds good to me :)
Quote from Wub

Actually, that might be a terrible example in OP's steps to reproduce. Is it?

This particular case is not inefficient as the constructed string will simply get assigned to the flagPrefixed (and the default constructed one will get destroyed), but I'm sure we have a few of these out there.