I have a use case where based on the reading of a const member variable, I will switch between two different logics:
void foo(){
if(member_var_){ // const, initialized at object creation
a();
} else {
b();
}
}
This foo()
is being reference many many times in my code, so I was wondering instead of branching based on member_var
at runtime, I might be able to eliminate this step at object construction:
private std::function<void()> action_; // foo() just calls action_()
// Constructor
if(member_var_){
action_ = a;
} else {
action_ = b;
}
However, I see 7x performance drop in my benchmark, I was expecting some performance lost due to the redirection (an extra function call overhead and some code cache miss), but 7 times is a little surprising, I couldn't find anything alarming in the source code, maybe someone has insight on this? also is there no better solution than dynamically checking the bool variable over and over?