Brandon Captain wrote: |
---|
"I was curious about how it breaks down to assembly which I reckon is compiler-specific. I'm mostly trying to figure out if using references is faster when used in loops that are executed every frame (as in a game engine)." |
Yes, the implementation of a reference is implementation defined. Some compilers will implement references as a pointer with automatic dereferencing, or will simply be an alias for the referent. Given the two possible implementations, it's difficult to determine which implementation type is actually be used by your compiler.
If the compiler implements the reference as an alias, and not a pointer, there shouldn't be anything to push onto
function()'s stack frame, because it doesn't allocate memory. If the compiler implements the reference as a pointer, then the reference's internal pointer will have to be pushed onto
function()'s stack frame.
I recommend using the second code example (the one that accepts an
Object by reference), because there's no possibility of a null-pointers (which eliminates a null check), there's a possibility of
ptr not having to be pushed onto the stack, and there's a possibility of no dereference overhead.
Wazzak