0

Assuming that I have a processElement function like this:

class InputProcessor {
    public void processElement(T element) {
        nextOperator.processElement(element);
    }
}

Now I'd like to add an on/off switch to count the elements during the "switch is on" period. However I don't want to add an "if else" like this because it may downgrade the performance.

class InputProcessor {
    public void processElement(T element) {
        if (on) {
             count++;
        }
        nextOperator.processElement(element);
    }
}

Is there any way to help implement this? I have an idea but I'm not sure it works as expected(no performance degrading). I'll create an CountInputProcessor just like the InputProcessor except for the count part.

class CountInputProcessor {
    public void processElement(T element) {
        count++;
        nextOperator.processElement(element);
    }
}

And when I switch the feature on, I redefine the operator(the InputProcessor is stateless).

inputProcessor = new CountInputProcessor();
inputProcessor.processElement(element);

And when I switch the feature off, I redefine it as the old InputProcessor.

Jiayi Liao
  • 999
  • 4
  • 15
  • 2
    have you measured it? do you have evidence the downgrade in performance, if any, is significant? – Coderino Javarino Dec 18 '19 at 13:37
  • Just make the flag an `int`, to be either zero or one, and use `count += flag;`, unconditionally. But chances are high that the JVM will compile `if (on) { count++; }` to something similar (or a single “conditional add” instruction if the hardware architecture has it). – Holger Dec 20 '19 at 12:41

2 Answers2

1

I wouldn't worry about the performance hit. Just make sure the topology runs correctly and if it doesn't scale as you'd like then do the usual tuning things like increase the parallelism of your operators.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
0

The performance degradation of this switch will be minuscule thanks to the branch prediction of your JVM. I recommend to read in a related stackoverflow post on the impact of branch prediction. Since the toggle will be mostly off, JVM will speculatively execute the right code path.

If you really want to take your proposed route, you could also create different topologies depending on the flag and use a stop-with-savepoint and restart to switch between them. Of course, you need to make sure that the operators use the same UID, so that state recovery works as expected.

Arvid Heise
  • 3,524
  • 5
  • 11