문제

The Problem

I'm writing an application where performance is very important. (Specifically: A raytracer)

I want to have an option for something called "Adaptive Supersampling" in my program. It's fairly simple to implement but I want to have an option to turn it on or off.

Unfortunately I only see two options, which are:

  1. Put an if/else around two separate procedures (which are fairly similar) like so:

    void renderLoop() {
        if(adaptive) {
            doAdaptiveLoop();
        } else {
            doNormalLoop();
        }
    }
    
  2. Put specialized if/else statement scattered around the code.

    void renderLoop() {
        if(adaptive) something();
        else somethingElse();
    
        for(int i = 0; i < LOOP_1; i++) {
            if(adaptive) something1();
            else somethingElse1();
    
            for(int j = 0; j < LOOP_2, j++) {
                if(adaptive) something2();
                else somethingElse2();
            }
        }
        //... So on and so forth
    }
    

Both of these methods are fairly terrible. Method 1 repeats a lot of code, and Method 2 is both messy and inefficient. Both of them make it very difficult to add on many features. If I want to add more features, I have to add more if/else statements at a factorial rate with Method 1, and in a very confusing way in Method 2.

The Question

I've run across this problem many times in performance dependent applications, but I've never really been able to solve it.

The questions is: How do I add toggleable features to my program which is centered around many loops, whose performance will decrease with many boolean tests, and whose code will become messy with those tests?

도움이 되었습니까?

해결책

I also run into this problem on a regular basis, and I usually have to choose between options 1 and 2. If the number of options keeps growing, I usually fall back on the swiss-army-knife method: code generation.

In other words, I write program A to write functions B1, B2, etc. Program A takes the options as arguments and generates the appropriate function Bi as a text file, and then the final program includes all the Bi.

It's not pretty, but pretty is overrated. The advantage is, the common code among all the Bi is only stated in one place, so if you make a change to it, you only have to do it in one place, so you have fewer opportunities to get it wrong.

다른 팁

I think what you are searching for are function pointers or the analogous.

Let me elaborate, you specifically asked for performance, and I agree that chasing pointers might get in your way - BUT since you now seem to have a large number of branches depending on settings somewhere inside you application, you might as well wrap that code up in some function pointers. That does not mean you should call a function through a pointer when just adding two vectors.

On a side note; this is also relevant when parallelizing code; the work-items need not be too large as to mitigate the parallelization efforts but need be large enough to be processed efficiently.

Maybe use a global function pointer variable to hold the loop function your options tell you to use.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top