Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How large structs can be passed by value efficiently?

The rule of thumb is that it is okay to pass small structs by value and larger ones should be done pointers.

My question is where exactly is this cutoff point? How large can the structures be before you are better off passing them by pointer.

I know this will vary between platforms, but I assume some rough estimates could be given. A year or two ago I tried to figure out this on the PPC architecture and found to my surprise that one could pass quite a lot of data efficiently by value. Think 10 double values or so was just fine due to the large number of registers in PPC. By pointer actually involved more copying in and out of memory.

However I now I am on intel and I expect things could be different. Since the CPU don't have that many registers traditionally, but perhaps that is different on 64bit or floating point registers?

like image 679
Erik Engheim Avatar asked May 13 '09 14:05

Erik Engheim


People also ask

Can structs be passed by value?

A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases.

Are structs efficient?

Using struct has traditionally been more efficient, as searching for the correct method can be expensive. The performance of classes was significantly worse before Mathworks implemented the Execution Engine several releases ago.

Does C pass structs by value?

When structs are passed to functions, they are passed BY VALUE. That means that the function will receive a COPY OF the struct, and that copy is what is manipulated from within the function.

How are structs passed in assembly?

Structures, like arrays, are passed by reference, so as a parameter: they are just a 32-bit parameter (the pointer to the first structure member), and that pointer is pushed on the stack in the cases of cdecl and stdcall .


2 Answers

Okay, so I tried to follow advice and profile my code with using pointers and value. I also looked at the assembly code. It seems that the performance characteristics on x86 are quite different from PPC. On PPC the binary interface to C specified that arguments would be put into registers (there were so many to chose from), however it seems that even on 64bit x86 requires arguments to be put on the stack.

So that explains why on x86 passing by pointer always seems to be faster. However I noticed the compiler is very eager to inline. So it didn't matter in which way I did it. So I guess the conclusion is to use whatever passing which is convenient to you.

I think that favors pass by value, because working on copies of values is somewhat safer. My test case was a struct consisting of 4 doubles (so I guess that makes it 32 bytes on most platforms).

like image 50
Erik Engheim Avatar answered Nov 15 '22 17:11

Erik Engheim


If you search the web you'll find some guidelines about byte size for passing by reference and value. I would trust pretty much none of it. The only way to know that a particular struct is a problem is to

Profile It

This is the only way to know 100% that there is a problem.

Before the naysayers jump in. Yeah there are some obvious cases out there. I would not for instance ever pass a struct by value if it had say 100 members. But that wouldn't be for performance issues, it would be more for stack space problems.

like image 34
JaredPar Avatar answered Nov 15 '22 15:11

JaredPar