Menu

What is the history behind performance warnings to copy parameters by const reference for Qt classes that use Copy On Write

2020-07-31
2024-05-02
  • Steve Albright

    Steve Albright - 2021-09-24

    @danielmarjamaki - any comments on this?

     
  • Daniel Marjamäki

    I think it would be good to revisit this check. It's an old check. I did not consider QString and I agree it should be considered. Maybe it should recommend passing by value and using std::move in some situations.

     
  • Steve Albright

    Steve Albright - 2024-04-29

    With the new return by reference check, I am getting more pushback on making changes for the same reason.

    My preference is const references as much as possible and I don't understand this pushback.

    Is CPPCheck going to revisit this or has the decision been made?

     
    • Daniel Marjamäki

      Please feel free to adjust the checking.

      I don't have time to look at all tasks I want. :-( So help on this would be appreciated.

      I would assume there is also push back for parameters because sometimes it's preferable to use std::move instead of const references. I implemented the checker before there was std::move so it doesn't properly determine if std::move or const reference should be used.

      Why there is push back for the new return by reference checking, I don't know. Except when there is some clever classes that does not copy all the data.

      If we can avoid false positives for QString, etc that sounds good to me. Maybe we need some configuration option in the qt.cfg..

       
  • Oliver Stöneberg

    Qt doesn't really follow the pattern of returning const references because of the way the objects are implemented.

    Although the copy of QString does not imply the same cost as a non-Qt std::string it will still do the ref-counting stuff. that might be dismissible in most cases but in a very hot path that might still cause performance issues. Looking at the Qt documentation they seem to indicate it is mainly about memory usage. That used to be a major issue in earlier Qt versions possibly because people where just using copies and there was few tooling which pointed out that it was an unnecessary copy (people still regularly use auto instead of const auto& in range loops).

    It is similar to std::shared_ptr and it often comes more down to the coding guidelines or personal flavor on how people handle this. In some cases it might just be that you are not aware of what is going on e.g. std::function which might also become heavy in case it is a lamdba with heavy copies in the capture.

    I will see if I can put together a benchmark in the next few days.

     
  • CHR

    CHR - 2024-05-01

    Not sure if that is worth it. Since there is no downside at all to using const refs (except people not wishing to touch their code), I don't see why we should have extra code to accomodate copy-on-write classes.

     
    • Steve Albright

      Steve Albright - 2024-05-02

      A possible downside could be that the content of your const reference changes (maybe multi-threaded - mentioned above too) whereas a copy does not.

      Some of this is being discussed related to the return a reference check here: https://sourceforge.net/p/cppcheck/discussion/general/thread/184a00605c/

      I use const references as much as possible for parameter types and haven't seen any problems, but I'm interested to see how this plays out with the (similar?) return a reference check.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.