0

I have a config file (text) that is read upon application startup. If a flag, say enabled is turned on, somewhere down the line, function handle_enabled should be called. If it's turned off, then handle_disabled should be called.

Obviously this can be easily achieved during run time by using either branching or polymorphism (constructing two classes. But in both cases, some overheads are imposed and the application is microsecond sensitive in terms of performance. This branching can happen hundreds of time in a second.

Is there any obvious design pattern here that allows me to achieve a form of compile-time polymorphism / branching that is configurable using a text file? I have a feeling that I may be asking for a "cheat" of sort as these two concepts seem inherently contradictory. That said, a good practice that yields better performance is appreciated too.

EDIT 1: initialization is not performance critical. Only regular execution is

EDIT 2: I've not done a profiling. Asking mainly to see if I overlook some obvious design. If not, I'll do a profiling and select a solution based on empirical data.

resnet
  • 398
  • 3
  • 12
  • Is performance that important even during startup and initialization? Or only for the actual main part of the program after all initialization is done? – Some programmer dude Aug 05 '19 at 07:10
  • only after init. Will make an edit to the question – resnet Aug 05 '19 at 07:12
  • 3
    CPU branch predictors are pretty damn good if the branch is always the same - have you measured if this really has an impact on your app? (Worst case: build to exes.) – Mat Aug 05 '19 at 07:14
  • @Mat I've not done a profiling yet. I understand that asking this kind of optimization question before profiling is a bit misguided but I am just checking if I'm missing some obvious answer. Didn't know that branch predictors are good if branching is always the same. Will read more about it. Thank you! – resnet Aug 05 '19 at 07:19
  • If you calling a function anyway, why not just store corresponding function pointer/object at setup and call it through pointer/object at runtime? – sklott Aug 05 '19 at 07:23
  • 3
    If you want maximum possible performance code at cost of including full duplicates of critical parts you can make you critical code templated, instantiate all possible variations and call corresponding template after setup. – sklott Aug 05 '19 at 07:26
  • @sklott the discussion here https://stackoverflow.com/questions/20906060/if-statement-vs-function-pointer seems to suggest a somewhat comparable cost between using pointer and branching – resnet Aug 05 '19 at 07:27
  • 1
    @sklott it does seem like the only actual way to go about this without any run-time branching is to duplicate critical parts – resnet Aug 05 '19 at 07:46
  • `This branching can happen hundreds of time in a second` how is that a problem ? Anyway the question still make sense even for recreational purpose. – UmNyobe Aug 05 '19 at 08:46
  • Is the configuration file's content known at compile-time? if yes, you might embed that resource and do constexpr parsing to evaluate the correct function. – Jarod42 Aug 05 '19 at 11:27

1 Answers1

0

If the value for enabled and not enabled can be defined exactly, something like this would work:

const bool enable = 
#define enabled 1
#define disabled 0
#include "flagfile"
#undef enable
#undef disable
;

int main() {
        printf("Enabled: %s\n", enable ? "true" : "false");
        return 0;
}

flagfile can contain either enabled or disabled (0 and 1 or true and false, ... would also work).

The optimizer would remove all branches, that are not used.

MofX
  • 1,569
  • 12
  • 22
  • 1
    it needs to be changeable at runtime – Alan Birtles Aug 05 '19 at 07:28
  • 1
    He wrote "a form of compile-time polymorphism / branching that is configurable using a text file?" so I expected compile time – MofX Aug 05 '19 at 07:39
  • choosing this answer. Ut works in the general case that my question refers to. It's a way to compile the config with the executable. In my case, the production config is distributed via a different system so I can't directly use this but it does answer the question – resnet Aug 07 '19 at 07:17