5 ms·
Just curious, is there a reason for passing around the Tinn struct by-value? Will it get optimized?
by ptr 8y ago
Just curious, is there a reason for passing around the Tinn struct by-value? Will it get optimized?
- rjeli 8y agoIt’s only a few bytes, pointers to buffers
- yvdriess 8y agoIt's commonly optimized out, yes. Rule of thumb is typically to pass pointers to a struct if it gets fairly large and complex. Even then, you should really profile to see that the becomes a bottleneck, compilers inline aggressively and a pointer might defeat some other optimizations down the call chain.
- tom_mellior 8y agoIt might get optimized by inlining, these functions are pretty small and would make good candidates. Passing small structs by value isn't as expensive as it once was, modern ABIs pass the initial fields in registers. However, this struct does have ten pointer/integer fields, and I'm not aware of a mainstream ABI that allows that many. x86-64 allows up to six on Linux, IIRC. A quick check of the AArch64 ABI says it takes up to eight. So yeah, there will be stack traffic involved if these function calls are not inlined.
- kbumsik 8y agoI'm curious too. Even if it will be optimized isn't it still be better to pass-by-reference?
- skrebbel 8y agoIn C?
- kbumsik 8y agoI mean pointer.
- tom_mellior 8y agoIt might be easier to answer if you explained why you think it would be better. There may be cases where by-reference (yes, that's an absolutely acceptable thing to say, even in C) is more efficient: If the struct is a kind of "this pointer" (as in object-oriented programming) that is passed to a bunch of functions but relatively rarely used, then passing it by reference causes much less register pressure. Passing it by value would mean occupying a lot of registers with values that are probably not accessed. On the other hand, if you expect the callee to do actual computation on all or most of the fields of the struct you pass in, passing it by value should probably be the way to go. This way, the values to be processed are already in registers and don't have to be stored to the stack by the caller and loaded back from the stack by the callee. There is no general answer that is uniformly best, it depends on the characteristics of the actual program and their interactions with the compiler's optimizations. With inlining in particular, which should often allow the compiler to generate the same code for both versions.
- foo101 8y ago> There may be cases where by-reference (yes, that's an absolutely acceptable thing to say, even in C) I don't want to be nit-picking. But I would like to know why "by-reference" is an acceptable thing to say. When I learnt C, C++, Java, etc. I was repeatedly told that pass by reference exists only in C++. In C, a pointer is passed by value. In Java, a reference is passed by value. Doesn't calling "pass pointer by value" as "pass by reference" lead to confusion and distortion of the true meaning of "by reference"?
- pubby 8y agoWhat do you do when you want to find the value a pointer points to? You dereference it of course! C++ and Java have their own specific language-lawyer definitions of the word reference, but outside of those languages, "reference" is a catch-all term that includes pointers, handles, and indexes.
- robotresearcher 8y agoC implements the concept 'pass by reference' using the mechanism of 'pointer passed by value'. You have no problem using the terms 'loop' or 'recurse' to describe C code, right? But these high-level ideas are implemented without using those terms in the code. Same with 'pass by reference'.
- cubano 8y agoI would expect it would depend on what the function actually did with the data.
- glouwbug 8y agoHi, I'm glouw. Structs on the stack I pass by value. Structs on the heap I pass by pointer. It's a mental thing that I find helps with large codebases.
- offbytwo 8y agoUnrelated but thanks for making this! The code is clean and easy to follow. - a student trying to get a better grasp on neural networks