-1

I just want to know if it makes a performance-difference while copying objects in C++ if I use many instances of a class or use std::shared_ptr.

Background: I have some structures which are delivered through a signals&slot mechanism (Qt). (I know that instances are copied while sending a signal) These delivering can occur many times so it has to be fast with low memory usage.

edit (add some details):
I write an embedded application (yeah, Qt is not the fastest for embedded backend I know) which can have a dynamic number of "modules". Each module has its own functionality. Every module has a signal and a slot. Which module receive emitted signals is freely configurable. So it could be that many signals are emitted in a very small time. In this case the signals has to be delivered as fast as possible.
The delivered structure has some module-specific data and the data which has to be delivered to the other modules. I cannot say how large the delivered data will be because on the future there will be many more modules which maybe delivers much data.

BTW: I abuse std::shared_ptrin this case. I do not use I for really sharing the ownership. Qt just treat references and instances the same way in signals&slots, it copies the object. So to have the benefits of both, easy memory management of instance and lower memory usage of reference, I thought of using a std::shared_ptr.

Psy-Kai
  • 187
  • 3
  • 13
  • Measure, Measure, Measure. – NathanOliver Sep 01 '15 at 12:07
  • Why don't you write a benchmark? – m02ph3u5 Sep 01 '15 at 12:08
  • Copying a pointer is almost free (shared ptr slightly more expensive), copying complex objects are expensive. But it's pretty hard to give a correct answer without details. – Melkon Sep 01 '15 at 12:08
  • 1
    How many copies do you anticipate? How large are they? And I don't understand the dichotomy here. What do you mean "many instance of a class or std::shared_ptr"? Use shared_ptr if you mean to share a single instance. Use unique_ptr if you mean to have one owner, then share the raw pointer. Use weak_ptr if you mean to share the instance but not ownership - to avoid cycles. Sometimes making a copy will be faster (threading for instance). – Robinson Sep 01 '15 at 12:23
  • Completely unanswerable without profile details. – Puppy Sep 01 '15 at 17:11

1 Answers1

0

Qt just treat references and instances the same way in signals&slots, it copies the object.

No, it only copies in specific circumstances. See this answer for details. TL;DR: It only copies if it needs to deliver the call over a queued connection, and then only if there is a receiver attached to a given signal. With direct connections (or default automatic connections within the same thread), if both the signal and the slot pass the argument by reference, then no copies will be made.

I abuse std::shared_ptr in this case.

It's not abuse. You're passing what amount to a shared data structure, held by a shared_ptr. It makes perfect sense.

The real question is: if your structures are expensive to copy, why won't you use explicit sharing via QSharedData and QExplicitlySharedDataPointer? And why doesn't your question include measurement results to substantiate your concern? Come on, such things are trivial to measure. You've got Qt to help you out - use it.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • ok, some things for me to read, thx. I avoided the pointer-classes of Qt until now (I am just at the end of my apprenticeship so I am not so experienced). – Psy-Kai Sep 02 '15 at 09:32