c++ - Input parameter passing: is there a size threshold for efficient pass-by-value? -


in c++, when input argument cheap copy (e.g. int, float, etc.), it's passed by value. instead, input "observed" arguments aren't cheap copy (e.g. std::string) passed const &.

i wondering types pod representing 2d vector having int coordinates, e.g.

struct vec2i {     int x;     int y; }; 

on 32-bit msvc compiler, it's 8 bytes (2 * sizeof(int)). pass value or const &?

and vec2d having double-type coordinates?
(on msvc 2 * sizeof(double), 2 * 8 = 16 bytes.)

is there "size threshold" (e.g. 16 bytes?) putting line , say: "for pods on size x pass const &, , smaller pods pass value"?


ps: please don't use arguments "premature optimization" in replies.
me sounds case of ++it vs. it++ (where it stl iterator): it's not ++it premature optimization, point it++ premature pessimization :)

behind scenes, 1 driving factor whether it's possible pass variable in 1 or more registers. in 20th century, compilers doing quite when argument type mapped directly register. passing structure 2 members in register pair 21st century optimization.

as mention x86 in comments, that's special case. it's register-starved, , may not have register pair available argument passing. both x86 , arm better in respect, 1 of reasons why x64 faster , arm more power-economic

boost::call_traits<t> attempt figure out whether it's smart pass t reference, it's not perfect.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -