When is it more efficient to pass structures by value and when by reference to C #?

I have studied a little, and it seems that the general wisdom says that structures should be no more than 16 bytes, because otherwise they carry a penalty for performance when copying. With C # 7 and return returns, it became pretty easy to completely avoid copying structures as a whole. I assume that as the size of the structure decreases, going through ref has more overhead that just copies the value.

Is there a rule about when passing structures by value becomes faster than ref? What factors influence this? (structure size, process bit, etc.)

A little more context: I am working on a game with the vast majority of data presented as continuous arrays of structures for maximum convenience regarding cache memory. As you can imagine, the transfer of such structures is quite common in such a scenario. I know that profiling is the only real way to determine the impact of performance on something. Nevertheless, I would like to understand the theoretical concepts underlying it, and I hope to write code with this understanding in mind and profile only edge cases.

Also, please note that I am not asking about best practices or sanity to pass everything by reference. I know the “best practices” and consequences, and I intentionally prefer not to follow them.


EDIT - access to the tag "duplicate"

vs #.net - ref, , .

.Net, - ? - , .

:

? . , .

? , . , 99% . , .

?. ref . .

"", ? , CPU .

? . , , .

?. SO Q & A.

, . ? X , Y [.NET Version, , , CPU]. Linux? Android? iOS? ?

, . , , , -, CLR/JIT/ASM/CPU, , , .

, , 16- , .

+4

Source: https://habr.com/ru/post/1685921/


All Articles