c++ - Optimizing composite std::functions -
is possible optimize series of "glued together" std::function
s and/or there implementation attempts this?
what mean expressed mathematically: want make std::function
function of function:
f(x,y,z) = x^2 * y^3 * z^4 g(x,y,z) = f(x,y,z) / (x*y^2)
is there way stl/compiler implementor optimize away parts of arithmetic calling function object of g
, created function object of f
?
this kind of symbolic simplification of functions, because std::function
, have spotted on machine level.
due being optimization, takes time, , isn't free (in clock cycles and/or memory), isn't allowed standard? leans close language typically ran through vm. (i'm thinking llvm more java here, runtime optimizations).
edit: in order make discussion "more useful", here's short code snippet (i understand lambda not std::function
, lambda can stored in std::function
, assuming auto
below means std::function<t>
appropriate t
express meant above):
auto f = [](const double x, const double y, const double z){ return x*x*y*y*y*z*z*z*z; }; auto g = [](const double c, const double y, const double z){ return f(x,y,z)/(x*y*y); };
a "trivial" compiler make g
equivalent
double g(const double x, const double y, const double z){ return x*x*y*y*y*z*z*z*z/(x*y*y); }
while optimized std::function
make (mathematically , in every other sense correct!):
double g( const double x, const double y, const double z){ return x*y*z*z*z*z; }
note although i'm talking mathematical functions here, similar transformations made functions in general sense, take more introspection, means overhead.
i can see being important when designing mathematical , physics simulations, generality of compositing existing library functions user-case functions, usual mathematical simplifications make nice method of expressive, yet performant calculation software.
this why leave optimizing compiler. they're algebraically equivalent not equivalent due fp imprecision. 2 versions of g yield subtly different answers, important if called in inner loop- not mention behavioural difference if x, y, z 0.
secondly, contents of function
unknown until run-time, there's no way compiler perform such optimizations doesn't have data needs.
Comments
Post a Comment