Reputation: 3180
I am at a situation where returning a value from a function is optional. So whether I return or don't return makes no difference logically, but can I safely assume the same thing performance-wise?
I mean are there any performance overheads(time or memory) when we use a function which returns some value(ex: Int
) over the function which returns void
?
This is a dummy project and the question is raised plain out of my curiosity.
Upvotes: 1
Views: 333
Reputation: 136208
It depends on the ABI used and whether the called function is inlined.
On x86_64 platforms with System V Application Binary Interface AMD64 (Linux, FreeBSD, macOS, Solaris and Windows Subsystem for Linux) return values with sizeof
of up to 16 bytes are returned in registers. Returning up to 16 bytes involves loading the return value into one or two 8-byte registers. Returning larger values involves stores into the stack of the caller through the hidden return value pointer passed into the callee, that also must be loaded in rax
register upon return.
See Calling conventions by Agner Fog for a detailed treatment of the calling conventions, in particular §7.1 Passing and returning objects. There are separate calling conventions for passing SIMD types in registers.
Upvotes: 1