Cabbage Logo
Back to Cabbage Site

cabbageGetValue speed

I’m interested in speed comparison between widget opcodes and “normal” global values (gk).

Is there any speed penalty when using cabbageGetValue or other widget opcodes?
I guess widget values are global but is there any difference in their access speed compared to gk values?
Is there any speed difference if a widget value is read within an always on instrument into a global gk variable, which is used across multiple instruments/instances, e.g. midi triggered notes, or is it faster to access the widget value directly from within the instruments/instances? In the later case there will generally be more widget “reading”, so it seems that if there is any overhead in using widget opcodes compared to accessing “normal” global variables, it might be better to copy widget values into gk values ones? What approach would you recommend?

The widget reading opcodes don’t do any memory allocation so they don’t really introduce any overhead. They do look up the value stored in the channel but this operation doesn’t involve any real overhead either, but yes, in comparison to accessing once, and using everywhere it might be technically slower, although in practice I doubt anyone would notice.

My typical approach to first write code I find accessible, readable, and easy to maintain. If I see some performance issues I’ll go back and see if I can find the bottleneck, but in practice it has never been down to the use of global vars vs widget opcodes. Oh wait, there is one classic performance issue with the cabbageSet opcode, but it’s not really to do with accessing channels. It’s whereby people use 1 as the trigger value, meaning it will trigger on each k-rate update. This is a complete and utter waste of CPU cycles.

Thanks! Do you mean that reading channel data is as fast as reading a global variable? If this is the case, since global variables also need to be read in all instruments/instances then I’d think using widget opcodes or global variables shouldn’t matter or it would be actually better to use widget opcodes to not allocate additional memory. Is my understanding correct?
Based on this I’m confused why you wrote:

Did you mean this because reading channel data is actually slower than reading global variables?

I’m just trying to get some insight on this because my instruments tend to get big and complex :wink:

Sorry, I see now my phrasing wasn’t the clearest :rofl: Let me clarify. Assigning the channel value to a global var once will technically be faster than doing multiple local assignments. But I don’t think the overhead is anything to worry about.

My number one concern for big and complex instruments is making sure the code is readable, which in turn makes it easier to maintain and spot problems. So personally I’d favour local assignments over global vars. But that’s just my own personal preference :slight_smile:

Got you. Thanks! And I agree that readability is crucial.

1 Like