Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

compiler optimization



  • Hi,

    I was wondering if the following statement makes any difference in amount of executed code:
    a) myQString.remove("something");
    b) myQString = myQString.remove("something");
    same for append prepend etc.

    wouldn't b) be on more instruction? If yes do recent compilers "fix" this?
    The syntax of b) is IMHO much clearer and I prefer it, but it isn't obvious to me and it seems different from
    i = i+1 or i +=1 as a function => .remove/.append etc is used.



  • I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

    Extra instructions:

    mov     rsi, rsp
    mov     rdi, rsp
    call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
    


  • @VRonin Thank you for the answer.
    I will mark the topic solved later today hoping someone miraculously says but moc does this or something.
    Anyways I will fiddle with the spacing/linebreaks of that code to like option a) more...

    Thanks again.


  • Lifetime Qt Champion

    Hi @damianatorrpm,

    QString::remove modifies the object it is called on. Don't fool yourself by writing:

    myQString = myQString.remove("something");

    because you might be tempted to write:

    const QString new = old.remove("foo");

    also, which modifies old, but that might not be obvious by looking at the code.

    Regards



  • @aha_1980

    because you might be tempted to write:
    const QString new = old.remove("foo");

    This is basically the reason I started b), long story, but I have to refactor that part where I started b) anyways, fewer_instruction.append("are fewer instructions").

    Thanks everyone.

    EDIT: I think I will switch to the operator type appending where possible to give contrast to functions that edit the data in place.


  • Qt Champions 2017

    @VRonin said in compiler optimization:

    I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

    What's the code you checked with and what optimization level? std::string and its slave classes are inline, so the compiler sees it all and should've inlined the call at the very least (if not using -O0).

    Edit: iirc recent standards made RVO mandatory.

    @damianatorrpm said in compiler optimization:

    I was wondering if the following statement makes any difference in amount of executed code

    It's more complicated than that; the number of instructions is not a direct measure. Instructions are not created equal, and there are specifics that are more important than the actual instruction (like branching and data colocation). On that topic, have you proven that this piece is a bottleneck to begin with? If you haven't, don't try to microoptimize it preemtively.


  • Moderators

    @VRonin said in compiler optimization:

    I checked the compiler output with g++ (and using std::string instead of QString)

    That might give a bit misleading results because QString is implicitly shared (copy on write) and std::string is not. I have no idea if it matters in this case, though.


  • Qt Champions 2017

    @sierdzio said in compiler optimization:

    I have no idea if it matters in this case, though.

    A great deal. QString has an additional indirection through the d_ptr and this is the great wall of china standing in front of the optimizations the compiler could possibly make.


Log in to reply