NeomerArcana
NeomerArcana

Reputation: 2311

How large should a type be to pass by reference rather than by value?

Considering move semantics as well, is there a sort of cutoff that defines when it's more expensive to pass by reference rather than by value?

I always see examples use fundamental types by value and some example class by reference. But if I have a small struct or a small class, is it better to pass these by value rather than reference?

Where's the cutoff?

Upvotes: 2

Views: 1399

Answers (1)

Lother
Lother

Reputation: 1797

A pass by reference is basically a pass by pointer (in terms of efficiency).

A pass by pointer/reference is moving one machine-native length piece of data (aka: one word, or 32-bits on a 32-bit machine, etc...)

So passing by value any sized data less than or equal to a native word will be as efficient as passing by pointer/reference.

If your data/object is larger than this, it will take multiple cycles to copy each word of that to the stack.

There are no efficiencies gained by passing by value when the data size is less than a word. The machine will execute a copy which takes just as long for half a word as it does a full word.

Upvotes: 2

Related Questions