[clazy] [Bug 361691] Suggestion to turn string into QStringLiteral instead of QLatin1String

Stephen Kelly via KDE Bugzilla bugzilla_noreply at kde.org
Sat Apr 16 17:35:13 BST 2016


https://bugs.kde.org/show_bug.cgi?id=361691

Stephen Kelly <steveire at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |---
             Status|RESOLVED                    |REOPENED
     Ever confirmed|0                           |1

--- Comment #10 from Stephen Kelly <steveire at gmail.com> ---
I modified the testcase to test a non-empty string:


#include <QString>
#include <QDebug>

#define N 100000

bool testQStringLiteral()
{
    qDebug() << "QStringLiteral";
    bool result = false;
    QString s;
    for (auto i = 0; i < 1000; ++i)
    {
      s += QStringLiteral("Long string part %1 --").arg(i);
    }
    for (int i = 0; i < N; ++i)
    {
        result |= s.contains(QStringLiteral("foo"));
    }
    return result;
}

bool testQLatin1String()
{
    qDebug() << "QLatin1String";
    bool result = false;
    QString s;
    for (auto i = 0; i < 1000; ++i)
    {
      s += QStringLiteral("Long string part %1 --").arg(i);
    }
    for (int i = 0; i < N; ++i)
    {
        result |= s.contains(QLatin1String("foo"));
    }
    return result;
}

int main(int argv, char **)
{
    if (argv == 1) {
        return testQLatin1String();
    } else {
        return testQStringLiteral();
    }
}


Here's my result

$ perf stat ./qttester 
QLatin1String

 Performance counter stats for './qttester':

       2780.971861      task-clock (msec)         #    1.001 CPUs utilized      
                 4      context-switches          #    0.001 K/sec              
                 0      cpu-migrations            #    0.000 K/sec              
               287      page-faults               #    0.103 K/sec              
     8,751,360,206      cycles                    #    3.147 GHz               
      (83.30%)
     1,093,537,940      stalled-cycles-frontend   #   12.50% frontend cycles
idle     (83.30%)
        18,016,598      stalled-cycles-backend    #    0.21% backend  cycles
idle     (66.60%)
    25,205,593,541      instructions              #    2.88  insns per cycle    
                                                  #    0.04  stalled cycles per
insn  (83.30%)
     4,581,167,754      branches                  # 1647.326 M/sec             
      (83.44%)
           132,291      branch-misses             #    0.00% of all branches   
      (83.39%)

       2.778918892 seconds time elapsed

$ perf stat ./qttester 1
QStringLiteral

 Performance counter stats for './qttester 1':

       3136.221850      task-clock (msec)         #    1.001 CPUs utilized      
                 5      context-switches          #    0.002 K/sec              
                 0      cpu-migrations            #    0.000 K/sec              
               281      page-faults               #    0.090 K/sec              
     8,869,692,354      cycles                    #    2.828 GHz               
      (83.28%)
     1,988,983,951      stalled-cycles-frontend   #   22.42% frontend cycles
idle     (83.28%)
        20,247,953      stalled-cycles-backend    #    0.23% backend  cycles
idle     (66.67%)
    25,190,950,431      instructions              #    2.84  insns per cycle    
                                                  #    0.08  stalled cycles per
insn  (83.40%)
     4,580,392,600      branches                  # 1460.481 M/sec             
      (83.40%)
           138,789      branch-misses             #    0.00% of all branches   
      (83.40%)

       3.133671112 seconds time elapsed



I don't think clazy is right here. I think clazy is recommending something
which is against intuition, and I don't think the benchmarks here justify that.
Optimizing for an empty string doesn't make sense.

-- 
You are receiving this mail because:
You are the assignee for the bug.



More information about the Unassigned-bugs mailing list