I'm looking to make a general, lazy evaluation-esque procedure to streamline my code.
Right now, I have the ability to speed up the execution of mathematical functions - provided that I pre-process it by calling another method first. More concretely, given a function of the type:
const Eigen::MatrixXd<double, -1, -1> function_name(const Eigen::MatrixXd<double, -1, -1>& input)
I can pass this into another function, g
, which will produce a new version of function_name
g_p
, which can be executed faster.
I would like to abstract all this busy-work away from the end-user. Ideally, I'd like to make a class such that when any function f
matching function_name
's method signature is called on any input (say, x
), the following happens instead:
- The class checks if
f
has been called before. - If it hasn't, it calls
g(f)
, followed byg_p(x)
. - If it has, it just calls
g_p(x)
This is tricky for two reasons. The first, is I don't know how to get a reference to the current method, or if that's even possible, and pass it to g
. There might be a way around this, but passing one function to the other would be simplest/cleanest for me.
The second bigger issue is how to force the calls to g
. I have read about the execute around pattern, which almost works for this purpose - except that, unless I'm understanding it wrong, it would be impossible to reference f
in the surrounding function calls.
Is there any way to cleanly implement my dream class? I ideally want to eventually generalize beyond the type of function_name
(perhaps with templates), but can take this one step at a time. I am also open to other solution to get the same functionality.
g
a function, which takes a function pointer and returns another function pointer? How/where/when do you "produce a new version" of a function? – Ulcerobj->invoke(f, x)
acceptable? – Eger