What is signficance of passing references in signals



  • In my most of legacy code . I am seeing passing parameters to signals as references Please find the example below

    QObject::connect(sourceView, SIGNAL(signal(const QString&, const QString&, int)), d_destView, SLOT(slot(onst QString&, const QString&, int)));

    what is the significance of the same



  • On paper references are faster as you don't have to copy the entire object but just copy its address (so 32 or 64 bits). In your example however, as QString is implicitly shared even passing by value would not trigger a full copy so in that snippet there is little to no advantage. In general, if the arguments don't need to be modified inside the method const references are often used for all but the basic types (int double, etc. as they are already 32 or 64 bits)


  • Moderators

    @VRonin said:

    little to no advantage

    I agree with the "little" part, but there is always an advantage. References are really just pointers. In case of implicitly shared object you need a pointer copy and refcount increase. Const references are just a pointer copy or not even that if the compiler optimizes it and the address is already in some cpu register. You also need to remember that implicit sharing involves atomic counters, which prevents some micro-optimizations the compiler can do.



  • To add to this, the actual connect statement should not include reference symbols if you use SIGNAL() and SLOT() as it will be slower, see http://doc.qt.io/qt-4.8/signalsandslots.html#advanced-signals-and-slots-usage



  • Hi @Qt-Enthusiast ,
    i'm not quite sure that i understand your question, but i found this about const references in signal and slots:

    Qt uses normalized signatures to decide whether two given signals and slots are compatible. Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with values.

    Read it here.



  • @VRonin A

    as I understand it will be slower to pass references in Signals and Slorts . Let me know if I am correct in my understanding


  • Moderators

    @Qt-Enthusiast I think QObject::connect can be slower, but not calling the slots after the connection was established. As long as you do not do many connects in a loop there is no need to care about performance here.


  • Moderators

    When using the SIGNAL/SLOT macros the signatures are looked up/compared as strings at runtime. To do that const and & are removed from the string, so it's marginally faster to not include them in the first place.

    What you really really should do is switch to the new connect syntax that uses function pointers:

    QObject::connect(sourceView, &SourceViewClass::signal, d_destView, &DestViewClass::slot);
    

    It's easier, doesn't require to remember parameters and their types, does type checking at compile time instead of silently failing at runtime and is a lot faster, as there are no strings involved at all and matching is done at compile time.



  • I am using qt.4.3.3 and if we can do that


  • Moderators

    Wow! 4.3.3 was released in like... 2007? From a security, bugfixes and support for current platforms standpoint it's ancient. Have you considered an update? At least to the top of the 4.x branch (which is pretty old by now too)?



  • Considered but Need approval from my seniors



  • @Qt-Enthusiast said:

    Considered but Need approval from my seniors

    I feel your pain. Good luck on that!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.