Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is an object sufficiently large that there is a performance gain in passing it by reference instead of by value?

As answered in this question by Charles Bailey pass by constant reference should be considered when the object type is large but what kind of object is considered large?

EDIT: OK for providing more data that could be used to provide a more concrete answer I decided to add a real world problem to this description.

Suppose we have an object like this:

typedef struct dipl {
    quint16 __id;
    quint16 __pos;
    quint16 __len;
} data;

And we have another object like this:

class bidipl
{
public:
    bidipl();
    quint8 id();
    void setid(quint8 __id);
    void appenddipl(dipl __dipl);
private:
    qint8 _M_id;
    dipllist _M_dipllist;
};

Where dipllist is typedef QList<dipl> dipllist;. Objects dipl and bidipl are created once but accessed over maybe 100 times a minute. So how should we pass dipl to our append function?

void appenddipl(dipl __dipl);

Or

void appenddipl(const dipl& __dipl);
like image 819
moki Avatar asked Aug 12 '14 21:08

moki


People also ask

Why is it usually better to pass objects by reference than by value?

2) For passing large sized arguments: If an argument is large, passing by reference (or pointer) is more efficient because only an address is really passed, not the entire object.

Why should we use const references for function parameters rather than pass by value?

Passing a parameter by const reference should be chosen where the semantics of references are actually required, or as a performance improvement only if the cost of potential aliasing would be outweighed by the expense of copying the parameter. At times, copying your parameters can also give you locality benefits.

What does pass by const reference mean?

Passing By Reference To Const in C++ Passing By Reference To Const in C++ C++ is an example of a message-passing paradigm language, which means that objects and values are passed to functions, which then return further objects and values based on the input data.

How pass by reference has a lower cost than pass by value?

Time and space cost One advantage of call by reference over call by value-result is that it is often cheaper, especially if the data to be passed is large (time/space costs of copying). Call by reference may have a time cost because of indirect addressing, though that can sometimes be optimized out.


Video Answer


1 Answers

There are two separate costs associated with passing objects by value: Copying the data representation, and executing constructors. You need to consider the cost of both.

  • Copying data is generally fast, but keep it within reason. It's OK to pass struct { int[7]; } by value, but probably not so much for struct { int[20000]; }.

  • Executing constructors may perform dynamic allocation, which brings the risk of exceptions and the cost of synchronization. This may be negligible, or it may be important. It depends.

The only variables you should probably pass by value unconditionally are those of word-sized, built-in types. For everything else you should think about the trade-offs.

like image 181
Kerrek SB Avatar answered Sep 30 '22 08:09

Kerrek SB