c++ - Improving readability without losing performance? -
let's assume have 3 functions f0, f1, f2
take 3 arguments. let's assume have big function bigfunc
call, takes return values first 3 functions arguments.
i may want make call this:
bigfunc(f0(arg0, arg1, arg2), f1(arg3, arg4, arg5), f2(arg6, arg7, arg8));
this big call, think far more readable write this:
auto bigarg0 = f0(arg0, arg1, arg2); auto bigarg1 = f1(arg3, arg4, arg5); auto bigarg2 = f2(arg6, arg7, arg8); bigfunc(bigarg0, bigarg1, bigarg2);
that's great if names bigarg0, bigarg1, bigarg2
allow me more specific doing (for example if f0, f1, f2
bit generic; can think of stl algorithms, different things depending of type of iterators give it).
the problem this, however, naming bigarg0, bigarg1, bigarg2
, make them not temporaries anymore, (i suppose) harder compiler optimize.
so, here question: what right way this, if don't want lose performance? make bigarg0, bigarg1, bigarg2
const? give arguments bigfunc
through std::move
? bit of both?
do destructor , copy-constructor of return values have observable behavior standard defines them?
if don't , compiler has neccessary info prove it, compiler can ellide copy without needing rvo exception.
perhaps that's case if use std::move
? either way moving might better copying...
or perhaps bigfunc
gets arguments constant reference, in case both ways result in same code anyway...
Comments
Post a Comment