Nick said:
Your question is a bit contradictive. You wish to know what happens below the surface, without 'believing' and without actually going below the surface.
Yeah, I kinda realized that I was being weaselish/lazy as I wrote the question. ;) I'm not averse to reading the dissassembly, I suppose. I guess I'm just daunted by having to read the dissassembly on the various compilers I work with, since the C++ specification [more then likely] doesn't specify these types of things as cross-compiler 'truths'.
I suppose I'll just suck it up and start reading dissassembly to get a better picture -- that said, I don't regret my question as it's led to some very interesting findings from you and the other posters. On that note:
Nick said:
To answer your question directly; return values that fit in 32-bit are stored in the 'eax' register (on 32-bit x86 architectures). Returing values via a pointer or reference always requires a memory operation. Working with registers is faster than working with memory. However, if you're only going to change a few fields in a structure, it's best to pass it by reference (or even better, write a member function).
Interesting! First off, I didn't know this. Is there any 'source' (MSDN, some architecture manual) where I could read this (as well as other goodies) from directly? (Not that I distrust you, but I'd love to know how you came to know this and how I can uncover similar things). I suppose it makes sense since that's the basic 'word' size for a 32-bit computer.
Second; to paraphrase: it's best to use return values for classes/structs/primitives that are < 4 bytes longs, otherwise use some sort of pointer reference (alongside your member function mention). Interesting. I always thought a compiler would be 'smart' enough to pass in my existing register (the variable I'm constructing on the left-hand-side of assignment) as the return register on the stack. I suppose I was asking too much. :)
Nick said:
x86-64 changes these 'truths' significantly though. So really the most productive approach is to learn some assembly, locate the real bottlenecks, and optimize those. If parameter passing/returning is no bottleneck, then don't bother about it.
Just because we're on the topic: I'm wondering if you could qualify 'significantly'. Does x86-64 not just double the size of the eax register? Or has whole gobs of the system changed?
Is there a place I can read about this on my own, or is this a place best uncovered, as you say, with dissassembly and experimentation?
Finally (sorry for the long post, but you and the others gave me reams to respond to. ;)): just to indicate my stance: I'm a total believer in "optimize the problem, don't optimize non-problems" - however I do believe it's good to do basic discipline checks on yourself once in a while, and change your habits accordingly (i.e. "++myFoo;" versus "myFoo++;").
I just have to learn what those types of disciplines should be. :)
Thanks!